Monthly Archives

August 2019

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

Tracking & Analytics

Salesforce Integration With Google Analytics - Step by Step

Salesforce is probably the most popular cloud based CRM in the market but unfortunately it is lacking the ability to integrate well with web tracking systems like Google analytics (at least the free version) to provide a full view of the user journey from A-Z (web lead to sale).

On the other hand Google Analytics (the most popular website traffic software) gets very close to provide the full user journey but as Google Analytics is not a CRM it is missing conversion tracking data (mainly leads tracking), a lead could be tracked easily in GA by tracking the lead  form thank you page but once this lead is pushed to a third party CRM like Salesforce GA can not get any information when and if that lead is closed. Google Analytics 360 has the ability to integrate with Saelsforce and closing the loop on conversion tracking but unfortunately GA 360 cost is around $150,000/year

In this step by step tutorial I will show you have you can integrate GA (the free version) with Salesforce and get the conversion tracking data, before start doing that I want you to understand my working environment and to be aware that it may not work 100% for you but with some modifications you can get this solution to work with any environment, :

  • The CMS I am testing this work on is WordPress, it is PHP based so all my codes will be in PHP
  • I am using Google Tag Manager to implement some JavaScript codes
  • I am using Salesforce' PHP simple implementation to interact with the Salesforce REST API from PHP
  • I am using the 30 days free Salesforce trial

Stage 1 - push Google Analytics CID to Salesforce:

Client ID (CID) is a unique user identifier created by Google Analytics and stored in a browser's cookie for two years, the number could be seen in GA's user explorer and also could be found in the browsers cookie

CID

All subsequent activities by the same user using the same browser will be listed under that CID and treated as one user's activities

GA-user-explorer

Step 1

Before even trying to push CID to Salesforce we need to create a new lead field in Salesforce by going to Salesforce > Set up >  Object Manager > Lead > Fields & Relationships > New

salesforce-new-field

Keep the field name CID__c handy as we will need it when we communicate with Salesforce using the API

Step 2:

Now we have the CID field ready in Salesforce, filling that field could be done in different ways, in my case I will be using the Web-to-Lead form which you can access through Salesforce > Setup > Web-to-lead > Create Web-to-Lead Form  and generate a source code that you can use on your website

web-to-lead

The form will be used in the contact us or lead generation page, before placing the form make sure you make the CID field hidden from users as it will be filled automatically using JavaScript. See an example of the Webto-Lead form with the CID field hidden:

Step 3:

Now we need to inject the CID value inside the hidden field, this could be done by reading the GA cookie via JavaScript (not recommended by Google) or by using native Google Analytics calls. I will be using native Google Analytics calls and include them using Google Tag Manger, the tag below will be able to find the CID and inject it in the hidden CID field (make sure to set up the right firing sequence as below)

GTM-tag-CID

The code above is compatible with GA Universal, for GA4 you can use the code below:

<script type="text/javascript">
function get_ga_clientid() {
  var cookie = {};
  document.cookie.split(';').forEach(function(el) {
    var splitCookie = el.split('=');
    var key = splitCookie[0].trim();
    var value = splitCookie[1];
    cookie[key] = value;
  });
return cookie["_ga"].substring(6);
}
document.getElementById('gacid').value= get_ga_clientid();
</script>

By now the form is fully communicating with Salesforce and passing the CID, you can make a test and you will see the CID showing in Salesfoce's dashboard

Stage 2 - push a pageview back Google Analytics from Salesforce with the same CID once the lead is closed:

For this stage we need to communicate with Salesforce using their API, we need to build a system that can check the leads status every 5-10 minutes and if any new lead is becoming closed we need to push a pageview back to Google analytics using the measurement protocol

Step 4:

Create an APP in Salesforce to enable the API connection Salesforce > Set up >  > Apps > App Manager > New Connected APP >> Save >> Continue

salesforce-App-CID

Save the consumer key and consumer secret from the next screen to use them in the API SDK

oauth

Edit the App permissions Salesforce > Set up >  > Apps > App Manager > Manage Connected APP >> Edit >> Save

OAuth-policies

You do not have to use exactly those settings, I just tried to keep the policies very lose to make building my first App easier

Step 5:

Download the developer SDK for your preferred programming language, in my case I will be coding in PHP so I can use the PHP developer SDK for Salesforce or  the simple implementation example here which I ended up using just to keep things simple for this tutorial. When using the simple implementation example here (or any other framework ) make sure to rename the call back file to match the call back URL in step 4 and edit the config.php file and add the credentials you got from step 4:

client_id

Visit the index.html file which will redirect you to login to your Salesforce account and after that you will be brought back automatically to the file demo_rest.php now you will have 15 minutes to test anything you want, once you are happy with the results you need to use the refresh token which you can get in the callback file using this code:

$refresh_token = $response['refresh_token'];

Step 6:

After establishing a successful connection using the API you need to write a call that list all or part of the leads:

list-leads

The output of the file will be as below:

leads-output

As you can see we have now a list of the leads with their CID and their status, what we will be processing in the next step are leads that contain CID and the are Closed - Converted

Step 7:

Create a new goal in Google Analytics with a destination URL like this /thank-you-page.html  or any other URL of your choice that is not used by any other goal, create also a page with no content for that URL.

Use Google Analytics Hit Builder to build a call that add a pageview for /thank-you-page.html

hit-builder

You can click send a hit to Google analytics to test the URL but eventually this URL must be part of your script (a cronjob) that will:

  • Scan Salesforce's leads using the API every few minutes or every hour
  • Find closed leads
  • Push a pageview hit to Google Analytics using the lead CID
  • Store that lead in an internal database that will be checked in the future to avoid saving duplicate leads

If the lead with right CID is pushed correctly to Google Analytics as a pageview, you should be able to see that pageview and a registered goal using the users' explorer

salesforce-GA

You can see how the medium organic search was inherited automatically using the existing CID without including that in the hit builder URL, at this point we have closed the loop on finding actual conversions that happen in Salesforce but missing in GA, the user flow could be checked in GA uninterrupted without any additional actions by GA users.

Good luck implementing that solution, please comment below if you a have a question