Skip to content

Commit

Permalink
feat(analytics-widget): add a new parameter pushInitialSearch (#1963)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Petr authored and vvo committed Feb 10, 2017
1 parent cce4f2e commit d777997
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions dev/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ search.addWidget(
// }]);
},
triggerOnUIInteraction: true,
pushInitialSearch: false,
})
);

Expand Down
5 changes: 5 additions & 0 deletions docs/_includes/widget-jsdoc/analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@
<span class="attr-infos">Default:<code class="attr-default">false</code>(<code>boolean</code>)</span>
</p>
<p class="attr-description">Trigger pushFunction after click on page or redirecting the page</p>
<p class="attr-name">
<span class='attr-optional'>`options.pushInitialSearch`<span class="show-description">…</span></span>
<span class="attr-infos">Default:<code class="attr-default">true</code>(<code>boolean</code>)</span>
</p>
<p class="attr-description">Trigger pushFunction after the initial search</p>

<p class="attr-legend">* <span>Required</span></p>
16 changes: 15 additions & 1 deletion src/widgets/analytics/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
* @param {Function} [options.pushFunction] Push function called when data are supposed to be pushed to analytic service
* @param {int} [options.delay=3000] Number of milliseconds between last search key stroke and calling pushFunction
* @param {boolean} [options.triggerOnUIInteraction=false] Trigger pushFunction after click on page or redirecting the page
* @param {boolean} [options.pushInitialSearch=true] Trigger pushFunction after the initial search
* @return {Object}
*/
const usage = `Usage:
analytics({
pushFunction,
[ delay=3000 ],
[ triggerOnUIInteraction=false ]
[ triggerOnUIInteraction=false ],
[ pushInitialSearch=true ]
})`;
function analytics({
pushFunction,
delay = 3000,
triggerOnUIInteraction = false,
pushInitialSearch = true,
} = {}) {
if (!pushFunction) {
throw new Error(usage);
Expand Down Expand Up @@ -106,6 +109,11 @@ function analytics({

let pushTimeout;

let isInitialSearch = true;
if (pushInitialSearch === true) {
isInitialSearch = false;
}

return {
init() {
if (triggerOnUIInteraction === true) {
Expand All @@ -119,6 +127,12 @@ function analytics({
}
},
render({results, state}) {
if (isInitialSearch === true) {
isInitialSearch = false;

return;
}

cachedState = {results, state};

if (pushTimeout) {
Expand Down

0 comments on commit d777997

Please sign in to comment.