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

You Might Also Like

No Comments

    Leave a Reply