The Sticky Cookie

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

The Sticky Cookie: How to Integrate Google

Analytics Source Data with Marketo


Today’s buyers conduct most of their research online, making it critical that you’re able to
understand how people are finding your website and how your content is performing. Without
these insights, it’s a bit like throwing a dart with blindfolds on. To be successful in this
buyer-empowered world, you need to have full visibility into your customer's’ engagement with
your website so you can identify what’s working well and what isn’t.

Google Analytics is an invaluable tool for gaining these types of insights. Every time an
anonymous visitor goes to your website, Google Analytics automatically detects the specific
source that led them there—whether that be Google search, Facebook, or a referral from an
online industry publication. These sources are also automatically grouped into broader
marketing channels, called Mediums, which often include organic, cpc, social, referral, email,
and direct.

In order to associate traffic with paid campaigns, Google allows you to add parameters to a URL
that identify important reporting data about your campaign like the site where the ad appeared,
the campaign name, ad creative, and the search term that was targeted. These are often
referred to as “UTM tags”, and typically look something like this:

http://yourdomain.com/?utm_source=adsite&utm_campaign=adcampaign&utm_term=adkeywor
d

How Marketo Captures This Data


Marketo, a popular marketing automation platform, allows you to create forms that will
automatically capture ​UTM parameter values​, store them in hidden fields, and then submit them
to a CRM like Salesforce. When implemented properly, this integration can allow you to close
the loop from visit to lead to customer, and determine the true ROI of your ad campaign.

Unfortunately, this only works if the tagged URL is also the page where the form is placed. If the
conversion funnel requires that the user navigate to multiple URLs, the values of the UTM tags
that were used on the landing page will not automatically persist throughout the visit.

It also doesn’t account for the vast majority of traffic to your site coming from non-paid channels
like organic search, referrals, and direct visits.
This is where an integration with Google Analytics becomes critical. Since Google Analytics is
already capturing this data, it should be pretty straightforward to take that data and pass it over
to Marketo and Salesforce, right? Wrong. Unfortunately it’s not so easy.

The Challenge: Google Analytics no longer stores this data in a


local cookie
In the classic version of Google Analytics, traffic acquisition data like source, medium, campaign
content, and keyword were stored in a local cookie called _utmz, set by the classic ga.js
tracking code.

Do a quick Google search and you’ll find tons of scripts that were designed to extract the values
from the cookie and pass them into hidden form fields. For a while, this solved the problem.

Unfortunately these scripts no longer work because with the new version of Google Analytics,
called Universal Analytics, all tracking happens at the server-side and the cookie set by the new
analytics.js contains no information about the user's traffic sources.

Technically, the classic Google Analytics tracking script still works, so you could simply place
the old version on your site until Google no longer supports it.

Since this isn’t a good permanent solution, we chose to go with the script e-nor created, which
mimics the classic Google Analytics cookie and can be found ​here​. With the tracking script
problem resolved, we can now focus our efforts on extracting that data from the cookie and
passing them to hidden fields in a Marketo form.

The Solution: Make UTM Values Sticky, And Then Store Them In
Hidden Marketo Fields

If you follow the steps below you’ll be able to capture a lead’s referring campaign, source,
medium, ad content, and keyword and carry those values all the way through the sales funnel,
from initial form submission to won opportunity. To do this, we’ll start at the end and work
backwards. Note: We are limiting the scope of some of these tags just to run on certain sites so
we don’t change everyone’s Salesforce and Marketo reports at once, but eventually we will roll it
out to all Northeastern sites and won’t need to limit the scope anymore.

1. Create Custom Fields in Salesforce


You’ll need to create at least 5 custom fields in your CRM where the traffic source data will be
stored. If using Salesforce, you may want to create these on both your Lead and Contact
objects so the values can carry over when a lead is converted to a contact.
● GA_Medium__c_account: This will store the medium set by Google Analytics
● GA_Source__c_account
● GA_Campaign__c_account
● GA_Term__c_account
● GA_Content__c_account

2. Add the Same Custom Fields to Marketo Forms


Add the same fields created in the previous step to all forms you wish to integrate with Google
Analytics. Since these should not be visible to the user, be sure to mark them as hidden.

