|
150 | 150 | configReferrerUrl = locationArray[2],
|
151 | 151 |
|
152 | 152 | // Holder of the logPagePing interval
|
153 |
| - pagePingInterval, |
| 153 | + pagePingInterval = null, |
| 154 | + pagePingCallbackInterval = null, |
154 | 155 |
|
155 | 156 | customReferrer,
|
156 | 157 |
|
|
348 | 349 | preservePageViewId = false,
|
349 | 350 |
|
350 | 351 | // Whether first trackPageView was fired and pageViewId should not be changed anymore until reload
|
351 |
| - pageViewSent = false; |
| 352 | + pageViewSent = false, |
| 353 | + |
| 354 | + // Activity tracking config for callback and pacge ping variants |
| 355 | + activityTrackingConfig = {}, |
| 356 | + activityTrackingCallbackConfig = {}; |
352 | 357 |
|
353 | 358 | // Object to house gdpr Basis context values
|
354 | 359 | let gdprBasisData = {};
|
|
1550 | 1555 | // Send ping (to log that user has stayed on page)
|
1551 | 1556 | var now = new Date();
|
1552 | 1557 |
|
1553 |
| - if (activityTrackingEnabled && !activityTrackingInstalled) { |
| 1558 | + if ((activityTrackingConfig.activityTrackingEnabled || activityTrackingCallbackConfig.activityTrackingEnabled) && !activityTrackingInstalled) { |
1554 | 1559 | activityTrackingInstalled = true;
|
1555 | 1560 |
|
1556 | 1561 | // Add mousewheel event handler, detect passive event listeners for performance
|
|
1607 | 1612 |
|
1608 | 1613 | // Periodic check for activity.
|
1609 | 1614 | lastActivityTime = now.getTime();
|
1610 |
| - clearInterval(pagePingInterval); |
1611 |
| - pagePingInterval = setInterval(function heartBeat() { |
1612 |
| - var now = new Date(); |
1613 |
| - |
1614 |
| - // There was activity during the heart beat period; |
1615 |
| - // on average, this is going to overstate the visitDuration by configHeartBeatTimer/2 |
1616 |
| - if ((lastActivityTime + configHeartBeatTimer) > now.getTime()) { |
1617 |
| - // Send ping if minimum visit time has elapsed |
1618 |
| - if (configMinimumVisitTime < now.getTime()) { |
1619 |
| - logPagePing(finalizeContexts(context, contextCallback)); // Grab the min/max globals |
1620 |
| - } |
1621 |
| - } |
1622 |
| - }, configHeartBeatTimer); |
| 1615 | + } |
| 1616 | + |
| 1617 | + if (activityTrackingConfig.activityTrackingEnabled && pagePingInterval === null && activityTrackingInstalled) { |
| 1618 | + pagePingInterval = activityInterval({ |
| 1619 | + ...activityTrackingConfig, |
| 1620 | + callback: args => logPagePing({ context: finalizeContexts(context, contextCallback), ...args }) // Grab the min/max globals |
| 1621 | + }); |
| 1622 | + } |
| 1623 | + |
| 1624 | + if (activityTrackingCallbackConfig.activityTrackingEnabled && pagePingCallbackInterval === null && activityTrackingInstalled) { |
| 1625 | + pagePingCallbackInterval = activityInterval(activityTrackingCallbackConfig); |
1623 | 1626 | }
|
1624 | 1627 | }
|
1625 | 1628 |
|
| 1629 | + function activityInterval({ activityTrackingEnabled, configHeartBeatTimer, configMinimumVisitTime, callback }) { |
| 1630 | + if (!activityTrackingEnabled) return; |
| 1631 | + |
| 1632 | + return setInterval(function heartBeat() { |
| 1633 | + var now = new Date(); |
| 1634 | + |
| 1635 | + // There was activity during the heart beat period; |
| 1636 | + // on average, this is going to overstate the visitDuration by configHeartBeatTimer/2 |
| 1637 | + if ((lastActivityTime + configHeartBeatTimer) > now.getTime()) { |
| 1638 | + // Send ping if minimum visit time has elapsed |
| 1639 | + if (configMinimumVisitTime < now.getTime()) { |
| 1640 | + refreshUrl(); |
| 1641 | + callback({ pageViewId: getPageViewId(), minXOffset, minYOffset, maxXOffset, maxYOffset }); |
| 1642 | + resetMaxScrolls(); |
| 1643 | + } |
| 1644 | + } |
| 1645 | + }, configHeartBeatTimer); |
| 1646 | + } |
| 1647 | + |
1626 | 1648 | /**
|
1627 | 1649 | * Log that a user is still viewing a given page
|
1628 | 1650 | * by sending a page ping.
|
|
1631 | 1653 | *
|
1632 | 1654 | * @param context object Custom context relating to the event
|
1633 | 1655 | */
|
1634 |
| - function logPagePing(context) { |
1635 |
| - refreshUrl(); |
| 1656 | + function logPagePing({ context, minXOffset, minYOffset, maxXOffset, maxYOffset }) { |
1636 | 1657 | var newDocumentTitle = documentAlias.title;
|
1637 | 1658 | if (newDocumentTitle !== lastDocumentTitle) {
|
1638 | 1659 | lastDocumentTitle = newDocumentTitle;
|
|
1647 | 1668 | cleanOffset(minYOffset),
|
1648 | 1669 | cleanOffset(maxYOffset),
|
1649 | 1670 | addCommonContexts(context));
|
1650 |
| - resetMaxScrolls(); |
1651 | 1671 | }
|
1652 | 1672 |
|
1653 | 1673 | /**
|
|
2052 | 2072 | apiMethods.enableActivityTracking = function (minimumVisitLength, heartBeatDelay) {
|
2053 | 2073 | if (minimumVisitLength === parseInt(minimumVisitLength, 10) &&
|
2054 | 2074 | heartBeatDelay === parseInt(heartBeatDelay, 10)) {
|
2055 |
| - activityTrackingEnabled = true; |
2056 |
| - configMinimumVisitTime = new Date().getTime() + minimumVisitLength * 1000; |
2057 |
| - configHeartBeatTimer = heartBeatDelay * 1000; |
| 2075 | + activityTrackingConfig = { |
| 2076 | + activityTrackingEnabled: true, |
| 2077 | + configMinimumVisitTime: new Date().getTime() + minimumVisitLength * 1000, |
| 2078 | + configHeartBeatTimer: heartBeatDelay * 1000 |
| 2079 | + } |
| 2080 | + } else { |
| 2081 | + helpers.warn("Activity tracking not enabled, please provide integer values " + |
| 2082 | + "for minimumVisitLength and heartBeatDelay.") |
| 2083 | + } |
| 2084 | + }; |
| 2085 | + |
| 2086 | + /** |
| 2087 | + * Enables page activity tracking (replaces collector ping with callback). |
| 2088 | + * |
| 2089 | + * @param int minimumVisitLength Seconds to wait before sending first page ping |
| 2090 | + * @param int heartBeatDelay Seconds to wait between pings |
| 2091 | + * @param function callback function called with ping data |
| 2092 | + */ |
| 2093 | + apiMethods.enableActivityTrackingCallback = function (minimumVisitLength, heartBeatDelay, callback) { |
| 2094 | + if (minimumVisitLength === parseInt(minimumVisitLength, 10) && |
| 2095 | + heartBeatDelay === parseInt(heartBeatDelay, 10)) { |
| 2096 | + activityTrackingCallbackConfig = { |
| 2097 | + activityTrackingEnabled: true, |
| 2098 | + configMinimumVisitTime: new Date().getTime() + minimumVisitLength * 1000, |
| 2099 | + configHeartBeatTimer: heartBeatDelay * 1000, |
| 2100 | + callback |
| 2101 | + } |
2058 | 2102 | } else {
|
2059 | 2103 | helpers.warn("Activity tracking not enabled, please provide integer values " +
|
2060 | 2104 | "for minimumVisitLength and heartBeatDelay.")
|
|
0 commit comments