|
56 | 56 | * @param functionName global function name
|
57 | 57 | * @param namespace The namespace of the tracker object
|
58 | 58 | * @param version The current version of the JavaScript Tracker
|
59 |
| - * @param pageViewId ID for the current page view, to be attached to all events in the web_page context |
60 | 59 | * @param mutSnowplowState An object containing hasLoaded, registeredOnLoadHandlers, and expireDateTime
|
61 | 60 | * Passed in by reference in case they are altered by snowplow.js
|
62 | 61 | * @param argmap Optional dictionary of configuration options. Supported fields and their default values:
|
|
82 | 81 | * 19. maxPostBytes, 40000
|
83 | 82 | * 20. discoverRootDomain, false
|
84 | 83 | */
|
85 |
| - object.Tracker = function Tracker(functionName, namespace, version, pageViewId, mutSnowplowState, argmap) { |
| 84 | + object.Tracker = function Tracker(functionName, namespace, version, mutSnowplowState, argmap) { |
86 | 85 |
|
87 | 86 | /************************************************************
|
88 | 87 | * Private members
|
|
264 | 263 | commonContexts = [],
|
265 | 264 |
|
266 | 265 | // Enhanced Ecommerce Contexts to be added on every `trackEnhancedEcommerceAction` call
|
267 |
| - enhancedEcommerceContexts = []; |
| 266 | + enhancedEcommerceContexts = [], |
| 267 | + |
| 268 | + // Whether pageViewId should be regenerated after each trackPageView. Affect web_page context |
| 269 | + preservePageViewId = false; |
268 | 270 |
|
269 | 271 | if (argmap.hasOwnProperty('discoverRootDomain') && argmap.discoverRootDomain) {
|
270 | 272 | configCookieDomain = helpers.findRootDomain();
|
271 | 273 | }
|
272 | 274 |
|
273 |
| - if (autoContexts.webPage) { |
274 |
| - commonContexts.push(getWebPageContext()); |
275 |
| - } |
276 |
| - |
277 | 275 | if (autoContexts.gaCookies) {
|
278 | 276 | commonContexts.push(getGaCookiesContext());
|
279 | 277 | }
|
|
748 | 746 | function addCommonContexts(userContexts) {
|
749 | 747 | var combinedContexts = commonContexts.concat(userContexts || []);
|
750 | 748 |
|
| 749 | + if (autoContexts.webPage) { |
| 750 | + combinedContexts.push(getWebPageContext()); |
| 751 | + } |
| 752 | + |
751 | 753 | // Add PerformanceTiming Context
|
752 | 754 | if (autoContexts.performanceTiming) {
|
753 | 755 | var performanceTimingContext = getPerformanceTimingContext();
|
|
813 | 815 | return combinedContexts;
|
814 | 816 | }
|
815 | 817 |
|
| 818 | + /** |
| 819 | + * Initialize new `pageViewId` if it shouldn't be preserved. |
| 820 | + * Should be called when `trackPageView` is invoked |
| 821 | + */ |
| 822 | + function resetPageView() { |
| 823 | + if (!preservePageViewId || mutSnowplowState.pageViewId == null) { |
| 824 | + mutSnowplowState.pageViewId = uuid.v4(); |
| 825 | + } |
| 826 | + } |
| 827 | + |
| 828 | + /** |
| 829 | + * Safe function to get `pageViewId`. |
| 830 | + * Generates it if it wasn't initialized by other tracker |
| 831 | + */ |
| 832 | + function getPageViewId() { |
| 833 | + if (mutSnowplowState.pageViewId == null) { |
| 834 | + mutSnowplowState.pageViewId = uuid.v4(); |
| 835 | + } |
| 836 | + return mutSnowplowState.pageViewId |
| 837 | + } |
| 838 | + |
816 | 839 | /**
|
817 | 840 | * Put together a web page context with a unique UUID for the page view
|
818 | 841 | *
|
|
822 | 845 | return {
|
823 | 846 | schema: 'iglu:com.snowplowanalytics.snowplow/web_page/jsonschema/1-0-0',
|
824 | 847 | data: {
|
825 |
| - id: pageViewId |
| 848 | + id: getPageViewId() |
826 | 849 | }
|
827 | 850 | };
|
828 | 851 | }
|
|
1147 | 1170 | function logPageView(customTitle, context, contextCallback, tstamp) {
|
1148 | 1171 |
|
1149 | 1172 | refreshUrl();
|
| 1173 | + resetPageView(); |
1150 | 1174 |
|
1151 | 1175 | // So we know what document.title was at the time of trackPageView
|
1152 | 1176 | lastDocumentTitle = documentAlias.title;
|
|
1162 | 1186 | purify(customReferrer || configReferrerUrl),
|
1163 | 1187 | addCommonContexts(finalizeContexts(context, contextCallback)),
|
1164 | 1188 | tstamp);
|
1165 |
| - |
| 1189 | + |
1166 | 1190 | // Send ping (to log that user has stayed on page)
|
1167 | 1191 | var now = new Date();
|
1168 | 1192 | if (configMinimumVisitTime && configHeartBeatTimer && !activityTrackingInstalled) {
|
|
2202 | 2226 | trackError: function (message, filename, lineno, colno, error, contexts) {
|
2203 | 2227 | var enrichedContexts = addCommonContexts(contexts);
|
2204 | 2228 | errorManager.trackError(message, filename, lineno, colno, error, enrichedContexts);
|
| 2229 | + }, |
| 2230 | + |
| 2231 | + /** |
| 2232 | + * Stop regenerating `pageViewId` (available from `web_page` context) |
| 2233 | + */ |
| 2234 | + preservePageViewId: function () { |
| 2235 | + preservePageViewId = true |
2205 | 2236 | }
|
2206 | 2237 | };
|
2207 | 2238 | };
|
|
0 commit comments