|
299 | 299 | * @param event e The event targeting the link
|
300 | 300 | */
|
301 | 301 | function linkDecorationHandler(e) {
|
302 |
| - var duid = loadDomainUserIdCookie()[1]; |
303 | 302 | var tstamp = new Date().getTime();
|
304 |
| - var initialQsParams = '_sp=' + duid + '.' + tstamp; |
305 |
| - var elt = e.target; |
306 |
| - if (elt.href) { |
307 |
| - elt.href = helpers.decorateQuerystring(elt.href, '_sp', duid + '.' + tstamp); |
| 303 | + var initialQsParams = '_sp=' + domainUserId + '.' + tstamp; |
| 304 | + if (this.href) { |
| 305 | + this.href = helpers.decorateQuerystring(this.href, '_sp', domainUserId + '.' + tstamp); |
308 | 306 | }
|
309 | 307 | }
|
310 | 308 |
|
|
319 | 317 | for (var i=0; i<document.links.length; i++) {
|
320 | 318 | var elt = document.links[i];
|
321 | 319 | if (!elt.spDecorationEnabled && crossDomainLinker(elt)) {
|
322 |
| - helpers.addEventListener(elt, 'click', linkDecorationHandler); |
323 |
| - helpers.addEventListener(elt, 'mousedown', linkDecorationHandler); |
| 320 | + helpers.addEventListener(elt, 'click', linkDecorationHandler, true); |
| 321 | + helpers.addEventListener(elt, 'mousedown', linkDecorationHandler, true); |
324 | 322 |
|
325 | 323 | // Don't add event listeners more than once
|
326 | 324 | elt.spDecorationEnabled = true;
|
|
503 | 501 | }
|
504 | 502 | }
|
505 | 503 |
|
| 504 | + /* |
| 505 | + * Sets or renews the session cookie |
| 506 | + */ |
| 507 | + function setSessionCookie() { |
| 508 | + cookie.cookie(getSnowplowCookieName('ses'), '*', configSessionCookieTimeout, configCookiePath, configCookieDomain); |
| 509 | + } |
| 510 | + |
506 | 511 | /*
|
507 | 512 | * Sets the Visitor ID cookie: either the first time loadDomainUserIdCookie is called
|
508 | 513 | * or when there is a new visit or a new page view
|
|
511 | 516 | cookie.cookie(getSnowplowCookieName('id'), _domainUserId + '.' + createTs + '.' + visitCount + '.' + nowTs + '.' + lastVisitTs, configVisitorCookieTimeout, configCookiePath, configCookieDomain);
|
512 | 517 | }
|
513 | 518 |
|
| 519 | + /** |
| 520 | + * Generate a pseudo-unique ID to fingerprint this user |
| 521 | + * Note: this isn't a RFC4122-compliant UUID |
| 522 | + */ |
| 523 | + function createNewDomainUserId() { |
| 524 | + return hash( |
| 525 | + (navigatorAlias.userAgent || '') + |
| 526 | + (navigatorAlias.platform || '') + |
| 527 | + json2.stringify(browserFeatures) + Math.round(new Date().getTime() / 1000) |
| 528 | + ).slice(0, 16); // 16 hexits = 64 bits |
| 529 | + } |
| 530 | + |
| 531 | + /* |
| 532 | + * Generate a new domainUserId and write it to a cookie |
| 533 | + */ |
| 534 | + function generateNewDomainUserId() { |
| 535 | + domainUserId = createNewDomainUserId(); |
| 536 | + if (configUseCookies && configWriteCookies) { |
| 537 | + var nowTs = Math.round(new Date().getTime() / 1000); |
| 538 | + setDomainUserIdCookie(domainUserId, nowTs, 0, nowTs, nowTs); |
| 539 | + } |
| 540 | + } |
| 541 | + |
| 542 | + /* |
| 543 | + * Try to load the domainUserId from the cookie |
| 544 | + * If this fails, generate a new one |
| 545 | + * If there is no session cookie, set one and increment the visit count |
| 546 | + */ |
| 547 | + function initializeDomainUserId() { |
| 548 | + var idCookieValue; |
| 549 | + if (configUseCookies) { |
| 550 | + idCookieValue = getSnowplowCookieValue('id'); |
| 551 | + } |
| 552 | + if (idCookieValue) { |
| 553 | + domainUserId = idCookieValue.split('.')[0]; |
| 554 | + } else { |
| 555 | + generateNewDomainUserId(); |
| 556 | + } |
| 557 | + if (configUseCookies && configWriteCookies) { |
| 558 | + if (!getSnowplowCookieValue('ses')) { |
| 559 | + var idCookie = loadDomainUserIdCookie(); |
| 560 | + idCookie[3] ++; |
| 561 | + idCookie.shift(); |
| 562 | + setDomainUserIdCookie.apply(null, idCookie); |
| 563 | + } |
| 564 | + setSessionCookie(); |
| 565 | + } |
| 566 | + } |
| 567 | + |
514 | 568 | /*
|
515 | 569 | * Load visitor ID cookie
|
516 | 570 | */
|
|
528 | 582 | // New visitor set to 0 now
|
529 | 583 | tmpContainer.unshift('0');
|
530 | 584 | } else {
|
531 |
| - // Domain - generate a pseudo-unique ID to fingerprint this user; |
532 |
| - // Note: this isn't a RFC4122-compliant UUID |
533 |
| - if (!domainUserId) { |
534 |
| - domainUserId = hash( |
535 |
| - (navigatorAlias.userAgent || '') + |
536 |
| - (navigatorAlias.platform || '') + |
537 |
| - json2.stringify(browserFeatures) + nowTs |
538 |
| - ).slice(0, 16); // 16 hexits = 64 bits |
539 |
| - } |
540 | 585 |
|
541 | 586 | tmpContainer = [
|
542 | 587 | // New visitor
|
|
602 | 647 | // Add the page URL last as it may take us over the IE limit (and we don't always need it)
|
603 | 648 | sb.add('url', purify(configCustomUrl || locationHrefAlias));
|
604 | 649 |
|
605 |
| - |
606 | 650 | // Update cookies
|
607 | 651 | if (configUseCookies && configWriteCookies) {
|
608 | 652 | setDomainUserIdCookie(_domainUserId, createTs, visitCount, nowTs, lastVisitTs);
|
609 |
| - cookie.cookie(sesname, '*', configSessionCookieTimeout, configCookiePath, configCookieDomain); |
| 653 | + setSessionCookie(); |
610 | 654 | }
|
611 | 655 | }
|
612 | 656 |
|
|
910 | 954 | */
|
911 | 955 | updateDomainHash();
|
912 | 956 |
|
| 957 | + initializeDomainUserId(); |
| 958 | + |
913 | 959 | if (argmap.crossDomainLinker) {
|
914 | 960 | decorateLinks(argmap.crossDomainLinker);
|
915 | 961 | }
|
|
0 commit comments