For the following systems, we have special instruction for saving GA Client ID to Salesforce:
If you don’t use either of those, this guide is for you:
Overview
In order to send offline sales stats from Salesforce to Google Analytics, we need to know which lead/account/opportunity/booking belongs to which website visitor in Google Analytics.
This can be achieved by saving the ID of each website visitor to Salesforce field zuevsoftware__gaconnector_Google_Analytics_Client_ID__c during the form submission.
This ID (Google Analytics Client ID, or GA CID) should be saved to Salesforce Lead object, alongside the normal form submission data like phone, name or email address.
This ID normally looks like two long numbers separated by a dot, for example: 1233979415.1478058607
There are ways we can retrieve CID:
- Either using JavaScript, by calling analytics.js methods.
- Or on the server-side, by simply retrieving the cookie that contains CID.
Method #1 – Retrieving CID from JavaScript
Here is the code snippet that can be used to retrieve client ID:
1 |
var clientId = ga.getAll()[0].get('clientId'); |
This code should be placed after Google Analytics code, as it won’t work unless GA tracker is already initialized.
Method #2 – Retrieving CID on the server-side
Client ID is stored in browser cookies, which is why it can also be retrieved on the server-side.
The name of the cookie that contains CID is _ga:
The only problem is that _ga cookie contains some extra information except for GA CID itself.
Here is a sample value that is stored in _ga cookie: GA1.2.1233979415.1478058607
1233979415.1478058607 is the actual CID, while GA1.2. is just prefix that contains versioning number and cookie domain level.
We need to remove the prefix and only send the second half of the CID to Salesforce.
Here is the code example (in PHP) for retrieving CID on the sever-side:
1 2 3 |
<?php $cid = preg_replace('/GA[0-9]+\.[0-9]+\./', '', $_COOKIE['_ga']); ?> |
The JavaScript method is more preferable, as:
You should not directly access the cookie analytics.js sets, as the cookie format might change in the future (from analytics.js documentation).
But not to worry, the server-side cookie method has been working for a long long time and will probably keep working for a long-long time. gaconnector.com website relies on this method too.