Tracking & Analytics

How to Pass Google Analytics Traffic Source Dimensions To Salesforce

Would not be great if marketers can see Google Analytics traffic source dimensions in Salesforce? Unfortunately Salesforce by default doesn't provide any attributions for web leads pushed to Salesforce, I am not sure why considering that is not difficult to do that for a lead that is pushed using web-to-lead form where a simple additional code can push the lead source dimensions like source/medium and more other dimensions that marketers badly need to calculate ROI. This guide will provide a step-by-step method explaining how to push the lead source dimensions mainly  source, medium and keyword (if available) to Salesforce, I will be using the same terminology used by Google Analytics to keep things consistent for marketers.

Step 1:

We need first to create few extra fields in Salesforce to save the lead source dimensions, in sales force go Salesforce > Set up >  Object Manager > Lead > Fields & Relationships > New > Text

new-field

We need to repeat the process above few times depending on the number of diminutions we are willing to push to Salesforcee, in my case I have created three dimensions Medium, Source and Term (keyword).

fields

It is recommended to choose a long text area field for the Source as some URLs can get really long .

Step 2:

Now we have the fields ready in Salesforce, we need to create a Web-to-Lead form which you can access through Salesforce > Setup > Web-to-lead > Create Web-to-Lead Form   (make sure to add Source, Medium and Term to the form).

web-to-lead-1

Generate the form then hide the Source, Medium and Term fields, you need also to clean up the extra text related to those 3 fields.

form1

Step 3:

We need to save the Source, Medium and Term for every website visitor in a cookie, you can do that using UTMZ-replicator  just add it to your website using Google Tag Manger as a custom HTML tag and let it fire on every pageview.

GTM-UTMZ

Once this script is added you will be able to see Source, Medium and Term saved in a cookie called __utmzz

utmz-cookie

Step 4:

Source, Medium and Term are saved in a cookie now it is time to take their values and populate them to the corresponding hidden fields in Salesforce's web-to- lead form, to do that you just need to create a tag in Google Tag Manger (custom HTML tag) and insert the code below (the cookie reading part of this code is taken from Stackoverflow with some modifications)

<script type="text/javascript">
var ga_source = '';
var ga_campaign = '';
var ga_medium = '';
var ga_term = '';
var ga_content = '';
var gc = '';
var c_name = "__utmzz";
if (document.cookie.length>0){
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1){
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
gc = unescape(document.cookie.substring(c_start,c_end));
}
}
if(gc != ""){
var y = gc.split('|');
for(i=0; i<y.length; i++){
if(y[i].indexOf('utmcsr=') >= 0) ga_source = y[i].substring(y[i].indexOf('=')+1);
if(y[i].indexOf('utmccn=') >= 0) ga_campaign = y[i].substring(y[i].indexOf('=')+1);
if(y[i].indexOf('utmcmd=') >= 0) ga_medium = y[i].substring(y[i].indexOf('=')+1);
if(y[i].indexOf('utmctr=') >= 0) ga_term = y[i].substring(y[i].indexOf('=')+1);
if(y[i].indexOf('utmcct=') >= 0) ga_content = y[i].substring(y[i].indexOf('=')+1);
}
}
document.getElementById('00N3i00000BG1jK').value= ga_source;
document.getElementById('00N3i00000BG1hp').value= ga_medium;
document.getElementById('00N3i00000BG1lf').value= ga_term;
</script>

You can see that the code above includes also campaign which I did add to Salesforce but you can easily add it following the process provided early in this guide. After publishing the code above and submitting a form lead you should be able to see the lead source dimensions (Source, Medium and Keyword) in Salesforce lead page.

utm-salesforce

Please feel free to comment below if you have any questions

You Might Also Like

1 Comment

  • Reply
    Matthew
    December 12, 2023 at 5:41 pm

    Great post - thanks!

Leave a Reply