-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathdwanalytics-22.2.js
More file actions
502 lines (431 loc) · 22 KB
/
dwanalytics-22.2.js
File metadata and controls
502 lines (431 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*!
* dwAnalytics - Web Analytics Tracking
* Based partially on Piwik
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
*/
// Create dw namespace if necessary
if (typeof dw == 'undefined') {
var dw = {};
}
if (typeof dw.__dwAnalyticsLoaded == 'undefined')
{
dw.__dwAnalyticsLoaded = true;
// DWAnalytics singleton and namespace
dw.__dwAnalytics = function () {
/************************************************************
* Private data
************************************************************/
var MAX_URL_LENGTH = 2000;
var expireDateTime,
/* plugins */
plugins = {},
/* alias frequently used globals for added minification */
documentAlias = document,
navigatorAlias = navigator,
screenAlias = screen,
windowAlias = window,
hostnameAlias = windowAlias.location.hostname;
/************************************************************
* Private methods
************************************************************/
/*
* Is property (or variable) defined?
*/
function isDefined(property) {
return typeof property !== 'undefined';
}
/*
* DWAnalytics Tracker class
*
* trackerUrl and trackerSiteId are optional arguments to the constructor
*
* See: Tracker.setTrackerUrl() and Tracker.setSiteId()
*/
function Tracker(trackerUrl, siteId) {
/************************************************************
* Private members
************************************************************/
var // Tracker URL
configTrackerUrl = trackerUrl + '?' || '?',
// Document URL
configCustomUrl,
// Document title
configTitle = documentAlias.title,
// Client-side data collection
browserHasCookies = '0',
pageReferrer,
// Plugin, Parameter name, MIME type, detected
pluginMap = {
// document types
pdf: ['pdf', 'application/pdf', '0'],
// media players
quicktime: ['qt', 'video/quicktime', '0'],
realplayer: ['realp', 'audio/x-pn-realaudio-plugin', '0'],
wma: ['wma', 'application/x-mplayer2', '0'],
// interactive multimedia
director: ['dir', 'application/x-director', '0'],
flash: ['fla', 'application/x-shockwave-flash', '0'],
// RIA
java: ['java', 'application/x-java-vm', '0'],
gears: ['gears', 'application/x-googlegears', '0'],
silverlight: ['ag', 'application/x-silverlight', '0']
},
// Guard against installing the link tracker more than once per Tracker instance
linkTrackingInstalled = false,
/*
* encode or escape
* - encodeURIComponent added in IE5.5
*/
escapeWrapper = windowAlias.encodeURIComponent || escape,
/*
* decode or unescape
* - decodeURIComponent added in IE5.5
*/
unescapeWrapper = windowAlias.decodeURIComponent || unescape,
/*
* registered (user-defined) hooks
*/
registeredHooks = {};
/*
* Set cookie value
*/
function setCookie(cookieName, value, daysToExpire, path, domain, secure) {
var expiryDate;
if (daysToExpire) {
// time is in milliseconds
expiryDate = new Date();
// there are 1000 * 60 * 60 * 24 milliseconds in a day (i.e., 86400000 or 8.64e7)
expiryDate.setTime(expiryDate.getTime() + daysToExpire * 8.64e7);
}
documentAlias.cookie = cookieName + '=' + escapeWrapper(value) +
(daysToExpire ? ';expires=' + expiryDate.toGMTString() : '') +
';path=' + (path ? path : '/') +
(domain ? ';domain=' + domain : '') +
(secure ? ';secure' : '');
}
/*
* Get cookie value
*/
function getCookie(cookieName) {
var cookiePattern = new RegExp('(^|;)[ ]*' + cookieName + '=([^;]*)'),
cookieMatch = cookiePattern.exec(documentAlias.cookie);
return cookieMatch ? unescapeWrapper(cookieMatch[2]) : 0;
}
/*
* Send image request to DWAnalytics server using GET.
* The infamous web bug is a transparent, single pixel (1x1) image
* Async with a delay of 100ms.
*/
function getImage(urls) {
dw.__timeoutCallback = function()
{
for (var i = 0; i < urls.length; i++)
{
var image = new Image(1, 1);
image.onLoad = function () {};
image.src = urls[i];
}
};
setTimeout(dw.__timeoutCallback, 100);
}
/*
* Browser plugin tests
*/
function detectBrowserPlugins() {
var i, mimeType;
// Safari and Opera
// IE6: typeof navigator.javaEnabled == 'unknown'
if (typeof navigatorAlias.javaEnabled !== 'undefined' && navigatorAlias.javaEnabled()) {
pluginMap.java[2] = '1';
}
// Firefox
if (typeof windowAlias.GearsFactory === 'function') {
pluginMap.gears[2] = '1';
}
if (navigatorAlias.mimeTypes && navigatorAlias.mimeTypes.length) {
for (i in pluginMap) {
mimeType = navigatorAlias.mimeTypes[pluginMap[i][1]];
if (mimeType && mimeType.enabledPlugin) {
pluginMap[i][2] = '1';
}
}
}
}
/*
* Get page referrer
*/
function getReferrer() {
var referrer = '';
try {
referrer = top.document.referrer;
} catch (e) {
if (parent) {
try {
referrer = parent.document.referrer;
} catch (e2) {
referrer = '';
}
}
}
if (referrer === '') {
referrer = documentAlias.referrer;
}
return referrer;
}
/*
* Does browser have cookies enabled (for this site)?
*/
function hasCookies() {
var testCookieName = '_pk_testcookie';
if (!isDefined(navigatorAlias.cookieEnabled)) {
setCookie(testCookieName, '1');
return getCookie(testCookieName) == '1' ? '1' : '0';
}
return navigatorAlias.cookieEnabled ? '1' : '0';
}
/*
* Log the page view / visit
*/
function logPageView(analyticsContext, collectedData, customTitle) {
var id = Math.random();
var tuples = constructDataSet(analyticsContext, collectedData, customTitle, id);
if (collectedData != null && collectedData.debugEnabled) {
var text = '';
for (var i = 0; i < tuples.length; i++)
{
text += tuples[i][0] + '"=' + tuples[i][1] + '"\n';
}
alert(text);
}
var urls = createUrls(analyticsContext, configTrackerUrl, tuples, id);
getImage(urls);
}
/************************************************************
* Constructor
************************************************************/
/*
* initialize tracker
*/
pageReferrer = getReferrer();
browserHasCookies = hasCookies();
detectBrowserPlugins();
try {
process_anact_cookie();
} catch (err) {};
/************************************************************
* Public data and methods
************************************************************/
return {
/*
* Log visit to this page (called from the bottom of the page).
*/
trackPageView: function (customTitle) {
logPageView(null, null, customTitle);
},
/*
* Log visit to this page (called from the dwac script).
*/
trackPageViewWithProducts: function (analyticsContext, collectedData, customTitle) {
logPageView(analyticsContext, collectedData, customTitle);
}
};
function appendToRequest(/*Array*/ tuple, /*string*/ request) {
var beginningChar = request.charAt(request.length - 1) == '?' ? '' : '&';
return request + beginningChar + tuple[0] + "=" + tuple[1];
}
function lengthOfTuple(/*Array*/ tuple) {
return tuple[0].length + tuple[1].length + 2;
}
function constructDataSet(/*object*/ analyticsContext, collectedData, /*string*/ customTitle, /*number*/ id) {
var tuples = [
["url", escapeWrapper(isDefined(configCustomUrl) ? configCustomUrl : documentAlias.location.href)],
["res", screenAlias.width + 'x' + screenAlias.height],
["cookie", browserHasCookies],
["ref", escapeWrapper(pageReferrer)],
["title", escapeWrapper(isDefined(customTitle) && customTitle != null ? customTitle : configTitle)]
];
// plugin data
for (var index in pluginMap) {
tuples.push([pluginMap[index][0], pluginMap[index][2]])
}
if (analyticsContext != null && analyticsContext.dwEnabled)
{
tuples.push(["dwac", id]);
tuples.push(["cmpn", analyticsContext.sourceCode]);
tuples.push(["tz", analyticsContext.timeZone]);
analyticsContext.category = dw.ac._category;
if (dw.ac._searchData) {
analyticsContext.searchData = dw.ac._searchData;
}
addProducts(analyticsContext, collectedData, tuples);
}
return tuples;
}
function addProducts(/*object*/ analyticsContext, /*object*/ collectedData, /*Array*/ tuples)
{
tuples.push(["pcc", analyticsContext.siteCurrency]);
tuples.push(["pct", analyticsContext.customer]);
tuples.push(["pcat", analyticsContext.category]);
if (analyticsContext.searchData) {
if (analyticsContext.searchData.q) tuples.push(["pst-q", analyticsContext.searchData.q]);
if (analyticsContext.searchData.searchID) tuples.push(["pst-id", analyticsContext.searchData.searchID]);
if (analyticsContext.searchData.refs) tuples.push(["pst-refs", analyticsContext.searchData.refs]);
if (analyticsContext.searchData.sort) tuples.push(["pst-sort", analyticsContext.searchData.sort]);
if (undefined != analyticsContext.searchData.persd) tuples.push(["pst-pers", analyticsContext.searchData.persd]);
if (analyticsContext.searchData.imageUUID) tuples.push(["pst-img", analyticsContext.searchData.imageUUID]);
if (analyticsContext.searchData.suggestedSearchText) tuples.push(["pst-sug", analyticsContext.searchData.suggestedSearchText]);
if (analyticsContext.searchData.locale) tuples.push(["pst-loc", analyticsContext.searchData.locale]);
if (analyticsContext.searchData.queryLocale) tuples.push(["pst-qloc", analyticsContext.searchData.queryLocale]);
if (undefined != analyticsContext.searchData.showProducts) tuples.push(["pst-show", analyticsContext.searchData.showProducts]);
}
var pies = collectedData.productImpressions.getEntries();
var pres = collectedData.productRecommendations.getEntries();
var pves = collectedData.productViews.getEntries();
var count = 0;
for (var i = 0; i < pies.length; i++) {
tuples.push([("pid-" + count), pies[i].value.id]);
tuples.push([("pev-" + count), "event3"]);
count++;
}
for (var j = 0; j < pres.length; j++) {
tuples.push([("pid-" + count), pres[j].value.id]);
tuples.push([("pev-" + count), "event3"]);
tuples.push([("evr4-" + count), 'Yes']);
count++;
}
for (var k = 0; k < pves.length; k++) {
tuples.push([("pid-" + count), pves[k].value.id]);
tuples.push([("pev-" + count), "event4"]);
count++;
}
}
function createUrls(analyticsContext, configTrackerUrl, tuples, id) {
var urls = [];
var request = configTrackerUrl;
for (var i = 0; i < tuples.length; i++) {
// we don't want to break up a product grouping, for example,
// ["pid-0","p1"],["pev-0","event3"],["evr4-0",'Yes'] should not be broken into two separate requests
var nextTupleIsProductAndWouldMakeRequestTooLong = (tuples[i][0].slice(0, "pid-".length) == "pid-")
&& (lengthOfTuple(tuples[i])
+ ((i + 1) < tuples.length ? lengthOfTuple(tuples[i + 1]) : 0)
+ ((i + 2) < tuples.length ? lengthOfTuple(tuples[i + 2]) : 0)
+ request.length > MAX_URL_LENGTH);
var nextTupleIsNotProductAndWouldMakeRequestTooLong = (tuples[i][0].slice(0, "pid-".length) != "pid-")
&& (lengthOfTuple(tuples[i]) + request.length > MAX_URL_LENGTH);
if (nextTupleIsProductAndWouldMakeRequestTooLong || nextTupleIsNotProductAndWouldMakeRequestTooLong) {
urls.push(request);
// close the current request and create a new one,
// the new request should have the basic dwac, cmpn,tz, pcc, pct, pcat values
request = appendToRequest(["dwac", id], configTrackerUrl);
if (analyticsContext != null && analyticsContext.dwEnabled) {
request = appendToRequest(["cmpn", analyticsContext.sourceCode], request);
request = appendToRequest(["tz", analyticsContext.timeZone], request);
request = appendToRequest(["pcc", analyticsContext.siteCurrency], request);
request = appendToRequest(["pct", analyticsContext.customer], request);
request = appendToRequest(["pcat", analyticsContext.category], request);
if (analyticsContext.searchData) {
if (analyticsContext.searchData.q) appendToRequest(["pst-q", analyticsContext.searchData.q], request);
if (analyticsContext.searchData.searchID) appendToRequest(["pst-id", analyticsContext.searchData.searchID], request);
if (analyticsContext.searchData.refs) appendToRequest(["pst-refs", JSON.stringify(analyticsContext.searchData.refs)], request);
if (analyticsContext.searchData.sort) appendToRequest(["pst-sort", JSON.stringify(analyticsContext.searchData.sort)], request);
if (undefined != analyticsContext.searchData.persd) appendToRequest(["pst-pers", analyticsContext.searchData.persd], request);
if (analyticsContext.searchData.imageUUID) appendToRequest(["pst-img", analyticsContext.searchData.imageUUID], request);
if (analyticsContext.searchData.suggestedSearchText) appendToRequest(["pst-sug", analyticsContext.searchData.suggestedSearchText], request);
if (analyticsContext.searchData.locale) appendToRequest(["pst-loc", analyticsContext.searchData.locale], request);
if (analyticsContext.searchData.queryLocale) appendToRequest(["pst-qloc", analyticsContext.searchData.queryLocale], request);
if (undefined != analyticsContext.searchData.showProducts) appendToRequest(["pst-show", analyticsContext.searchData.showProducts], request);
}
}
}
request = appendToRequest(tuples[i], request);
}
// Add the "do not follow" cookie in the URL as a query param
// The cookie is set per-site, and gets applied to all sub-sites, if present
var doNotFollowCookieValue = getCookie('dw_dnt');
// If cookie not found
if (doNotFollowCookieValue === 0
|| doNotFollowCookieValue === ''
|| doNotFollowCookieValue === null
|| doNotFollowCookieValue === false) {
// do nothing, meaning tracking is allowed
}
// Cookie found, i.e. value should be '0' or '1' (string values)
else {
// Set whatever the cookie value is
request = appendToRequest([ "dw_dnt", doNotFollowCookieValue ], request);
}
urls.push(request);
return urls;
}
}
function extractCookieValueAndRemoveCookie(cookieName) {
var cookieValue = extractEncodedCookieValue (cookieName);
if (cookieValue) {
document.cookie = cookieName + '=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT;';
}
return cookieValue;
};
function extractEncodedCookieValue (cookieName) {
return decodeURIComponent (
document.cookie.replace(
new RegExp('(?:(?:^|.*;)\\s*'
+ encodeURIComponent(cookieName).replace(/[\-\.\+\*]/g, '\\$&')
+ '\\s*\\=\\s*([^;]*).*$)|^.*$')
, '$1').replace(/\+/g, '%20')
) || null;
}
function process_anact_cookie() {
if (dw.ac) {
dw.ac._anact=extractCookieValueAndRemoveCookie('__anact');
if ( dw.ac._anact && !dw.ac.eventsIsEmpty() ) {
return;
}
if ( dw.ac._anact_nohit_tag || dw.ac._anact ) {
var unescaped = dw.ac._anact_nohit_tag ? dw.ac._anact_nohit_tag : dw.ac._anact;
var jsonPayload = JSON.parse(unescaped);
if (jsonPayload) {
var payload = Array.isArray(jsonPayload) ? jsonPayload[0] : jsonPayload;
if (payload
&& 'viewSearch' == payload.activityType
&& payload.parameters) {
var params = payload.parameters;
var search_params = {};
search_params.q = params.searchText;
search_params.suggestedSearchText = params.suggestedSearchText;
search_params.persd = params.personalized;
search_params.refs = params.refinements;
search_params.sort = params.sorting_rule;
search_params.imageUUID = params.image_uuid;
search_params.showProducts = params.showProducts;
search_params.searchID = params.searchID;
search_params.locale = params.locale;
search_params.queryLocale = params.queryLocale;
dw.ac.applyContext({searchData: search_params});
var products = params.products;
if (products && Array.isArray(products)) {
for (i = 0; i < products.length; i++) {
if (products[i]) {
dw.ac._capture({ id : products[i].id, type : dw.ac.EV_PRD_SEARCHHIT });
}
}
}
}
}
}
}
};
/************************************************************
* Public data and methods
************************************************************/
return {
/*
* Get Tracker
*/
getTracker: function (analyticsUrl) {
return new Tracker(analyticsUrl);
}
};
}();
}