-
Notifications
You must be signed in to change notification settings - Fork 2
/
GTM TAG timer with measuring time seen on page and variable length by content
66 lines (60 loc) · 3.32 KB
/
GTM TAG timer with measuring time seen on page and variable length by content
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
// Full version
<script>var TimerDynamicSetting = {
count: 0,
repeat: 2,
active: 1, // Is timmer active?
customtime: 10000 // Minimum timer time in seconds
},
TimerDynamicvar;
// Setting time according to content length
// dl.pagePostType2 - Wordpress variable from plugin DuracellTomi's Google Tag Manager for WordPress
try {
if ({{dl.pagePostType2}}.includes("single-")) {
TimerDynamicSetting.customtime = TimerDynamicSetting.customtime + Math.floor((document.querySelector("#main")
.innerText.replace(/(^\s*)|(\s*$)/gi, "")
.replace(/[ ]{2,}/gi, " ").replace(/\n /, "\n")
.split(' ').length / 228) * 60 * 0.25) * 1000;
}
} catch (e) {;};
//Timer with pause and resume function
function TimerDynamic(d, e) {
var a, b, c = e;
this.pause = function() {
window.clearTimeout(a);
c -= new Date - b;
};
this.resume = function() {
b = new Date;
window.clearTimeout(a);
a = window.setTimeout(d, c);
};
this.resume();
}
function TimerDynamicStart() {
TimerDynamicSetting.active = 1;
TimerDynamicvar = new TimerDynamic(function() {
TimerDynamicSetting.count += 1;
TimerDynamicSetting.active = 0;
dataLayer.push({
event: "TimerDynamic",
"TimerDynamic.count": TimerDynamicSetting.count,
"TimerDynamic.repeat": TimerDynamicSetting.repeat,
"TimerDynamic.customtime": TimerDynamicSetting.customtime * 0.001
});
clearTimeout(TimerDynamicvar);
TimerDynamicSetting.count < TimerDynamicSetting.repeat && TimerDynamicStart();
}, TimerDynamicSetting.customtime);
}
function handleVisibilityChangeForTimmer() {
"hidden" == document.visibilityState ? 1 === TimerDynamicSetting.active && TimerDynamicvar.pause() : (dataLayer.push({
event: "visibilityChange.on"
}), 1 === TimerDynamicSetting.active && TimerDynamicvar.resume());
}
TimerDynamicStart();
document.addEventListener("visibilitychange", handleVisibilityChangeForTimmer, !1);</script>
// minimalist version
var TimerDynamicSetting = {count:0, repeat:2, active:1, customtime:10000}, TimerDynamicvar;
try {{{dl.pagePostType2}}.includes("single-") && (TimerDynamicSetting.customtime += 1000 * Math.floor(document.querySelector("#main").innerText.replace(/(^\s*)|(\s*$)/gi, "").replace(/[ ]{2,}/gi, " ").replace(/\n /, "\n").split(" ").length / 228 * 15));} catch (b) {}
function dynamictimer(f){var a,b,c=TimerDynamicSetting.customtime;this.pause=function(){window.clearTimeout(a);c-=new Date-b};this.resume=function(){b=new Date;window.clearTimeout(a);a=window.setTimeout(f,c)};this.resume()}
function DynamicTimerStart(){TimerDynamicSetting.active=1;TimerDynamicvar=new dynamictimer(function(){TimerDynamicSetting.count+=1;TimerDynamicSetting.active=0;dataLayer.push({event:"TimerDynamic","TimerDynamic.count":TimerDynamicSetting.count,"TimerDynamic.repeat":TimerDynamicSetting.repeat,"TimerDynamic.customtime":.001*TimerDynamicSetting.customtime});clearTimeout(TimerDynamicvar);TimerDynamicSetting.count<TimerDynamicSetting.repeat&&DynamicTimerStart()})}DynamicTimerStart();
document.addEventListener("visibilitychange",function(){"hidden"==document.visibilityState?1===TimerDynamicSetting.active&&TimerDynamicvar.pause():(dataLayer.push({event:"visibilityChange.on"}),1===TimerDynamicSetting.active&&TimerDynamicvar.resume())},!1);