12 Replies Latest reply on Jul 23, 2013 8:34 AM by Dan Reed

    Capturing UTM Data With Cookies Through JavaScript, need help editing code


      I"m a little confused, if we are trying to capture the following:

      utm_campaign
      utm_source
      utm_content
      utm_medium
      utm_term


      We want to edit the following code:

      // Get the values from the cookies and put them into the hidden fields
        $jQ(searchStringField).attr("value",$jQ.cookie('mktoSearchString'));
        $jQ(searchEngineField).attr("value",$jQ.cookie('mktoSearchEngine'));
        $jQ(payPerClickKeywordField).attr("value",$jQ.cookie('mktoPPCKeyword'));


      But what should we change this to? What the field values are and then do this for each form field we want to capture like this?

      // Get the values from the cookies and put them into the hidden fields
        $jQ(sourceField).attr("value",$jQ.cookie('utm_source_c'));
       
      and then repeat this for each of the form fields? or just edit the fields specified in the code (ppc keyword, search engine string and search engine)? And what if we are not capturing Search Engine String or Search Engine should we still keep the code as is?

      Thank you very much!!!
        • Re: Capturing UTM Data With Cookies Through JavaScript, need help editing code
          Dan Reed

               Hi Doug,

               This solution requires some intermediate to advanced javascript and html knowledge, so I will answer with that assumption.

               There are 4 important steps in the Community article you are referencing (http://community.marketo.com/MarketoArticle?id=kA050000000Kyxt) to accomplish this. I'm only going to address those parts that need to be changed by you, not the entire code snippet:

               1. Store the id of your hidden field in a javascript variable
               2. Store the value from your URL parameter in a javascript variable
               3. Move URL parameter variable value to a browser cookie
               4. Move from cookie to hidden field on form

               Assuming your field id is "UTM_Campaign" and the url parameter looks like "utm_campaign", here are the relevant pieces of code:

               Step 1:
               // IDs for the fields to be updated.
               var utmcampaignFieldID = "#UTM_Campaign"

                


               Step 2:
               // Store the URL parameter value in the variable "ucampaign"
                     var ucampaign = $jQ.getQueryString({ID: utm_campaign,
                       URL: refer, DefaultValue: "" });

               Step 3:
               // Create a cookie named "utmcampaigncookie" and store the value from the variable "ucampaign"
               $jQ.cookie('ucampaign', utmcampaigncookie,
                        {expires: 730, path: '\/', domain: cookieDomain});

               Step 4:
               // Get the values from the cookies and put them into the hidden field by referencing the field ID
               $jQ(utmcampaignFieldID).attr("value",$jQ.cookie('utmcampaigncookie'));

               If you need a more comprehensive answer, or would like someone to implement the code for you, I'd be glad to help in a more formal setting. Email me at dan@spearmarketing.com.

               Thanks!

          • Re: Capturing UTM Data With Cookies Through JavaScript, need help editing code
            Dan Reed

                 Hi Doug,

                 This solution requires some intermediate to advanced javascript and html knowledge, so I will answer with that assumption.

                 There are 4 important steps in the Community article you are referencing (http://community.marketo.com/MarketoArticle?id=kA050000000Kyxt) to accomplish this. I'm only going to address those parts that need to be changed by you, not the entire code snippet:

                 1. Store the id of your hidden field in a javascript variable
                 2. Store the value from your URL parameter in a javascript variable
                 3. Move URL parameter variable value to a browser cookie
                 4. Move from cookie to hidden field on form

                 Assuming your field id is "UTM_Campaign" and the url parameter looks like "utm_campaign", here are the relevant pieces of code:

                 Step 1:
                 // IDs for the fields to be updated.
                 var utmcampaignFieldID = "#UTM_Campaign"


                 Step 2:
                 // Store the URL parameter value in the variable "ucampaign"
                       var ucampaign = $jQ.getQueryString({ID: utm_campaign,
                         URL: refer, DefaultValue: "" });

                 Step 3:
                 // Create a cookie named "utmcampaigncookie" and store the value from the variable "ucampaign"
                 $jQ.cookie('ucampaign', utmcampaigncookie,
                          {expires: 730, path: '\/', domain: cookieDomain});

                 Step 4:
                 // Get the values from the cookies and put them into the hidden field by referencing the field ID
                 $jQ(utmcampaignFieldID).attr("value",$jQ.cookie('utmcampaigncookie'));

                 If you need a more comprehensive answer, or would like someone to implement the code for you, I'd be glad to help in a more formal setting. Email me at dan@spearmarketing.com.

                 Thanks!

            • Re: Capturing UTM Data With Cookies Through JavaScript, need help editing code
              Hi Dan,

              Thanks so much for the reply, unfortunately I added the code as you suggested and for some reason the UTM data is not appearing within the Marketo Lead Database. I am sure you are really busy but here is the code, I included it within the JavaScript File code for Marketo forms on non-Marketo Pages:

              <script src="http://code.jquery.com/jquery-latest.min.js" charset="utf-8" type="text/javascript"></script>
              <script src="/sites/all/themes/tekzennew/js/jquery.cookie.js" type="text/javascript"></script>
              <script src="/sites/all/themes/tekzennew/js/jQueryString-2.0.2-Min.js" type="text/javascript"></script>
              
              <script type="text/javascript">
              var $jQ = jQuery.noConflict();
              
              $jQ(document).ready(function(){
              
                 //
                 //--- CHANGE THESE!!! ---
                 //
              
                  // Change this to match domains of referrers you want to ignore.
                  // You'll want to ignore referrers from your own domains.
                  // Use only the base domain name without subdomains (ex. "company.com")
                  // Separate multiple domains with commas (leave the brackets).
                var excludedReferrers = [ "tekscan.com" ];
              
                  // Change this to match the base domain of your company's landing pages.
                  // Cookies will be created with this domain.
                  // Ex. If your landing page domain is "pages.yourcompany.com" then use
                  //     "yourcompany.com"
                var cookieDomain = "tekscan.com";
              
                  // The URL parameter that has your pay-per-click info.
                  // Typically "kw" or "keyword" (depends on how you set up your PPC URLs)
                var payPerClickParameter = "utm_term";
                
                //IDs for the fields to be updated.
                var utmcampaignFieldID = "#UTM_Campaign"
                var utmsourceFieldID = "#UTM_Source"
                var utmtermFieldID = "#UTM_Term"
                var utmcontentFieldID = "#UTM_Content"
                var utmmediumFieldID = "#UTM_Medium"
              
                 //
                 //-- you probably shouldn't change anything after this --
                 //
              
                var refer = document.referrer;
                var searchString;
                var searchEngine;
              
                    // if there's no referrer, do nothing
                if ( (refer == undefined) || (refer == "") ) { ; }
                else {
                   // get the domain of the referring website -- http://[[this-thing.com]]/
                  var referrerDomain =
                    refer.substr(refer.indexOf("\/\/") + 2,
                      refer.indexOf("\/",8) - refer.indexOf("\/\/") - 2).toLowerCase();
              
                  var excludedDomainFound = false;
                  var i = 0;
              
                    // search the excluded domain list to see if the referrer domain is on it
                  while ( (i < excludedReferrers.length) && !excludedDomainFound) {
                    var thisExcludedDomain = excludedReferrers[i].toLowerCase();
              
                      // weird semantics here -- indexOf returns "-1" if the search string isnt found.
                      // thus excludedDomainFound is true only when indexOf matches an excluded domain (!= -1)
                    excludedDomainFound = (referrerDomain.indexOf(thisExcludedDomain) != -1);
                    i++;
                  }
              
                   // only if the referrer isn't in our excluded domain list...
                  if( !excludedDomainFound ) {
                      // extract the URL parameters from common search engines
                      // To add your own, each engine needs:
                      //  name: how the search engine will appear on your Marketo leads
                      //  url: REGEX for matching the engine's referrer.  ex.  /\.google\./i
                      //  query: URL parameter that contains the search query - usually "p" or "q"
              
                    var searchEngines = [
                     { name: "Yahoo", url: /\.yahoo\.co/i, query: "p" },
                     { name: "Google", url: /\.google\./i, query: "q" },
                     { name: "Microsoft Live", url: /\.live\.com/i, query: "q" },
                     { name: "MSN Search", url: /search\.msn\./i, query: "q" },
                     { name: "AOL", url: /\.aol\./i, query: "query" },
                     { name: "Bing", url: /\.bing\.com/i, query: "q" },
                     { name: "Ask", url: /\.ask\.com/i, query: "q" }
                    ];
              
                      // find the referring search engine (if any)
                    i = 0;
                    while (i < searchEngines.length) {
                      if (refer.match(searchEngines[i].url)) {
                        searchEngine = searchEngines[i].name;
                        searchString = $jQ.getQueryString({ ID: searchEngines[i].query,
                          URL: refer, DefaultValue: "" });
                        break;
                      }
                      i++;
                    }
                       // If no search engine is found, this person probably used a less
                       // popular one.  Use the referring doman, then guess the query parameter
                    if (i == searchEngines.length) {
              
                       searchEngine = referrerDomain;
              
                       var queries = ["q","p","query"];
                       var i = 0;
                       while ((i < queries.length) && (searchString == undefined)) {
                         searchString = $jQ.getQueryString({ ID: queries[i], URL: refer });
                         i++;
                       }
              
                         // no search strings found -- use this text instead.
                       if (searchString == undefined) {
                         searchString = "None";
                       }
                    }
              
                       // Use the provided URL parameter to get the PPC keyword.
                    var payPerClickWord = $jQ.getQueryString({ID: payPerClickParameter,
                      URL: refer, DefaultValue: "" });
                        
                        // Store the URL parameter value in the variable "ucampaign"
                  var ucampaign = $jQ.getQueryString({ID: utm_campaign, URL: refer, DefaultValue: "" });
                   var usource = $jQ.getQueryString({ID: utm_source, URL: refer, DefaultValue: "" });
                  var uterm = $jQ.getQueryString({ID: utm_term, URL: refer, DefaultValue: "" });
                  var ucontent = $jQ.getQueryString({ID: utm_content, URL: refer, DefaultValue: "" });               
                  var umedium = $jQ.getQueryString({ID: utm_medium, URL: refer, DefaultValue: "" });
              
                       // Put the info into cookies.  These values will be extracted
                       // and put into a Marketo form later.  Expires in 2 years.
                    $jQ.cookie('mktoPPCKeyword', payPerClickWord,
                       {expires: 730, path: '\/', domain: cookieDomain});
                    $jQ.cookie('mktoSearchEngine', searchEngine,
                       {expires: 730, path: '\/', domain: cookieDomain});
                    $jQ.cookie('mktoSearchString', searchString,
                       {expires: 730, path: '\/', domain: cookieDomain});
                         
                    // Create a cookie named "utmcampaigncookie" and store the value from the variable "ucampaign"
                   $jQ.cookie('ucampaign', utmcampaigncookie,
                       {expires: 730, path: '\/', domain: cookieDomain});
                   $jQ.cookie('usource', utmsourcecookie,
                       {expires: 730, path: '\/', domain: cookieDomain});
                   $jQ.cookie('uterm', utmtermcookie,
                       {expires: 730, path: '\/', domain: cookieDomain});
                   $jQ.cookie('ucontent', utmcontentcookie,
                       {expires: 730, path: '\/', domain: cookieDomain});
                   $jQ.cookie('umedium', utmmediumcookie,
                       {expires: 730, path: '\/', domain: cookieDomain});
                         
                         
                         
                         
                  }
                }
               
                  // Get the values from the cookies and put them into the hidden fields
                $jQ(searchStringField).attr("value",$jQ.cookie('mktoSearchString'));
                $jQ(searchEngineField).attr("value",$jQ.cookie('mktoSearchEngine'));
                $jQ(payPerClickKeywordField).attr("value",$jQ.cookie('mktoPPCKeyword'));
                
                // Get the values from the cookies and put them into the hidden field by referencing the field ID
                $jQ(utmcampaignFieldID).attr("value",$jQ.cookie('utmcampaigncookie'));
                $jQ(utmsourceFieldID).attr("value",$jQ.cookie('utmsourcecookie'));
                $jQ(utmtermFieldID).attr("value",$jQ.cookie('utmtermcookie'));
                $jQ(utmcontentFieldID).attr("value",$jQ.cookie('utmcontentcookie'));
                $jQ(utmmediumFieldID).attr("value",$jQ.cookie('utmmediumcookie'));
                
              
                
              });
              
              </script>
              • Re: Capturing UTM Data With Cookies Through JavaScript, need help editing code
                Updated Code but still not working:

                <script src="http://code.jquery.com/jquery-latest.min.js" charset="utf-8" type="text/javascript"></script>
                <script src="/sites/all/themes/tekzennew/js/jquery.cookie.js" type="text/javascript"></script>
                <script src="/sites/all/themes/tekzennew/js/jQueryString-2.0.2-Min.js" type="text/javascript"></script>

                <script type="text/javascript">
                var $jQ = jQuery.noConflict();

                $jQ(document).ready(function(){

                   //
                   //--- CHANGE THESE!!! ---
                   //

                    // Change this to match domains of referrers you want to ignore.
                    // You'll want to ignore referrers from your own domains.
                    // Use only the base domain name without subdomains (ex. "company.com")
                    // Separate multiple domains with commas (leave the brackets).
                  var excludedReferrers = [ "tekscan.com" ];

                    // Change this to match the base domain of your company's landing pages.
                    // Cookies will be created with this domain.
                    // Ex. If your landing page domain is "pages.yourcompany.com" then use
                    //     "yourcompany.com"
                  var cookieDomain = "tekscan.com";

                    // The URL parameter that has your pay-per-click info.
                    // Typically "kw" or "keyword" (depends on how you set up your PPC URLs)
                  var payPerClickParameter = "utm_term";
                 
                  //IDs for the fields to be updated.
                  var utmcampaignFieldID = "#utm_campaign__c"
                  var utmsourceFieldID = "#utm_source__c"
                  var utmtermFieldID = "#utm_term__c"
                  var utmcontentFieldID = "#utm_content__c"
                  var utmmediumFieldID = "#utm_medium__c"

                   //
                   //-- you probably shouldn't change anything after this --
                   //

                  var refer = document.referrer;
                  var searchString;
                  var searchEngine;

                      // if there's no referrer, do nothing
                  if ( (refer == undefined) || (refer == "") ) { ; }
                  else {
                     // get the domain of the referring website -- http://[[this-thing.com]]/
                    var referrerDomain =
                      refer.substr(refer.indexOf("\/\/") + 2,
                        refer.indexOf("\/",8) - refer.indexOf("\/\/") - 2).toLowerCase();

                    var excludedDomainFound = false;
                    var i = 0;

                      // search the excluded domain list to see if the referrer domain is on it
                    while ( (i < excludedReferrers.length) && !excludedDomainFound) {
                      var thisExcludedDomain = excludedReferrers[i].toLowerCase();

                        // weird semantics here -- indexOf returns "-1" if the search string isnt found.
                        // thus excludedDomainFound is true only when indexOf matches an excluded domain (!= -1)
                      excludedDomainFound = (referrerDomain.indexOf(thisExcludedDomain) != -1);
                      i++;
                    }

                     // only if the referrer isn't in our excluded domain list...
                    if( !excludedDomainFound ) {
                        // extract the URL parameters from common search engines
                        // To add your own, each engine needs:
                        //  name: how the search engine will appear on your Marketo leads
                        //  url: REGEX for matching the engine's referrer.  ex.  /\.google\./i
                        //  query: URL parameter that contains the search query - usually "p" or "q"

                      var searchEngines = [
                       { name: "Yahoo", url: /\.yahoo\.co/i, query: "p" },
                       { name: "Google", url: /\.google\./i, query: "q" },
                       { name: "Microsoft Live", url: /\.live\.com/i, query: "q" },
                       { name: "MSN Search", url: /search\.msn\./i, query: "q" },
                       { name: "AOL", url: /\.aol\./i, query: "query" },
                       { name: "Bing", url: /\.bing\.com/i, query: "q" },
                       { name: "Ask", url: /\.ask\.com/i, query: "q" }
                      ];

                        // find the referring search engine (if any)
                      i = 0;
                      while (i < searchEngines.length) {
                        if (refer.match(searchEngines[i].url)) {
                          searchEngine = searchEngines[i].name;
                          searchString = $jQ.getQueryString({ ID: searchEngines[i].query,
                            URL: refer, DefaultValue: "" });
                          break;
                        }
                        i++;
                      }
                         // If no search engine is found, this person probably used a less
                         // popular one.  Use the referring doman, then guess the query parameter
                      if (i == searchEngines.length) {

                         searchEngine = referrerDomain;

                         var queries = ["q","p","query"];
                         var i = 0;
                         while ((i < queries.length) && (searchString == undefined)) {
                           searchString = $jQ.getQueryString({ ID: queries[i], URL: refer });
                           i++;
                         }

                           // no search strings found -- use this text instead.
                         if (searchString == undefined) {
                           searchString = "None";
                         }
                      }

                         // Use the provided URL parameter to get the PPC keyword.
                      var payPerClickWord = $jQ.getQueryString({ID: payPerClickParameter,
                        URL: refer, DefaultValue: "" });
                        
                        // Store the URL parameter value in the variable "ucampaign"
                    var ucampaign = $jQ.getQueryString({ID: utm_campaign, URL: refer, DefaultValue: "" });
                    var usource = $jQ.getQueryString({ID: utm_source, URL: refer, DefaultValue: "" });
                    var uterm = $jQ.getQueryString({ID: utm_term, URL: refer, DefaultValue: "" });
                    var ucontent = $jQ.getQueryString({ID: utm_content, URL: refer, DefaultValue: "" });            
                    var umedium = $jQ.getQueryString({ID: utm_medium, URL: refer, DefaultValue: "" });

                         // Put the info into cookies.  These values will be extracted
                         // and put into a Marketo form later.  Expires in 2 years.
                      $jQ.cookie('mktoPPCKeyword', payPerClickWord,
                         {expires: 730, path: '\/', domain: cookieDomain});
                      $jQ.cookie('mktoSearchEngine', searchEngine,
                         {expires: 730, path: '\/', domain: cookieDomain});
                      $jQ.cookie('mktoSearchString', searchString,
                         {expires: 730, path: '\/', domain: cookieDomain});
                        
                     // Create a cookie named "utmcampaigncookie" and store the value from the variable "ucampaign"
                    $jQ.cookie('ucampaign', utmcampaigncookie,
                         {expires: 730, path: '\/', domain: cookieDomain});
                    $jQ.cookie('usource', utmsourcecookie,
                         {expires: 730, path: '\/', domain: cookieDomain});
                    $jQ.cookie('uterm', utmtermcookie,
                         {expires: 730, path: '\/', domain: cookieDomain});
                    $jQ.cookie('ucontent', utmcontentcookie,
                         {expires: 730, path: '\/', domain: cookieDomain});
                    $jQ.cookie('umedium', utmmediumcookie,
                         {expires: 730, path: '\/', domain: cookieDomain});
                        
                        
                        
                        
                    }
                  }
                 
                    // Get the values from the cookies and put them into the hidden fields
                  $jQ(searchStringField).attr("value",$jQ.cookie('mktoSearchString'));
                  $jQ(searchEngineField).attr("value",$jQ.cookie('mktoSearchEngine'));
                  $jQ(payPerClickKeywordField).attr("value",$jQ.cookie('mktoPPCKeyword'));
                 
                  // Get the values from the cookies and put them into the hidden field by referencing the field ID
                  $jQ(utm_campaign__c).attr("value",$jQ.cookie('utmcampaigncookie'));
                  $jQ(utm_source__c).attr("value",$jQ.cookie('utmsourcecookie'));
                  $jQ(utm_term__c).attr("value",$jQ.cookie('utmtermcookie'));
                  $jQ(utm_content__c).attr("value",$jQ.cookie('utmcontentcookie'));
                  $jQ(utm_medium__c).attr("value",$jQ.cookie('utmmediumcookie'));
                 

                 
                });

                </script>
                • Re: Capturing UTM Data With Cookies Through JavaScript, need help editing code
                  Are you loading this on a Marketo landing page? You have relative paths to your JS files, are they loading?

                  <script src="http://code.jquery.com/jquery-latest.min.js" charset="utf-8" type="text/javascript"></script>
                  <script src="/sites/all/themes/tekzennew/js/jquery.cookie.js" type="text/javascript"></script>
                  <script src="/sites/all/themes/tekzennew/js/jQueryString-2.0.2-Min.js" type="text/javascript"></script>

                  Update these to absolute paths and see if that works? 
                  • Re: Capturing UTM Data With Cookies Through JavaScript, need help editing code
                    Hi Jason,

                    Thanks for the advice, I changed the paths and noticed that I don't actually have the jQueryString-2.0.2-Min.js file on our server, do you know a place I can download this file or a path to this file like I have with the jQuery-latest.min.JS file

                    Thanks,

                    Doug

                    • Re: Capturing UTM Data With Cookies Through JavaScript, need help editing code
                      Dan Reed
                      Hi Doug,

                      What is the subdomain for your Marketo instance? It would be something like "info.tekscan.com" or "offer.tekscan.com."
                      • Re: Capturing UTM Data With Cookies Through JavaScript, need help editing code
                        Google found this one: http://discover.certain.com/js/public/jQueryString-2.0.2-Min.js
                        • Re: Capturing UTM Data With Cookies Through JavaScript, need help editing code
                          Jason, thanks I actually just found that one as well using Google.....unfortunately it is still not working with this update.

                          Dan, do you mean where do the landing pages live? If so, they are on pages.tekscan.com we are using Marketo forms on non-Marketo pages.

                          An example of a live page in beta is http://www.tekscan.com/bite-test-test and I have pasted the updated JavaScript file below:

                          <script src="http://www.tekscan.com/sites/all/themes/tekzennew/js/jquery.cookie.js" type="text/javascript"></script>
                               <script src="http://discover.certain.com/js/public/jQueryString-2.0.2-Min.js" type="text/javascript"></script>
                               <script src="http://code.jquery.com/jquery-latest.min.js" charset="utf-8" type="text/javascript"></script>
                               
                               
                               <script type="text/javascript">
                               var $jQ = jQuery.noConflict();
                               
                               $jQ(document).ready(function(){
                               
                                  //
                                  //--- CHANGE THESE!!! ---
                                  //
                               
                                   // Change this to match domains of referrers you want to ignore.
                                   // You'll want to ignore referrers from your own domains.
                                   // Use only the base domain name without subdomains (ex. "company.com")
                                   // Separate multiple domains with commas (leave the brackets).
                                 var excludedReferrers = [ "tekscan.com" ];
                               
                                   // Change this to match the base domain of your company's landing pages.
                                   // Cookies will be created with this domain.
                                   // Ex. If your landing page domain is "pages.yourcompany.com" then use
                                   //     "yourcompany.com"
                                 var cookieDomain = "tekscan.com";
                               
                                   // The URL parameter that has your pay-per-click info.
                                   // Typically "kw" or "keyword" (depends on how you set up your PPC URLs)
                                 var payPerClickParameter = "utm_term";
                                
                                 //IDs for the fields to be updated.
                                 var utmcampaignFieldID = "#utm_campaign__c"
                                 var utmsourceFieldID = "#utm_source__c"
                                 var utmtermFieldID = "#utm_term__c"
                                 var utmcontentFieldID = "#utm_content__c"
                                 var utmmediumFieldID = "#utm_medium__c"
                               
                                  //
                                  //-- you probably shouldn't change anything after this --
                                  //
                               
                                 var refer = document.referrer;
                                 var searchString;
                                 var searchEngine;
                               
                                     // if there's no referrer, do nothing
                                 if ( (refer == undefined) || (refer == "") ) { ; }
                                 else {
                                    // get the domain of the referring website -- http://[[this-thing.com]]/
                                   var referrerDomain =
                                     refer.substr(refer.indexOf("\/\/") + 2,
                                       refer.indexOf("\/",8) - refer.indexOf("\/\/") - 2).toLowerCase();
                               
                                   var excludedDomainFound = false;
                                   var i = 0;
                               
                                     // search the excluded domain list to see if the referrer domain is on it
                                   while ( (i < excludedReferrers.length) && !excludedDomainFound) {
                                     var thisExcludedDomain = excludedReferrers[i].toLowerCase();
                               
                                       // weird semantics here -- indexOf returns "-1" if the search string isnt found.
                                       // thus excludedDomainFound is true only when indexOf matches an excluded domain (!= -1)
                                     excludedDomainFound = (referrerDomain.indexOf(thisExcludedDomain) != -1);
                                     i++;
                                   }
                               
                                    // only if the referrer isn't in our excluded domain list...
                                   if( !excludedDomainFound ) {
                                       // extract the URL parameters from common search engines
                                       // To add your own, each engine needs:
                                       //  name: how the search engine will appear on your Marketo leads
                                       //  url: REGEX for matching the engine's referrer.  ex.  /\.google\./i
                                       //  query: URL parameter that contains the search query - usually "p" or "q"
                               
                                     var searchEngines = [
                                      { name: "Yahoo", url: /\.yahoo\.co/i, query: "p" },
                                      { name: "Google", url: /\.google\./i, query: "q" },
                                      { name: "Microsoft Live", url: /\.live\.com/i, query: "q" },
                                      { name: "MSN Search", url: /search\.msn\./i, query: "q" },
                                      { name: "AOL", url: /\.aol\./i, query: "query" },
                                      { name: "Bing", url: /\.bing\.com/i, query: "q" },
                                      { name: "Ask", url: /\.ask\.com/i, query: "q" }
                                     ];
                               
                                       // find the referring search engine (if any)
                                     i = 0;
                                     while (i < searchEngines.length) {
                                       if (refer.match(searchEngines[i].url)) {
                                         searchEngine = searchEngines[i].name;
                                         searchString = $jQ.getQueryString({ ID: searchEngines[i].query,
                                           URL: refer, DefaultValue: "" });
                                         break;
                                       }
                                       i++;
                                     }
                                        // If no search engine is found, this person probably used a less
                                        // popular one.  Use the referring doman, then guess the query parameter
                                     if (i == searchEngines.length) {
                               
                                        searchEngine = referrerDomain;
                               
                                        var queries = ["q","p","query"];
                                        var i = 0;
                                        while ((i < queries.length) && (searchString == undefined)) {
                                          searchString = $jQ.getQueryString({ ID: queries[i], URL: refer });
                                          i++;
                                        }
                               
                                          // no search strings found -- use this text instead.
                                        if (searchString == undefined) {
                                          searchString = "None";
                                        }
                                     }
                               
                                        // Use the provided URL parameter to get the PPC keyword.
                                     var payPerClickWord = $jQ.getQueryString({ID: payPerClickParameter,
                                       URL: refer, DefaultValue: "" });
                                       
                                       // Store the URL parameter value in the variable "ucampaign"
                                   var ucampaign = $jQ.getQueryString({ID: utm_campaign, URL: refer, DefaultValue: "" });
                                   var usource = $jQ.getQueryString({ID: utm_source, URL: refer, DefaultValue: "" });
                                   var uterm = $jQ.getQueryString({ID: utm_term, URL: refer, DefaultValue: "" });
                                   var ucontent = $jQ.getQueryString({ID: utm_content, URL: refer, DefaultValue: "" });            
                                   var umedium = $jQ.getQueryString({ID: utm_medium, URL: refer, DefaultValue: "" });
                               
                                        // Put the info into cookies.  These values will be extracted
                                        // and put into a Marketo form later.  Expires in 2 years.
                                     $jQ.cookie('mktoPPCKeyword', payPerClickWord,
                                        {expires: 730, path: '\/', domain: cookieDomain});
                                     $jQ.cookie('mktoSearchEngine', searchEngine,
                                        {expires: 730, path: '\/', domain: cookieDomain});
                                     $jQ.cookie('mktoSearchString', searchString,
                                        {expires: 730, path: '\/', domain: cookieDomain});
                                       
                                    // Create a cookie named "utmcampaigncookie" and store the value from the variable "ucampaign"
                                   $jQ.cookie('ucampaign', utmcampaigncookie,
                                        {expires: 730, path: '\/', domain: cookieDomain});
                                   $jQ.cookie('usource', utmsourcecookie,
                                        {expires: 730, path: '\/', domain: cookieDomain});
                                   $jQ.cookie('uterm', utmtermcookie,
                                        {expires: 730, path: '\/', domain: cookieDomain});
                                   $jQ.cookie('ucontent', utmcontentcookie,
                                        {expires: 730, path: '\/', domain: cookieDomain});
                                   $jQ.cookie('umedium', utmmediumcookie,
                                        {expires: 730, path: '\/', domain: cookieDomain});
                                       
                                       
                                       
                                       
                                   }
                                 }
                                
                                   // Get the values from the cookies and put them into the hidden fields
                                 $jQ(searchStringField).attr("value",$jQ.cookie('mktoSearchString'));
                                 $jQ(searchEngineField).attr("value",$jQ.cookie('mktoSearchEngine'));
                                 $jQ(payPerClickKeywordField).attr("value",$jQ.cookie('mktoPPCKeyword'));
                                
                                 // Get the values from the cookies and put them into the hidden field by referencing the field ID
                                 $jQ(utm_campaign__c).attr("value",$jQ.cookie('utmcampaigncookie'));
                                 $jQ(utm_source__c).attr("value",$jQ.cookie('utmsourcecookie'));
                                 $jQ(utm_term__c).attr("value",$jQ.cookie('utmtermcookie'));
                                 $jQ(utm_content__c).attr("value",$jQ.cookie('utmcontentcookie'));
                                 $jQ(utm_medium__c).attr("value",$jQ.cookie('utmmediumcookie'));
                                
                               
                                
                               });
                               
                               </script>
                          • Re: Capturing UTM Data With Cookies Through JavaScript, need help editing code
                            Dan Reed

                                 Hi Doug,

                                 Not sure where you got this code: script src="/sites/all/themes/tekzennew/js/jQueryString-2.0.2-Min.js"

                                 The correct absolute path is: http://pages.tekscan.com/js/public/jQueryString-2.0.2-Min.js

                                 Same goes for the other js file references:
                            http://pages.tekscan.com/js/public/jquery.cookie.js
                                 http://pages.tekscan.com/js/public/jquery-latest.min.js


                                 I would not store a copy of these files on a separate server. It will work, but if Marketo ever decides to update the javascript, the updates won't be carried over. On any of the relative paths in Marketo's original code example (http://community.marketo.com/MarketoArticle?id=kA050000000Kyxt), just put "http://pages.tekscan.com" before the "/js.." and those paths should work.

                                 That fixes the path issue. Make those updates and then test it again.

                            • Re: Capturing UTM Data With Cookies Through JavaScript, need help editing code
                              Hi Dan,

                              Thanks for the suggestions!

                              Unfortunately the page is still not working after making those changes. I do not know if this matters or not but we are currently using Drupal 5 as our CMS and this has created issues with other aspects of the Marketo integration. For example, form validation was not working because for some reason the last span tag was being removed (I think because there was no actual content between the tags) so I ended up having to add an extra span tag with a space for every required field in order for the form validation to work properly.

                              <span class="mktFormMsg"> </span>

                              Do you think something like this could be creating issues? Have you ever heard of something like this?

                              Here is the latest and greatest version of my code:

                              <script src="http://pages.tekscan.com/js/public/jquery.cookie.js" type="text/javascript"></script>
                                   <script src="http://pages.tekscan.com/js/public/jQueryString-2.0.2-Min.js" type="text/javascript"></script>
                                   <script src="http://pages.tekscan.com/js/public/jquery-latest.min.js" charset="utf-8" type="text/javascript"></script>
                                   
                                   
                                   <script type="text/javascript">
                                   var $jQ = jQuery.noConflict();
                                   
                                   $jQ(document).ready(function(){
                                   
                                      //
                                      //--- CHANGE THESE!!! ---
                                      //
                                   
                                       // Change this to match domains of referrers you want to ignore.
                                       // You'll want to ignore referrers from your own domains.
                                       // Use only the base domain name without subdomains (ex. "company.com")
                                       // Separate multiple domains with commas (leave the brackets).
                                     var excludedReferrers = [ "tekscan.com" ];
                                   
                                       // Change this to match the base domain of your company's landing pages.
                                       // Cookies will be created with this domain.
                                       // Ex. If your landing page domain is "pages.yourcompany.com" then use
                                       //     "yourcompany.com"
                                     var cookieDomain = "tekscan.com";
                                   
                                       // The URL parameter that has your pay-per-click info.
                                       // Typically "kw" or "keyword" (depends on how you set up your PPC URLs)
                                     var payPerClickParameter = "utm_term";
                                    
                                     //IDs for the fields to be updated.
                                     var utmcampaignFieldID = "#utm_campaign__c"
                                     var utmsourceFieldID = "#utm_source__c"
                                     var utmtermFieldID = "#utm_term__c"
                                     var utmcontentFieldID = "#utm_content__c"
                                     var utmmediumFieldID = "#utm_medium__c"
                                   
                                      //
                                      //-- you probably shouldn't change anything after this --
                                      //
                                   
                                     var refer = document.referrer;
                                     var searchString;
                                     var searchEngine;
                                   
                                         // if there's no referrer, do nothing
                                     if ( (refer == undefined) || (refer == "") ) { ; }
                                     else {
                                        // get the domain of the referring website -- http://[[this-thing.com]]/
                                       var referrerDomain =
                                         refer.substr(refer.indexOf("\/\/") + 2,
                                           refer.indexOf("\/",8) - refer.indexOf("\/\/") - 2).toLowerCase();
                                   
                                       var excludedDomainFound = false;
                                       var i = 0;
                                   
                                         // search the excluded domain list to see if the referrer domain is on it
                                       while ( (i < excludedReferrers.length) && !excludedDomainFound) {
                                         var thisExcludedDomain = excludedReferrers[i].toLowerCase();
                                   
                                           // weird semantics here -- indexOf returns "-1" if the search string isnt found.
                                           // thus excludedDomainFound is true only when indexOf matches an excluded domain (!= -1)
                                         excludedDomainFound = (referrerDomain.indexOf(thisExcludedDomain) != -1);
                                         i++;
                                       }
                                   
                                        // only if the referrer isn't in our excluded domain list...
                                       if( !excludedDomainFound ) {
                                           // extract the URL parameters from common search engines
                                           // To add your own, each engine needs:
                                           //  name: how the search engine will appear on your Marketo leads
                                           //  url: REGEX for matching the engine's referrer.  ex.  /\.google\./i
                                           //  query: URL parameter that contains the search query - usually "p" or "q"
                                   
                                         var searchEngines = [
                                          { name: "Yahoo", url: /\.yahoo\.co/i, query: "p" },
                                          { name: "Google", url: /\.google\./i, query: "q" },
                                          { name: "Microsoft Live", url: /\.live\.com/i, query: "q" },
                                          { name: "MSN Search", url: /search\.msn\./i, query: "q" },
                                          { name: "AOL", url: /\.aol\./i, query: "query" },
                                          { name: "Bing", url: /\.bing\.com/i, query: "q" },
                                          { name: "Ask", url: /\.ask\.com/i, query: "q" }
                                         ];
                                   
                                           // find the referring search engine (if any)
                                         i = 0;
                                         while (i < searchEngines.length) {
                                           if (refer.match(searchEngines[i].url)) {
                                             searchEngine = searchEngines[i].name;
                                             searchString = $jQ.getQueryString({ ID: searchEngines[i].query,
                                               URL: refer, DefaultValue: "" });
                                             break;
                                           }
                                           i++;
                                         }
                                            // If no search engine is found, this person probably used a less
                                            // popular one.  Use the referring doman, then guess the query parameter
                                         if (i == searchEngines.length) {
                                   
                                            searchEngine = referrerDomain;
                                   
                                            var queries = ["q","p","query"];
                                            var i = 0;
                                            while ((i < queries.length) && (searchString == undefined)) {
                                              searchString = $jQ.getQueryString({ ID: queries[i], URL: refer });
                                              i++;
                                            }
                                   
                                              // no search strings found -- use this text instead.
                                            if (searchString == undefined) {
                                              searchString = "None";
                                            }
                                         }
                                   
                                            // Use the provided URL parameter to get the PPC keyword.
                                         var payPerClickWord = $jQ.getQueryString({ID: payPerClickParameter,
                                           URL: refer, DefaultValue: "" });
                                           
                                           // Store the URL parameter value in the variable "ucampaign"
                                       var ucampaign = $jQ.getQueryString({ID: utm_campaign, URL: refer, DefaultValue: "" });
                                       var usource = $jQ.getQueryString({ID: utm_source, URL: refer, DefaultValue: "" });
                                       var uterm = $jQ.getQueryString({ID: utm_term, URL: refer, DefaultValue: "" });
                                       var ucontent = $jQ.getQueryString({ID: utm_content, URL: refer, DefaultValue: "" });            
                                       var umedium = $jQ.getQueryString({ID: utm_medium, URL: refer, DefaultValue: "" });
                                   
                                            // Put the info into cookies.  These values will be extracted
                                            // and put into a Marketo form later.  Expires in 2 years.
                                         $jQ.cookie('mktoPPCKeyword', payPerClickWord,
                                            {expires: 730, path: '\/', domain: cookieDomain});
                                         $jQ.cookie('mktoSearchEngine', searchEngine,
                                            {expires: 730, path: '\/', domain: cookieDomain});
                                         $jQ.cookie('mktoSearchString', searchString,
                                            {expires: 730, path: '\/', domain: cookieDomain});
                                           
                                        // Create a cookie named "utmcampaigncookie" and store the value from the variable "ucampaign"
                                       $jQ.cookie('ucampaign', utmcampaigncookie,
                                            {expires: 730, path: '\/', domain: cookieDomain});
                                       $jQ.cookie('usource', utmsourcecookie,
                                            {expires: 730, path: '\/', domain: cookieDomain});
                                       $jQ.cookie('uterm', utmtermcookie,
                                            {expires: 730, path: '\/', domain: cookieDomain});
                                       $jQ.cookie('ucontent', utmcontentcookie,
                                            {expires: 730, path: '\/', domain: cookieDomain});
                                       $jQ.cookie('umedium', utmmediumcookie,
                                            {expires: 730, path: '\/', domain: cookieDomain});
                                           
                                           
                                           
                                           
                                       }
                                     }
                                    
                                       // Get the values from the cookies and put them into the hidden fields
                                     $jQ(searchStringField).attr("value",$jQ.cookie('mktoSearchString'));
                                     $jQ(searchEngineField).attr("value",$jQ.cookie('mktoSearchEngine'));
                                     $jQ(payPerClickKeywordField).attr("value",$jQ.cookie('mktoPPCKeyword'));
                                    
                                     // Get the values from the cookies and put them into the hidden field by referencing the field ID
                                     $jQ(utm_campaign__c).attr("value",$jQ.cookie('utmcampaigncookie'));
                                     $jQ(utm_source__c).attr("value",$jQ.cookie('utmsourcecookie'));
                                     $jQ(utm_term__c).attr("value",$jQ.cookie('utmtermcookie'));
                                     $jQ(utm_content__c).attr("value",$jQ.cookie('utmcontentcookie'));
                                     $jQ(utm_medium__c).attr("value",$jQ.cookie('utmmediumcookie'));
                                    
                                   
                                    
                                   });
                                   
                                   </script>

                                   Thanks again for all of the help!!!!
                              • Re: Capturing UTM Data With Cookies Through JavaScript, need help editing code
                                Dan Reed

                                     Hi Doug,

                                     To properly troubleshoot custom javascript would require more time and access. I've implemented this solution in several systems successfully, so I don't see why it wouldn't work in yours, but to know for sure is hard to determine by posting here.

                                     My company is a Marketo Preferred Services Partner, so we'd be glad to help you fix this issue, along with any others you might have in your Marketo instance. Sorry my explanation focused on the 4 steps wasnt a quick fix! And sorry for the sales pitch... Id like to help but dont think I can do it through this community thread.

                                     Feel free to email me!

                                     - Dan Reed, Director of Marketing Automation at Spear Marketing Group

                                     dan@spearmarketing.com