Cannot get value of certain cookies - Javascript -


i have been using this code found on github collect utm/other url parameters.

i have been able save parameters in cookie, , pass values hidden input form.

this code loaded through google tag manager on every page of website.

scenario:

-the cookie sessions exist on every page of site expected.

-the main website on non-secure http connection (http://www.example.com).

-a secure page exists on subdomain. (https://www.subdomain.example.com).

problem: on secure, subdomain page, cannot values none of utm cookies. can values visitors, ireferrer, lreferrer, , ilandingpage cookies, not utm cookies.

here code using:

<script type="text/javascript" charset="utf-8">    jquery(document).ready(function(){    var _uf = _uf || {};   _uf.domain = ".exampledomain.com";      var utmcookie;  utmcookie = (function() {   function utmcookie(options) {     if (options == null) {       options = {};     }     this._cookienameprefix = '_uc_';     this._domain = options.domain;     this._sessionlength = options.sessionlength || 1;     this._cookieexpirydays = options.cookieexpirydays || 365;     this._additionalparams = options.additionalparams || [];     this._utmparams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'];     this.writeinitialreferrer();     this.writelastreferrer();     this.writeinitiallandingpageurl();     this.setcurrentsession();     if (this.additionalparamspresentinurl()) {       this.writeadditionalparams();     }     if (this.utmpresentinurl()) {       this.writeutmcookiefromparams();     }     return;   }    utmcookie.prototype.createcookie = function(name, value, days, path, domain, secure) {     var cookiedomain, cookieexpire, cookiepath, cookiesecure, date, expiredate;     expiredate = null;     if (days) {       date = new date;       date.settime(date.gettime() + days * 24 * 60 * 60 * 1000);       expiredate = date;     }     cookieexpire = expiredate != null ? '; expires=' + expiredate.togmtstring() : '';     cookiepath = path != null ? '; path=' + path : '; path=/';     cookiedomain = domain != null ? '; domain=' + domain : '';     cookiesecure = secure != null ? '; secure' : '';     document.cookie = this._cookienameprefix + name + '=' + escape(value) + cookieexpire + cookiepath + cookiedomain + cookiesecure;   };    utmcookie.prototype.readcookie = function(name) {     var c, ca, i, nameeq;     nameeq = this._cookienameprefix + name + '=';     ca = document.cookie.split(';');     = 0;     while (i < ca.length) {       c = ca[i];       while (c.charat(0) === ' ') {         c = c.substring(1, c.length);       }       if (c.indexof(nameeq) === 0) {         return c.substring(nameeq.length, c.length);       }       i++;     }     return null;   };    utmcookie.prototype.erasecookie = function(name) {     this.createcookie(name, '', -1, null, this._domain);   };    utmcookie.prototype.getparameterbyname = function(name) {     var regex, regexs, results;     name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');     regexs = '[\\?&]' + name + '=([^&#]*)';     regex = new regexp(regexs);     results = regex.exec(window.location.search);     if (results) {       return decodeuricomponent(results[1].replace(/\+/g, ' '));     } else {       return '';     }   };    utmcookie.prototype.additionalparamspresentinurl = function() {     var j, len, param, ref;     ref = this._additionalparams;     (j = 0, len = ref.length; j < len; j++) {       param = ref[j];       if (this.getparameterbyname(param)) {         return true;       }     }     return false;   };    utmcookie.prototype.utmpresentinurl = function() {     var j, len, param, ref;     ref = this._utmparams;     (j = 0, len = ref.length; j < len; j++) {       param = ref[j];       if (this.getparameterbyname(param)) {         return true;       }     }     return false;   };    utmcookie.prototype.writecookie = function(name, value) {     this.createcookie(name, value, this._cookieexpirydays, null, this._domain);   };    utmcookie.prototype.writeadditionalparams = function() {     var j, len, param, ref, value;     ref = this._additionalparams;     (j = 0, len = ref.length; j < len; j++) {       param = ref[j];       value = this.getparameterbyname(param);       this.writecookie(param, value);     }   };    utmcookie.prototype.writeutmcookiefromparams = function() {     var j, len, param, ref, value;     ref = this._utmparams;     (j = 0, len = ref.length; j < len; j++) {       param = ref[j];       value = this.getparameterbyname(param);       this.writecookie(param, value);     }   };    utmcookie.prototype.writecookieonce = function(name, value) {     var existingvalue;     existingvalue = this.readcookie(name);     if (!existingvalue) {       this.writecookie(name, value);     }   };    utmcookie.prototype._samedomainreferrer = function(referrer) {     var hostname;     hostname = document.location.hostname;     return referrer.indexof(this._domain) > -1 || referrer.indexof(hostname) > -1;   };    utmcookie.prototype._isinvalidreferrer = function(referrer) {     return referrer === '' || referrer === void 0;   };    utmcookie.prototype.writeinitialreferrer = function() {     var value;     value = document.referrer;     if (this._isinvalidreferrer(value)) {       value = 'direct';     }     this.writecookieonce('referrer', value);   };    utmcookie.prototype.writelastreferrer = function() {     var value;     value = document.referrer;     if (!this._samedomainreferrer(value)) {       if (this._isinvalidreferrer(value)) {         value = 'direct';       }       this.writecookie('last_referrer', value);     }   };    utmcookie.prototype.writeinitiallandingpageurl = function() {     var value;     value = this.cleanurl();     if (value) {       this.writecookieonce('initial_landing_page', value);     }   };    utmcookie.prototype.initialreferrer = function() {     return this.readcookie('referrer');   };    utmcookie.prototype.lastreferrer = function() {     return this.readcookie('last_referrer');   };    utmcookie.prototype.initiallandingpageurl = function() {     return this.readcookie('initial_landing_page');   };    utmcookie.prototype.incrementvisitcount = function() {     var cookiename, existingvalue, newvalue;     cookiename = 'visits';     existingvalue = parseint(this.readcookie(cookiename), 10);     newvalue = 1;     if (isnan(existingvalue)) {       newvalue = 1;     } else {       newvalue = existingvalue + 1;     }     this.writecookie(cookiename, newvalue);   };    utmcookie.prototype.visits = function() {     return this.readcookie('visits');   };    utmcookie.prototype.setcurrentsession = function() {     var cookiename, existingvalue;     cookiename = 'current_session';     existingvalue = this.readcookie(cookiename);     if (!existingvalue) {       this.createcookie(cookiename, 'true', this._sessionlength / 24, null, this._domain);       this.incrementvisitcount();     }   };    utmcookie.prototype.cleanurl = function() {     var cleansearch;     cleansearch = window.location.search.replace(/utm_[^&]+&?/g, '').replace(/&$/, '').replace(/^\?$/, '');     return window.location.origin + window.location.pathname + cleansearch + window.location.hash;   };    return utmcookie;  })();  var utmform, _uf;  utmform = (function() {   function utmform(options) {     if (options == null) {       options = {};     }     this._utmparamsmap = {};     this._utmparamsmap.utm_source = options.utm_source_field || 'usource';     this._utmparamsmap.utm_medium = options.utm_medium_field || 'umedium';     this._utmparamsmap.utm_campaign = options.utm_campaign_field || 'ucampaign';     this._utmparamsmap.utm_content = options.utm_content_field || 'ucontent';     this._utmparamsmap.utm_term = options.utm_term_field || 'uterm';     this._additionalparamsmap = options.additional_params_map || {};     this._initialreferrerfield = options.initial_referrer_field || 'ireferrer';     this._lastreferrerfield = options.last_referrer_field || 'lreferrer';     this._initiallandingpagefield = options.initial_landing_page_field || 'ilandpage';     this._visitsfield = options.visits_field || 'visits';     this._addtoform = options.add_to_form || 'all';     this._formqueryselector = options.form_query_selector || 'form';     this.utmcookie = new utmcookie({       domain: options.domain,       sessionlength: options.sessionlength,       cookieexpirydays: options.cookieexpirydays,       additionalparams: object.getownpropertynames(this._additionalparamsmap)     });     if (this._addtoform !== 'none') {       this.addallfields();     }   }    utmform.prototype.addallfields = function() {     var fieldname, param, ref, ref1;     ref = this._utmparamsmap;     (param in ref) {       fieldname = ref[param];       this.addformelem(fieldname, this.utmcookie.readcookie(param));     }     ref1 = this._additionalparamsmap;     (param in ref1) {       fieldname = ref1[param];       this.addformelem(fieldname, this.utmcookie.readcookie(param));     }     this.addformelem(this._initialreferrerfield, this.utmcookie.initialreferrer());     this.addformelem(this._lastreferrerfield, this.utmcookie.lastreferrer());     this.addformelem(this._initiallandingpagefield, this.utmcookie.initiallandingpageurl());     this.addformelem(this._visitsfield, this.utmcookie.visits());   };    utmform.prototype.addformelem = function(fieldname, fieldvalue) {     var allforms, firstform, form, i, len;     if (fieldvalue) {       allforms = document.queryselectorall(this._formqueryselector);       if (allforms.length > 0) {         if (this._addtoform === 'first') {           firstform = allforms[0];           firstform.insertbefore(this.getfieldel(fieldname, fieldvalue), firstform.firstchild);         } else {           (i = 0, len = allforms.length; < len; i++) {             form = allforms[i];             form.insertbefore(this.getfieldel(fieldname, fieldvalue), form.firstchild);           }         }       }     }   };    utmform.prototype.getfieldel = function(fieldname, fieldvalue) {     var fieldel;     fieldel = document.createelement('input');     fieldel.type = "hidden";     fieldel.name = fieldname;     fieldel.value = fieldvalue;     return fieldel;   };    return utmform;  })();  _uf = window._uf || {};  window.utmform = new utmform(_uf);      /*         var usource = jquery("input[name='usource']").val();         var umedium = jquery("input[name='umedium']").val();         var ucampaign = jquery("input[name='ucampaign']").val();         var ucontent = jquery("input[name='ucontent']").val();         var uterm = jquery("input[name='uterm']").val();         var ireferrer = jquery("input[name='ireferrer']").val();         var lreferrer = jquery("input[name='lreferrer']").val();         var ilandpage = jquery("input[name='ilandpage']").val();         var visits = jquery("input[name='visits']").val();      console.log(usource);     console.log(umedium);     console.log(ucampaign);     console.log(visits);     */       jquery('#saveprofile').on('click', function(e){         console.log('click submitted');         e.preventdefault();         // original javascript code chirp internet: www.chirp.com.au         // please acknowledge use of code including header.  function getcookie(cname) {     var name = cname + "=";     var ca = document.cookie.split(';');     for(var = 0; <ca.length; i++) {         var c = ca[i];         while (c.charat(0)==' ') {             c = c.substring(1);         }         if (c.indexof(name) == 0) {             return c.substring(name.length,c.length);         }     }     return ""; }         var readcampaign = readcookie_uc_utm_campaign         var firstname = jquery('#ownerinformation_firstname').val();         var lastname = jquery('#ownerinformation_lastname').val();         var usource = getcookie('_uc_utm_source');       console.log(usource);         var umedium = getcookie('_uc_utm_medium') || '';         var ucampaign = getcookie('_uc_utm_campaign') || '';         var ucontent = getcookie('_uc_utm_content') || '';         var uterm = getcookie('_uc_utm_term') || '';         var ireferrer = jquery("input[name='ireferrer']").val();         var lreferrer = jquery("input[name='lreferrer']").val();         var ilandpage = jquery("input[name='ilandpage']").val();         var visits = jquery("input[name='visits']").val();         datalayer.push({'event':firstname,'event_cat':lastname,'event_action':usource,'event_label':visits});          jquery.ajax({         url: "https://script.google.com/macros/s/googleappsscript/exec",           data: {'firstname':firstname,'lastname':lastname,'usource':usource, 'umedium':umedium, 'ucampaign':ucampaign, 'ucontent':ucontent, 'uterm':uterm, 'ireferrer':ireferrer, 'lreferrer':lreferrer, 'visits':visits},         type: "post",         datatype: "json"        });         });   }) </script> 

any or guidance appreciated.

thanks,

blaine

the reason why cookies restricted domains prevent security breaches.

see answer way around it: cross-domain cookies


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -