Re: UTM Values Not Capturing Consistently

BrunoPacheco
Level 2

UTM Values Not Capturing Consistently

I am capturing UTM parameters in hidden fields on Marketo forms via cookie. Recently we noticed that some of users UTM values are not being captured in the hidden fields even though the UTM parameters are in website cookies. We reviewed the java script for the cookie code and its storing the values.

 

Does anyone know if there's a problem or if there are any bugs related to the forms in Marketo?

PS: It is failing in approximately 20% of leads captures

<script>

function setCookie(cname, cvalue, exdays) {
        var existingValue = getCookie(cname); 
        if (existingValue !== cvalue) {

        var d = new Date();

        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));

        var expires = "expires=" + d.toUTCString();

        var domain = "; domain=." + window.location.hostname.split('.').slice(-2).join('.');

        var secure = "; Secure"; // add secure attribute
          
        var sameSite = "; SameSite=Strict"; // add SameSite attribute

        document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/" + domain + secure;
        }
}

 

function getUTMParam(param) {

    var queryString = window.location.search;

    var urlParams = new URLSearchParams(queryString);

    return urlParams.get(param);

}

 

function getCookie(cname) {
    var name = cname + "=";

    var decodedCookie = decodeURIComponent(document.cookie);

    var ca = decodedCookie.split(';');

    for (var i = 0; i < 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 "";

}

 

function setUTMCookie() {
    var expiryDay = 30;
    var expiryVisitor = 30; // update visitor ID duration to 30 days
    var visitorId = getCookie('visitor_id') || ''; // get visitor ID from cookie or set to empty string
    var utm_source = getUTMParam('utm_source');
    var utm_medium = getUTMParam('utm_medium');
    var utm_campaign = getUTMParam('utm_campaign');
    var utm_term = getUTMParam('utm_term');
    var utm_content = getUTMParam('utm_content');
    var utm_id = getUTMParam('utm_id');
    var GCLID = getUTMParam('gclid');
    var MSCLKID = getUTMParam('msclkid');
    var referrer = utm_source?null:document.referrer;
    var current_source = utm_source || referrer;

    try {
        if (current_source) {
            current_source = new URL(current_source);
            current_source = current_source.host.replace("www.","");
        }
    } catch (error) {
        current_source = current_source.replace("www.","");
    }

    var isUTMParam = utm_source || utm_medium || utm_campaign || utm_term || utm_content || utm_id || GCLID || MSCLKID;
    if(!isUTMParam && visitorId ) return;

    // Check if internal_site parameter is present in the URL
    var internal_site = getUTMParam('internal_site');
    if (internal_site === 'true' && visitorId) {
        return; // do not update UTM parameters if navigating within internal site and user is not new
    }

 // Set source and medium based on organic search engine
    if (/^(.*\.)?(goo.gl|360.cn|aliceadsl.fr|alicesuche.aol.de|biglobe.ne.jp|docomo.ne.jp|duckduckgo.com|go.mail.ru|goo.ne.jp|google.co.uk|isearch.avg.com|lo|al.msn.com|m.yahoo.com|money.msn.com|najdi.si|rakuten.co.jp|rambler.ru|search-results.com|search.aol.fr|search.auone.jp|search.babylon.com|search.centrum.cz|search.comcast.net|search.conduit.com|search.incredimail.com|search.lycos.de|search.netscape.com|search.tut.by|search.ukr.net|search.virgilio.it|sesam.no|szukaj.onet.pl|terra.com.br|alice.com|alltheweb.com|altavista.com|aol.com|ask.com|baidu.com|bing.com|cnn.com|daum.net|ecosia.org|ekolay.net|eniro.se|globo.com|google.com|haosou.com|kvasir.no|lycos.com|mamma.com|msn.com|mynet.com|naver.com|ozu.es|qwant.com|seznam.cz|so.com|sogou.com|startsiden.no|szukacz.pl|voila.fr|wp.pl|yahoo.com|yam.com|yandex.com|yahoo.cn|yandex.ru|360|aliceadsl|alicesuche|biglobe|docomo|duckduckgo|go.mail|goo|google|isearch.avg|lo|al.msn|m.yahoo|money.msn|najdi|rakuten|rambler|search-results|search.aol|search.auone|search.babylon|search.centrum|search.comcast|search.conduit|search.incredimail|search.lycos|search.netscape|search.tut|search.ukr|search.virgilio|sesam|szukaj|terra|alice|alltheweb|altavista|aol|ask|baidu|bing|cnn|daum|ecosia|ekolay|eniro|globo|haosou|kvasir|lycos|mamma|msn|mynet|naver|ozu|qwant|seznam|so|sogou|startsiden|szukacz|voila|wp|yahoo|yam|yandex|tutanota|protonmail)$/i.test(current_source) && !utm_medium) {
utm_medium = 'organic';
}
// Set source and medium based on social media website
if (/^(.*.)?(fb.com|instagr.am|lnkd.in|snap.com|pin.it|youtu.be|facebook.com|twitter.com|t.co|linkedin.com|youtube.com|instagram.com|pinterest.com|tumblr.com|snapchat.com|reddit.com|whatsapp.com|wechat.com|line.me|telegram.org|viber.com|vk.com|tiktok.com|twitch.tv|discordapp.com|medium.com|quora.com|slideshare.net|meetup.com|yelp.com|facebook|twitter|linkedin|youtube|instagram|pinterest|tumblr|snapchat|reddit|whatsapp|wechat|line|telegram|viber|vk|tiktok|twitch|discordapp|medium|quora|slideshare|meetup|yelp)$/i.test(current_source) && !utm_medium) {
utm_medium = 'social';
}

// Check if internal_site parameter is present in the URL and visitor is new
if (internal_site === 'true' && !visitorId) {
// generate a new visitor ID and set it in a separate cookie
visitorId = Math.random().toString(36).substr(2, 9);
setCookie('visitor_id', visitorId, expiryVisitor);

// store UTM parameters in cookies
if (utm_campaign !== null && utm_campaign !== '') {
    setCookie('utm_campaign', utm_campaign, expiryDay);
}
if (utm_term !== null && utm_term !== '') {
    setCookie('utm_term', utm_term, expiryDay);
}
if (utm_content !== null && utm_content !== '') {
    setCookie('utm_content', utm_content, expiryDay);
}
if (utm_id !== null && utm_id !== '') {
    setCookie('utm_id', utm_id, expiryDay);
}
if (GCLID !== null && GCLID !== '') {
    setCookie('gclid', GCLID, expiryDay);
}
if (MSCLKID !== null && MSCLKID !== '') {
    setCookie('msclkid', MSCLKID, expiryDay);
}
if (utm_source !== null && utm_source !== '') {
    setCookie('utm_source', utm_source, expiryDay);
}
if (utm_medium !== null && utm_medium !== '') {
    setCookie('utm_medium', utm_medium, expiryDay);
}
}
// Check if internal_site parameter is present in the URL and visitor is not new
else if (internal_site === 'true' && visitorId) {
// Do not store UTM parameters in cookies
}
// Check if internal_site parameter is not present in the URL
else {
// Set all UTM parameters in cookies
  if (!visitorId) {

    // generate a new visitor ID and set it in a separate cookie

    visitorId = Math.random().toString(36).substr(2, 9);

    setCookie('visitor_id', visitorId, expiryVisitor);

}

if (utm_source !== null && utm_source !== '') {
    setCookie('utm_source', utm_source, expiryDay);

}

if (utm_medium !== null && utm_medium !== '') {

    setCookie('utm_medium', utm_medium, expiryDay);

}
if (utm_campaign !== null && utm_campaign !== '') {
setCookie('utm_campaign', utm_campaign, expiryDay);
}
if (utm_term !== null && utm_term !== '') {
setCookie('utm_term', utm_term, expiryDay);
}
if (utm_content !== null && utm_content !== '') {
setCookie('utm_content', utm_content, expiryDay);
}
if (utm_id !== null && utm_id !== '') {
setCookie('utm_id', utm_id, expiryDay);
}
if (GCLID !== null && GCLID !== '') {
setCookie('gclid', GCLID, expiryDay);
}
if (MSCLKID !== null && MSCLKID !== '') {
setCookie('msclkid', MSCLKID, expiryDay);
}
}
  // Remove UTM parameters from cookies if they are not in the URL
if (!utm_campaign) {
if (getCookie('utm_campaign') !== '' && !internal_site) {
setCookie('utm_campaign', '', -1);
}
}
if (!utm_term) {
if (getCookie('utm_term') !== '' && !internal_site) {
setCookie('utm_term', '', -1);
}
}
if (!utm_content) {
if (getCookie('utm_content') !== '' && !internal_site) {
setCookie('utm_content', '', -1);
}
}
if (!utm_id) {
if (getCookie('utm_id') !== '' && !internal_site) {
setCookie('utm_id', '', -1);
}
}
if (!GCLID) {
if (getCookie('gclid') !== '' && !internal_site) {
setCookie('gclid', '', -1);
}
}
if (!MSCLKID) {
if (getCookie('msclkid') !== '' && !internal_site) {
setCookie('msclkid', '', -1);
}
}

// Set source and medium to direct/none if no UTM parameters and referrer is not set
if (!utm_source && (!referrer || referrer == "https://www.accruent.com/")) {
    utm_source = 'direct';
    utm_medium = 'none';
} else if (!utm_source && referrer && referrer.indexOf(window.location.hostname) === -1) {
    utm_source = referrer;
    utm_medium = utm_medium ? utm_medium : 'referral';
} else if (!utm_medium && utm_source) {
    utm_source = current_source;
    utm_medium = 'referral';
} else if (utm_medium && utm_source) {
    utm_source = current_source;
    utm_medium = utm_medium;
}

// Set all UTM parameters in cookies
if (utm_source !== null && utm_source !== '') {
    setCookie('utm_source', utm_source, expiryDay);
}
if (utm_medium !== null && utm_medium !== '') {
    setCookie('utm_medium', utm_medium, expiryDay);
}
}

setUTMCookie();

</script>
1 REPLY 1
Todd_Berlin
Level 2

Re: UTM Values Not Capturing Consistently

We have a similar setup and have noticed the same thing over the past couple of months (at least that long - it may be longer). Our script includes both the cookie value maintenance, and the adding of hidden fields to Marketo forms.

In doing a bit of quick testing across different browsers with a colleague just now, we've observed different things:

 

  1. Success - sometimes, the cookie is saved and the hidden fields are added to the form successfully.
  2. Partial success - the script loads, and cookie values are stored, but the hidden fields are not added to the form.
  3. Failure - the script does not get loaded on the page at all - as expected, no cookie is stored and no hidden form fields are added.

Can I ask how you add your script to your site? We use GTM and I'm thinking that the complete failure to load something to do with that. I also assume that the partial success is related to the order of when the script is loading vs the form being available to interact with.