Highlighted

Subdomain utag_main__ cookie

Silver Contributor
Silver Contributor

How can I get a page number cookie on a subdomain (that starts on first visit to subdomain) while keeping the session page view count in the the utag_main__pn?

1 REPLY 1

Subdomain utag_main__ cookie

Tealium Employee

Hi @annah_brown

 

So not the easiest thing to accomplish. However saying that, you could do something like:

 

(function(sub_domain, cookie_name, a,b,c,d, e){
  if (location.host === sub_domain) {
    a = (new Date()).getTime();
    b = utag.loader.RC(cookie_name);
    c = a + parseInt(utag.cfg.session_timeout);
    d = a;

    if (!b) {
      b = {};
    } else if (b.ses_id && typeof b._st != "undefined" && parseInt(b._st) < a) {
      delete b.utag_main.ses_id;
    }

    if (!b.ses_id) {
      b.ses_id = d + '';
      b._ss = b._pn = 1;
      b._sn = 1 + parseInt(b._sn || 0);
    } else {
      d = b.ses_id;
      b._ss = 0;
      b._pn = 1 + parseInt(b._pn);
      b._sn = parseInt(b._sn);
    }

    if (isNaN(b._sn) || b._sn < 1) {
      b._sn = b._pn = 1;
    }

    b._st = c + '';
    e = utag.cfg.domain;
    utag.cfg.domain = location.host;
    utag.loader.SC(cookie_name, {
      "_sn": b._sn,
      "_ss": b._ss,
      "_pn": b._pn + ";exp-session",
      "_st": c,
      "ses_id": d + ";exp-session"
    });
    utag.cfg.domain = e;
  }
}("sub.domain.com", "utag_sub_domain"));

This can be placed into a JS extension, scoped to DOM Ready. Or "All Tags" with "Before Load Rules" selected. If the later you will need to wrap the above code in a block like:

 

if (!utag.sub_domain_count) {

  //Insert other block of code here
  
  utag.sub_domain_count = true;
} 

This will now give you an additional cookie on the sub domain, that contains counters just for that domain, as well as having the main cookie, with the overall counter.

 

 

Hopefully this helps.

 

Regards,

 

Adrian

Ask me anything Tealium related or Javascript, or NodeJS.
Please remember to mark solutions as accepted for future searchers.