-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharealist-gtm.js
634 lines (409 loc) · 16.7 KB
/
arealist-gtm.js
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
(function() {
/* https://www.github.com/plamzi/AreaList-GTM
*
* v1.0.2
*
*/
var _ARL = function(settings) {
settings = settings || {};
settings.areas = settings.areas || [];
settings.data_layer_name = settings.data_layer_name || 'dataLayer';
settings.polling_frequency = settings.polling_frequency || 3;
settings.impressions_event_name = settings.impressions_event_name || 'AreaList Impressions',
settings.clicks_event_name = settings.clicks_event_name || 'AreaList Clicks',
settings.impression_event_name = settings.impression_event_name || 'AreaList Impression',
settings.click_event_name = settings.click_event_name || 'AreaList Click',
settings.storage_key_name = settings.storage_key_name || 'AreaList_GTM';
var data = {
on: {},
areas: settings.areas,
impressions: [],
clicks: [],
seen: [],
clicked: [],
track: {
impressions: !settings.disable_impressions,
clicks: !settings.disable_clicks
},
clickable: 'a, button, input, select, .btn, .button, [onclick], .dropdown, .dropdown-toggle, .clickable, label, *[data-target], i, textarea',
snap: `<div class='arlg' style='position: absolute; z-index: 100000; top: {1}px; left: {2}px; width: {3}px; height: {4}px; box-shadow: inset 0px 0px 30px -3px rgba(194,31,31,1); background: white;'></div>`,
highlight: `<div class='arlgh' style='pointer-events: none; position: absolute; z-index: 100000; top: {2}px; left: {3}px; width: {4}px; height: {5}px;'>
<div style='width: 100%; min-width: 200px; height: 20px; background-color: rgba(52,140,217,1); color: white; padding-left: 6px;'>{1}</div>
<div style='width: 100%; height: {6}px; box-shadow: 0px 0px 13px 0px rgba(52,140,217,1); background: transparent;'></div>
</div>`,
layer: settings.data_layer_name
};
if (settings.extend_clickable)
data.clickable += ', ' + settings.extend_clickable;
var j = (function() {
return window.jQuery;
})();
var clickable = function(e) {
return j(e).find(data.clickable).length;
};
var copy = function(a) {
return JSON.parse(JSON.stringify(a));
};
var log = function() {
if (settings.debug) {
[].unshift.call(arguments, '\uD83D\uDC41 [AreaList GTM]');
console.log.apply(null, arguments);
}
};
var format = function() {
var args = arguments;
return args[0].replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined' ? args[number] : match;
});
};
var save = function() {
//log('save');
if (!window.localStorage)
return log('save:', 'window.localStorage is not available...');
if (settings.disable_storage)
return log('save:', 'global switch disable_storage is on, not persisting...');
var store = {
url: location.href,
path: location.pathname,
impressions: data.impressions,
clicks: data.clicks
}
localStorage.setItem(settings.storage_key_name, JSON.stringify(store));
log('save:', store);
};
var reset = function(what) {
log('reset:', what || 'all');
if (!what || what == 'impressions') { /* not going to reset 'seen' so we can honor impression dedup */
data.seen = data.seen.concat(data.impressions);
data.impressions = [];
}
if (!what || what == 'clicks') {
data.clicked = data.clicked.concat(data.clicks); /* not going to reset 'clicked' so we can honor click dedup */
data.clicks = [];
}
save();
};
var title = function(a, t) {
var needsti = (a.options && a.options.hastitle);
var ti = t.find(a.title).length ? t.find(a.title).first() : null;
if (!ti && t.attr(a.title))
ti = t.attr(a.title);
try {
if (!ti && t.find('[' + a.title + ']'))
ti = t.find('[' + a.title + ']').first().attr(a.title);
}
catch(ex) {
//log('title from attribute exception:', ex);
}
if (!ti)
ti = t.parent().parent().parent().find(a.title).first();
if (!ti && needsti)
ti = t;
if (ti && ti.text)
ti = ti.text().trim().replace(/[\r\n\t]+/g, ' ').replace(/ +/g, ' ');
if (ti && ti.length > 50)
ti = ti.substr(0, 50) + '..';
return ti;
}
var collect = function(evt, nodes) {
if (evt != 'poll')
log('collect:', evt);
var imps = [];
data.areas.map(function(a) {
var tar = j(a.handle), needsti = (a.options && a.options.hastitle);
if (tar.length == 0)
return /* log('collect area not found:', a.name, a.handle) */;
if (a.options && a.options.single && tar.length > 1)
return /* log('collect area condition is single match so skipping:', a.name, a.handle) */;
if (a.options && a.options.multiple && tar.length == 1)
return /* log('collect area condition is multiple match so skipping:', a.name, a.handle) */;
tar.each(function() {
var t = j(this);
var d = {
name: a.name,
title: null,
list: a.list || settings.list || location.pathname,
cat: a.cat || settings.cat || document.title
}
if (a.disabled)
return log('collect area disabled:', d.name, a.handle);
if (a.title) {
var ti = title(a, t);
if (ti)
d.title = ti;
else
if (needsti)
return /* log('collect area condition is needs title so skipping:', d.name, a.handle) */;
}
var tracked = data.impressions.some(function(i) { return i.name == d.name && i.title == d.title; });
var seen = data.seen.some(function(i) { return i.name == d.name && i.title == d.title; });
if (tracked || seen)
return /* log('collect area impression exists:', JSON.stringify(d), a.handle) */;
if (!t.isVis()) {
if (!a.options || a.options.novischeck)
return /* log('collect area here but not visible:', d.name, d.title) */;
}
if (!t.inView()) {
if (!a.options || a.options.novischeck)
return /* log('collect area here & visible but not in viewport:', d.name, d.title) */;
}
if (a.options && a.options.clickable != -1 && !clickable(tar))
return /* log('collect area appears to contain no clickables:', d.name, d.title, tar.html()) */;
if (data.track.impressions) {
data.impressions.push(d);
imps.push(d);
if (settings.disable_storage)
push('impression', d);
}
if (settings.debug) {
var rect = t.get(0).getBoundingClientRect();
var snap = format(data.snap, parseInt(rect.y) + j(document).scrollTop(), parseInt(rect.x) + j(document).scrollLeft(), parseInt(rect.width), parseInt(rect.height));
//console.log('snap:', snap);
//var snap = data.snap;
//t.append(snap);
j('body').prepend(snap);
j('.arlg').fadeOut(600);
setTimeout(function() { j('.arlg').remove(); }, 600);
}
log('collect impression:', JSON.stringify(d), data.impressions);
});
});
if (imps.length) {
save();
trigger('impression', imps);
}
};
var click = function(evt) {
var tar = j(evt.target);
var clicks = [];
if (!tar.is(data.clickable) && !tar.closest(data.clickable).length)
return;
data.areas.map(function(a) {
//log('click begin eval:', JSON.stringify(a));
if (!tar.closest(a.handle).length)
return;
var p = tar.closest(a.handle);
var needsti = (a.options && a.options.hastitle);
if (a.disabled)
return log('click area disabled:', a.name);
if (a.options && a.options.single && j(a.handle).length > 1)
return log('click area condition is single match so skipping:', a.name, a.handle);
if (a.options && a.options.multiple && j(a.handle).length == 1)
return log('click area condition is multiple match so skipping:', a.name, a.handle);
var nodedup = ( a.options && a.options.noclickdedupe );
var d = {
name: a.name,
title: null,
list: a.list || settings.list || location.pathname,
cat: a.cat || settings.cat || document.title
};
if (a.title) {
var ti = title(a, p);
if (ti)
d.title = ti;
else
if (needsti)
return /* log('collect area condition is needs title so skipping:', d.name, a.handle) */;
}
var tracked = nodedup ? false : data.clicked.some(function(i) { return i.name == d.name && i.title == d.title; });
if (tracked)
return log('collect area click exists and dedup is on:', JSON.stringify(d), a.handle);
tracked = data.impressions.some(function(i) { return i.name == d.name && i.title == d.title; });
var seen = data.seen.some(function(i) { return i.name == d.name && i.title == d.title; });
if (!tracked && !seen && data.track.impressions) {
data.impressions.push(d);
log('collect impression on click:', JSON.stringify(d), data.impressions);
if (settings.disable_storage)
push('impression', d);
}
log('collect click:', JSON.stringify(d), data.clicks);
if (data.track.clicks) {
data.clicks.push(d);
clicks.push(d);
if (settings.disable_storage)
push('click', d);
}
});
if (clicks.length) {
save();
trigger('click', clicks, evt);
if (settings.disable_storage) {
push('clicks');
reset('clicks');
}
}
};
var fetch = function(what) {
var store = false;
if (window.localStorage) {
store = localStorage.getItem(settings.storage_key_name);
if (store)
store = JSON.parse(store);
}
log('fetch:', what, 'from', store);
if (!store) {
log('fetch found no store or in-memory tracker, returning null');
return [];
}
if (typeof store[what] != 'undefined')
return store[what];
log('fetch error: no such thing as', what);
};
var push = function(what, one) {
if (what == 'impressions') {
if (!data.impressions.length)
return;
var imp = data.impressions.map(function(i, n) {
var name = i.title ? i.name + ': ' + i.title : i.name;
return {
id: name,
name: name,
category: i.cat || 'AreaList',
list: i.list || fetch('path'),
position: n + 1
};
});
log('push impressions:', imp);
!window[data.layer] || window[data.layer].push({
event: settings.impressions_event_name,
ecommerce: { impressions: imp }
});
}
if (what == 'clicks') {
if (!data.clicks.length)
return;
var clicks = data.clicks.map(function(i, n) {
var name = i.title ? i.name + ': ' + i.title : i.name;
return {
id: name,
name: name,
category: i.cat || 'AreaList',
list: i.list || fetch('path'),
position: n
}
});
log('push clicks:', clicks);
!window[data.layer] || window[data.layer].push({
event: settings.clicks_event_name,
ecommerce: {
click: {
actionField: { list: clicks[0].list || location.pathname },
products: clicks
}
}
});
}
if (what == 'impression') {
log('push impression:', one);
!window[data.layer] || window[data.layer].push({
event: settings.impression_event_name,
arealist: one
});
}
if (what == 'click') {
log('push click:', one);
!window[data.layer] || window[data.layer].push({
event: settings.click_event_name,
arealist: one
});
}
};
var handle = function(t, p) {
var h = '';
if (p)
h = handle(j(t).parent(), false) + ' > ';
h += j(t).get(0).tagName.toLowerCase();
var id = j(t).attr('id');
if (id)
h += '#' + id;
var cls = j(t).attr('class');
if (cls)
h += '.' + cls.split(' ').slice(0, 2).join('.');
return h;
};
var highlight = function() {
if (!data.mouse)
return;
var at = document.elementFromPoint(data.mouse.x, data.mouse.y);
if (!at || data.highlighted == at)
return;
var rect = at.getBoundingClientRect();
if (rect.x <= 0 || rect.y <= 0)
return;
//log('highlight:', rect);
data.highlighted = at;
var ha = handle(at, true);
j('.arlgh').remove();
j('body').prepend( format(data.highlight, ha, rect.y + j(document).scrollTop() - 20, rect.x + j(document).scrollLeft(), rect.width, rect.height + 20, rect.height) );
};
var trigger = function(evt, d, e) {
if (data.on[evt])
data.on[evt].forEach(function(cb) {
log('trigger:', d);
if (e)
cb({ name: d.name, title: d.title, event: e });
else
cb(d);
});
};
var on = function(evt, cb) {
log('_arlg.on', evt, cb);
data.on[evt] = data.on[evt] || [];
data.on[evt].push(cb);
};
var init = function() {
if (!j) {
log('aborting due to lack of jQuery');
return false;
}
j.fn.inView = function() {
var eTop = j(this).offset().top;
var eBottom = eTop + j(this).outerHeight();
var vTop = j(window).scrollTop();
var vBottom = vTop + j(window).height();
return eBottom > vTop && eTop < vBottom;
};
j.fn.isVis = function() {
return j(this).is(':visible') && j(this).css('visibility') != 'hidden' && j(this).css('opacity') != '0';
};
j(window).on('load', function() {
data.impressions = fetch('impressions');
push('impressions');
data.clicks = fetch('clicks');
push('clicks');
reset();
data.seen = [];
data.clicked = [];
collect('load');
});
j(document).on('click', click);
log('init: now live with the following settings:', settings, 'inside', location.href);
if (data.areas.length) {
data._poll = setInterval(function() { collect('poll'); }, parseInt(settings.polling_frequency) * 1000);
log('init: polling activated for ' + data.areas.length + ' defined areas');
}
if (settings.disable_storage) {
window.addEventListener("beforeunload", function(e) {
push('impressions');
localStorage.removeItem(settings.storage_key_name);
});
log('init: settings.disable_storage is on, will push impressions on beforeuload, instantly for clicks');
}
if (settings.debug) {
document.addEventListener('mousemove', function(evt) { data.mouse = evt; }, false);
data._highlight = setInterval(highlight, 1 * 500);
log('init: debug mode highlighting enabled');
}
delete window.arealist_config;
return self;
};
init();
var self = {
on: on,
reset: reset,
data: function() { return data; }
}
return self;
}
window._arlg = window._arlg || new _ARL(window.arealist_config);
})();