3. Deploy the Clone Classic Google Analytics Cookie


Download a copy of e-nor’s ​javascript​ to mimic the Classic Google Analytics tracking script
cookie behavior. This will be customized in the next step. You can find ​Northeastern’s copy
here​. If you’re adding tracking to a subdomain or subdirectory of northeastern.edu then you
don’t need to update the code. You only need to update it if you’re tracking a separate domain
name (step 4).

4. Modify The Javascript to Work With Your Domain


Open the script you just downloaded in your favorite editor, and modify the line that looks like:
var domains = ["mydomain1.com", "mydomain2.com"]; Change mydomain to any top-level
domains that this script will be deployed on. This allows you to implement cross-domain
browsing so session data will persist across multiple top-level domains.

5. Download the Marketo Form Listener


Many thanks to Keith W. Shaw for this ​awesome script​ that allows you to easily detect Marketo
forms in Google Tag Manager. You’ll need this for the next few steps to work.

6. Deploy Scripts Through Google Tag Manager


Deploy both the Cloned Classic Google Analytics Cookie script and the Marketo Form Listener
script through Google Tag Manager on any page of your website you wish to include with this
solution. In most cases this should be all of the same pages you’re tracking with Google
Analytics. Marketo Form Listener is already deployed across all our pages.

7. Download and Deploy Our Extraction Script


This is the magic bullet that extracts the traffic source data stored in our fake Google Analytics
cookie, and then passes those values to hidden Marketo forms, allowing us to pass that data
along with the lead throughout their journey in our CRM. You can find the ​Northeastern copy
here​.

<script>
/*
* To populate mkto form fields from cookie
*/
function fillAllFormVals(){
var z = _uGC(document.cookie, "__utmz=", ";"),
source = _uGC(z, "utmcsr=", "|"),
medium = _uGC(z, "utmcmd=", "|"),
term = _uGC(z, "utmctr=", "|"),
content = _uGC(z, "utmcct=", "|"),
campaign = _uGC(z, "utmccn=", "|"),
gclid = _uGC(z, "utmgclid=", "|"),
allForms = MktoForms2.allForms();

if (gclid !="-") {
source = "google";
medium = "cpc";
}

var passedVals = {
"GA_Medium__c_account":medium,
"GA_Source__c_account":source,
"GA_Campaign__c_account":campaign,
"GA_Term__c_account":term,
"GA_Content__c_account":content,
"LeadSource":medium,
"Lead_Source_Detail__pc":source
}

for (i = 0; i < allForms.length; i++){


var form = allForms[i];
form.setValues(passedVals);
}
}

function _uGC(l,n,s){
if (!l || l=="" || !n || n=="" || !s || s=="") return "-";

var i,i2,i3,c="-";
i=l.indexOf(n);
i3=n.indexOf("=")+1;

if (i > -1) {
i2=l.indexOf(s,i); if (i2 < 0){ i2=l.length; }
c=l.substring((i+i3),i2);
}
return c;
}

(function(){
fillAllFormVals();
}());
</script>

If you chose to use different labels for your custom fields, you’ll want to modify the field labels
referenced in the script to match your own.

Since Marketo forms aren’t populated until after the page has fully loaded, you’ll want to
leverage a custom event triggered by the Marketo Form Listener to trigger this tag. For your
firing trigger, create a Custom Event called mkto.form.ready.

8. Run Some Tests


As a rule of thumb, you should always test any code deployments locally in your browser using
Google Tag Manager’s Preview and Debug feature. While in debug mode, you can verify that
the values are being set by exploring the source code or using an extension like ​Hidden Form
Finder​.

If you’ve implemented everything correctly and you’re using the Hidden Form Finder extension,
you should see hidden field values that look something like this:

9. Publish and Monitor


Now that you have a working solution that you’ve tested and validated, it’s time to unleash it into
the wild. Be sure to go back and check the results of your data after a few days or weeks to be
sure that you’re getting the results you expected, and then revise accordingly.
Enjoy Closed Loop Reporting!
With this solution you’ll be able to analyze the effectiveness of your marketing efforts like never
before. Good luck and be sure to share your results, comments, questions and suggestions for
improvements in the comments.

You might also like