-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
532 lines (465 loc) · 21.7 KB
/
script.js
File metadata and controls
532 lines (465 loc) · 21.7 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
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
/**
* Duración de Sesión en GTM (V24.0 - Milestones Configurables)
*
* Script profesional para el seguimiento unificado de la duración de la sesión a través de múltiples subdominios.
*
* CARACTERÍSTICAS V24.0:
* - Milestones completamente configurables
* - Detección automática de segundos vs minutos
* - Variables de dataLayer específicas: tiempo_seconds / tiempo_minuts
* - Soporte Cross-Subdomain: Utiliza cookies de origen para mantener el estado de la sesión al navegar entre subdominios.
* - Sistema de Fallback: localStorage/sessionStorage cuando cookies no están disponibles (modo incógnito, extensiones).
* - Detección automática de capacidades de storage y modo incógnito.
* - Validación robusta de estado y expiración automática de sesiones antiguas.
* - Debugging integrado configurable para troubleshooting.
* - Compatibilidad mejorada cross-browser con prefijos vendor.
*
* CONFIGURACIÓN DE MILESTONES:
* Puedes configurar cualquier combinación de segundos y minutos:
* - Segundos: 0,5,10,15,30,45
* - Minutos: 60,120,180,300,600,900,1200,1500,1800 (1,2,3,5,10,15,20,25,30 min)
* - Mixto: 0,5,15,30,60,120,300,600,1200,1800
*
* Realizado por CONVERTIAM.COM By Juan Carlos Díaz
* Contacto: jcarlos@convertiam.com | ¿Necesitas ayuda? ¡Escríbeme!
*/
(function() {
var sessionDurationTracker = {
// --- CONFIGURACIÓN ---
config: {
gtmVariableValue: '{{c_sessionMilestones}}',
// Configuración por defecto con segundos y minutos
defaultMilestones: '0,5,15,30,60,120,180,240,300,600,900,1200,1500,1800', // 0,5,15,30s + 1,2,3,4,5,10,15,20,25,30min
cookieName: 'gtm_session_tracker_state',
maxSessionAge: 24 * 60 * 60 * 1000, // 24 horas máximo para considerar sesión válida
debugMode: false // Para activar logs de debugging
},
MILESTONES: [],
// --- ESTADO INTERNO ---
isDisabled: false,
timer: null,
timeWhenPaused: 0,
nextMilestoneIndex: 0,
storageMethod: 'cookies', // cookies, localStorage, sessionStorage o memory
memoryState: null, // Fallback para modo memoria
// El estado ahora se gestiona en un solo objeto para guardarlo en la cookie.
state: {
startTime: 0,
inactiveTime: 0,
lastMilestoneIndex: 0,
version: 'V24.0'
},
// --- MÉTODOS DE UTILIDAD ---
log: function(message, type) {
if (!this.config.debugMode && type !== 'error') return;
var prefix = 'GTM Session Tracker V24.0: ';
if (type === 'error') {
console.error(prefix + message);
} else if (type === 'warn') {
console.warn(prefix + message);
} else {
console.log(prefix + message);
}
},
detectStorageCapabilities: function() {
var capabilities = {
cookies: false,
localStorage: false,
sessionStorage: false
};
// Test cookies
try {
document.cookie = "gtm_test=1; path=/";
capabilities.cookies = document.cookie.indexOf("gtm_test=1") !== -1;
document.cookie = "gtm_test=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
} catch(e) {
capabilities.cookies = false;
}
// Test localStorage
try {
localStorage.setItem('gtm_test', '1');
localStorage.removeItem('gtm_test');
capabilities.localStorage = true;
} catch(e) {
capabilities.localStorage = false;
}
// Test sessionStorage
try {
sessionStorage.setItem('gtm_test', '1');
sessionStorage.removeItem('gtm_test');
capabilities.sessionStorage = true;
} catch(e) {
capabilities.sessionStorage = false;
}
return capabilities;
},
initStorageMethod: function() {
var capabilities = this.detectStorageCapabilities();
if (capabilities.cookies) {
this.storageMethod = 'cookies';
this.log('Usando cookies para almacenamiento cross-subdomain');
} else if (capabilities.localStorage) {
this.storageMethod = 'localStorage';
this.log('Cookies no disponibles, usando localStorage (sin cross-subdomain)', 'warn');
} else if (capabilities.sessionStorage) {
this.storageMethod = 'sessionStorage';
this.log('Solo sessionStorage disponible, sesión limitada a pestaña actual', 'warn');
} else {
this.storageMethod = 'memory';
this.log('Sin storage persistente, funcionalidad limitada a página actual', 'warn');
}
},
getRootDomain: function() {
var hostname = window.location.hostname;
// Casos especiales para localhost e IPs
if (hostname === 'localhost' || hostname === '127.0.0.1' || /^\d+\.\d+\.\d+\.\d+$/.test(hostname)) {
return hostname;
}
var parts = hostname.split('.');
if (parts.length <= 2) {
return hostname;
}
return parts.slice(-2).join('.');
},
// --- MÉTODOS DE ALMACENAMIENTO UNIFICADOS ---
setSessionState: function(value) {
try {
switch(this.storageMethod) {
case 'cookies':
this.setSessionCookie(value);
break;
case 'localStorage':
localStorage.setItem(this.config.cookieName, JSON.stringify(value));
break;
case 'sessionStorage':
sessionStorage.setItem(this.config.cookieName, JSON.stringify(value));
break;
case 'memory':
this.memoryState = value;
break;
}
} catch(e) {
this.log("Error guardando estado: " + e.message, 'error');
// Fallback a memoria
this.memoryState = value;
this.storageMethod = 'memory';
}
},
getSessionState: function() {
try {
switch(this.storageMethod) {
case 'cookies':
return this.getSessionCookie();
case 'localStorage':
var stored = localStorage.getItem(this.config.cookieName);
return stored ? JSON.parse(stored) : null;
case 'sessionStorage':
var stored = sessionStorage.getItem(this.config.cookieName);
return stored ? JSON.parse(stored) : null;
case 'memory':
return this.memoryState;
}
} catch(e) {
this.log("Error leyendo estado: " + e.message, 'error');
return null;
}
return null;
},
setSessionCookie: function(value) {
try {
var cookieValue = JSON.stringify(value);
var rootDomain = this.getRootDomain();
// Intentar con dominio específico primero
document.cookie = this.config.cookieName + "=" + cookieValue + "; path=/; domain=" + rootDomain;
// Verificar que se guardó correctamente
var testRead = this.getSessionCookie();
if (!testRead || testRead.startTime !== value.startTime) {
// Fallback sin especificar dominio
document.cookie = this.config.cookieName + "=" + cookieValue + "; path=/";
this.log('Fallback a cookie sin dominio específico', 'warn');
}
} catch(e) {
this.log("Error al escribir la cookie: " + e.message, 'error');
throw e;
}
},
getSessionCookie: function() {
try {
var nameEQ = this.config.cookieName + "=";
var ca = document.cookie.split(';');
for(var i=0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) {
var value = c.substring(nameEQ.length,c.length);
var parsed = JSON.parse(value);
// Validar estructura básica del estado
if (parsed && typeof parsed.startTime === 'number' && parsed.startTime > 0) {
return parsed;
} else {
this.log('Estado de cookie inválido, se reiniciará la sesión', 'warn');
return null;
}
}
}
} catch (e) {
this.log("Error al leer o parsear la cookie: " + e.message, 'error');
return null;
}
return null;
},
isValidSession: function(storedState, currentTime) {
if (!storedState || !storedState.startTime) {
return false;
}
// Verificar que la sesión no sea demasiado antigua
var sessionAge = currentTime - storedState.startTime;
if (sessionAge > this.config.maxSessionAge) {
this.log('Sesión expirada por antigüedad, iniciando nueva sesión');
return false;
}
return true;
},
// --- NUEVA LÓGICA PARA DETECTAR SEGUNDOS VS MINUTOS ---
getMilestoneType: function(seconds) {
return seconds < 60 ? 'seconds' : 'minutes';
},
getMilestoneValue: function(seconds) {
if (seconds < 60) {
return seconds; // Para segundos, devolver el valor directo
} else {
return Math.round(seconds / 60); // Para minutos, convertir y redondear
}
},
generateMilestoneData: function(seconds) {
var type = this.getMilestoneType(seconds);
var value = this.getMilestoneValue(seconds);
if (seconds === 0) {
return {
type: 'special',
value: 0,
label: 'page_load',
dataLayerEvent: 'session_duration',
dataLayerVariable: 'tiempo_seconds',
dataLayerValue: 0
};
}
var label, dataLayerVariable;
if (type === 'seconds') {
label = value + '_seconds';
dataLayerVariable = 'tiempo_seconds';
} else {
label = value + '_minutes';
dataLayerVariable = 'tiempo_minuts'; // Mantener el nombre original sin 'e'
}
return {
type: type,
value: value,
label: label,
seconds: seconds,
dataLayerEvent: 'session_duration',
dataLayerVariable: dataLayerVariable,
dataLayerValue: value
};
},
// --- LÓGICA PRINCIPAL (MEJORADA PARA MILESTONES CONFIGURABLES) ---
init: function() {
this.log('Inicializando Session Duration Tracker V24.0');
// Inicializar método de almacenamiento
this.initStorageMethod();
this.loadConfig();
if (this.isDisabled) return;
this.loadState();
this.startTimer();
this.setupVisibilityChange();
if (this.state.lastMilestoneIndex === 0) {
this.fireMilestone(0);
}
this.log('Tracker inicializado correctamente con ' + this.MILESTONES.length + ' milestones configurados');
},
loadState: function() {
var now = new Date().getTime();
var storedState = this.getSessionState();
if (storedState && this.isValidSession(storedState, now)) {
this.state = storedState;
this.log('Estado de sesión existente cargado');
} else {
// Es una nueva sesión (o la cookie no existía/era inválida)
this.state.startTime = now;
this.state.inactiveTime = 0;
this.state.lastMilestoneIndex = 0;
this.state.version = 'V24.0';
this.log('Nueva sesión iniciada');
}
},
fireMilestone: function(milestoneIndex) {
var milestone = this.MILESTONES[milestoneIndex];
if (!milestone) {
this.stopTimer();
this.log('Todos los milestones completados');
return;
}
window.dataLayer = window.dataLayer || [];
// Crear objeto base del dataLayer
var dataLayerObject = {
'event': milestone.dataLayerEvent
};
// Agregar la variable específica según el tipo
dataLayerObject[milestone.dataLayerVariable] = milestone.dataLayerValue;
// Agregar información adicional para debugging
dataLayerObject['session_duration_label'] = milestone.label;
dataLayerObject['session_duration_seconds'] = milestone.seconds;
dataLayerObject['session_storage_method'] = this.storageMethod;
dataLayerObject['session_cross_subdomain'] = this.storageMethod === 'cookies';
dataLayerObject['session_tracker_version'] = 'V24.0';
dataLayerObject['milestone_type'] = milestone.type;
window.dataLayer.push(dataLayerObject);
this.log('Milestone disparado: ' + milestone.label + ' (' + milestone.seconds + 's) - Variable: ' + milestone.dataLayerVariable + ' = ' + milestone.dataLayerValue);
// Guardar el nuevo estado
this.state.lastMilestoneIndex = milestoneIndex + 1;
this.setSessionState(this.state);
},
setupVisibilityChange: function() {
if (this.isDisabled) return;
var self = this;
// Usar el evento más compatible
var visibilityEvent = 'visibilitychange';
if (typeof document.hidden === "undefined") {
if (typeof document.webkitHidden !== "undefined") {
visibilityEvent = 'webkitvisibilitychange';
} else if (typeof document.mozHidden !== "undefined") {
visibilityEvent = 'mozvisibilitychange';
} else if (typeof document.msHidden !== "undefined") {
visibilityEvent = 'msvisibilitychange';
}
}
document.addEventListener(visibilityEvent, function() {
if (self.isDisabled) return;
var isHidden = document.hidden !== undefined ? document.hidden :
document.webkitHidden !== undefined ? document.webkitHidden :
document.mozHidden !== undefined ? document.mozHidden :
document.msHidden !== undefined ? document.msHidden : false;
if (isHidden) {
self.stopTimer();
self.timeWhenPaused = new Date().getTime();
self.log('Página oculta, pausando timer');
} else {
if (self.timeWhenPaused > 0) {
var elapsedWhileHidden = new Date().getTime() - self.timeWhenPaused;
self.state.inactiveTime += elapsedWhileHidden;
self.setSessionState(self.state);
self.timeWhenPaused = 0;
self.log('Página visible, agregado tiempo inactivo: ' + Math.round(elapsedWhileHidden/1000) + 's');
}
self.startTimer();
}
});
},
loadConfig: function() {
try {
var milestoneString = this.config.gtmVariableValue;
if (!milestoneString || milestoneString === 'undefined' || milestoneString === '{{c_sessionMilestones}}') {
milestoneString = this.config.defaultMilestones;
this.log('Usando milestones por defecto: ' + milestoneString);
}
var milestoneSeconds = milestoneString.split(',').map(function(s) { return parseInt(s.trim(), 10); });
var validMilestones = milestoneSeconds.filter(function(s) { return !isNaN(s) && s >= 0; });
if (validMilestones.length === 0) {
throw new Error('No se encontraron milestones válidos');
}
// Ordenar milestones y generar datos completos
var sortedMilestones = validMilestones.sort(function(a, b) { return a - b; });
this.MILESTONES = sortedMilestones.map(function(seconds) {
return this.generateMilestoneData(seconds);
}, this);
this.log('Configuración cargada: ' + this.MILESTONES.length + ' milestones');
this.log('Milestones configurados:', this.MILESTONES.map(function(m) {
return m.label + ' (' + m.dataLayerVariable + '=' + m.dataLayerValue + ')';
}).join(', '));
} catch (e) {
this.log('Error fatal procesando la configuración de hitos: ' + e.message, 'error');
this.isDisabled = true;
}
},
startTimer: function() {
if (this.timer || this.isDisabled) return;
var self = this;
this.timer = setInterval(function() { self.checkMilestones(); }, 1000);
},
stopTimer: function() {
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
},
checkMilestones: function() {
if (this.isDisabled) {
this.stopTimer();
return;
}
try {
var activeTime = Math.floor((new Date().getTime() - this.state.startTime - this.state.inactiveTime) / 1000);
var nextMilestone = this.MILESTONES[this.state.lastMilestoneIndex];
if (nextMilestone && activeTime >= nextMilestone.seconds) {
this.fireMilestone(this.state.lastMilestoneIndex);
}
} catch(e) {
this.log('Error verificando milestones: ' + e.message, 'error');
}
},
// --- MÉTODO PÚBLICO PARA DEBUGGING ---
getSessionInfo: function() {
if (this.isDisabled) return null;
var now = new Date().getTime();
var totalTime = now - this.state.startTime;
var activeTime = totalTime - this.state.inactiveTime;
return {
version: 'V24.0',
storageMethod: this.storageMethod,
totalTime: Math.floor(totalTime / 1000),
activeTime: Math.floor(activeTime / 1000),
inactiveTime: Math.floor(this.state.inactiveTime / 1000),
milestonesReached: this.state.lastMilestoneIndex,
nextMilestone: this.MILESTONES[this.state.lastMilestoneIndex] ? this.MILESTONES[this.state.lastMilestoneIndex] : null,
crossSubdomain: this.storageMethod === 'cookies',
allMilestones: this.MILESTONES.map(function(m) {
return {
label: m.label,
seconds: m.seconds,
variable: m.dataLayerVariable,
value: m.dataLayerValue
};
})
};
}
};
try {
sessionDurationTracker.init();
// Exponer métodos para debugging (opcional)
if (typeof window !== 'undefined') {
window.gtmSessionTrackerDebug = {
getInfo: function() { return sessionDurationTracker.getSessionInfo(); },
enableDebug: function() {
sessionDurationTracker.config.debugMode = true;
sessionDurationTracker.log('Debug mode habilitado - Tracker V24.0');
},
disableDebug: function() {
sessionDurationTracker.config.debugMode = false;
},
getMilestones: function() {
return sessionDurationTracker.MILESTONES.map(function(m) {
return {
label: m.label,
seconds: m.seconds,
variable: m.dataLayerVariable,
value: m.dataLayerValue,
type: m.type
};
});
}
};
}
} catch (e) {
console.error('GTM Session Tracker V24.0: Error fatal inesperado durante la inicialización.', e);
}
})();