How to integrate Salesforce with Google Analytics?


To enable Salesforce integration with Google Analytics, you must have the Edit Permissions first. In this article, we have given the process with code samples for your reference. The topics covered in this post include –

  • User identification based on requirements
  • Check on Salesforce cloud objects and fields
  • Learn how to create new custom fields in Salesforce?
  • The Configuration of Lead or Opportunity objects
  • Lead Form Editing
  • Salesforce milestone configuration to import
  • Set up the product or attribute data (optional)
  • Testing the Setup
  • Define or set the notices or limits

How to identify the users?

There are two options for user identification in analytics either Client ID or User ID. To facilitate the data import in Salesforce cloud, you should implement the Client ID first and you had the option to implement User ID too.

How to identify the users?

With Client ID, you could identify the browser instance anonymously. This is the cookie-based, non-authenticated identifier generated by the analytics enabled site automatically when the user visits a site for the first time. Hence, a Client ID is created and this is the most suitable technique for business to retain the existing customers and getting new leads.

If any business has a high rate of logged-in users then it needs a USER ID that will analyze a group of the session with a unique identifiable ID. If you wanted to set up the user ID then it needs extra work and code on your site.

  • First of all, you should activate the USER ID feature into Analytics Property.
  • Now add the user ID to your lead form page and more pages you wanted to track. This is possible to achieve with Google Tag Manager or JS variable.

Check on required Salesforce cloud objects and fields

Check on required Salesforce cloud objects and fields

To set up the Salesforce integration with Google Analytics, you must have the full access to the given Salesforce objects and fields. If any user has administrative access then he could access these objects and fields quickly. To allow the access, you need to define the permission set properly for the user who is interested in connecting Salesforce or analytics accounts together. A user with limited access is not allowed to work with all fields or objects as given above.

Learn how to create new custom fields in Salesforce?

To store the analytics tracking code in Salesforce, let us create the three fields first. The name for the fields could be given as below.

Field Name API Name Field Length


Rules to follow for custom fields creation –

  • First, you should create the fields before deciding on the tracking method.
  • Give the field names in Uppercase and label could be anything that you want.
  • Assign the read-only property to protect against unwanted changes.
  • The fields must be created for both LEAD and OPPORTUNITY

The Configuration of Lead or Opportunity objects

For the Lead object, you should enable the History Tracking Field. In the case of the Opportunity object, enable the History Tracking feature for the Stage field. This will make sure that any changes to these fields will lead a HIT event.

Related Post:  Call Center Integration with Salesforce Lightning Experience

Map the fields in one object to another one as given below –

  • GAUSERID to Opportunity.GAUSERID
  • GATRACKID to Opportunity.GATRACKID

Lead Form Editing

To capture or store the needed tracking information, we will modify the lead form in this step. For this purpose, you should know about your analytics tracking ID. You must be wondering why we need tracking ID here?

With the help of tracking ID, you can always distinguish how to import the data from multiple lead forms from different sites tracked with multiple analytical accounts, this can be made possible by user/client ID only.

In the first step, let us learn how to add the custom fields in the lead form using JS programming language. Here is code sample for your reference –

<form action=”” name=”myForm”>

Phone: <input type=”text” name=”phone_number”>

<input type=”hidden” id=”JTCLIENTID” name=”JTCLIENTID” value=””>

<input type=”hidden” id=”JTUSERID” name=”JTUSERID” value=””>

<input type=”hidden” id=”JTTRACKID” name=”JTTRACKID” value=”UA-XXXXX-YY”>

<input type=”submit”>


With this sample code, you can add both user id and client id to your lead form. In case, you wanted to add client ID only then you could remove the JTUSERID option here. To set the ID statistically, replace the “UA-XXXXX-YY” with your Track ID. Here, in the next step, let us learn how to set the ID dynamically.

<script type=”text/javascript”>

ga(function() {

var tracker = ga.getAll()[0];

var trackingId = tracker.get(‘trackingId’);



Once you have added the custom fields and track ID, let us see how to set the custom tracking field values? You just need to add the code given below to populate the user ID or the client ID.

<script type=”text/javascript”>


‘submit’, function(event) {

ga(function() {

var tracker = ga.getAll()[0];

var clientId = tracker.get(‘clientId’);

document.getElementById(‘GACLIENTID’).value = clientId;

var userId = tracker.get(‘userId’);

document.getElementById(‘GAUSERID’).value = userId;




Integrate Google Analytics with your Salesforce account

Here, you will add the google analytics with your Salesforce cloud account by allowing the authorized access to the account. For this purpose, you will need to create a Salesforce Sales Cloud data set. Here are the steps to follow –

  • Firstly, sign in to your Google Analytics account.
  • Now from the admin option in the bottom check the check the Properties. You must have the Edit Permissions for this property.
  • Now check the Data import option from the Property and click on Create.
  • Now set the data type under the CRM data and select the Salesforce.
  • Read or accept the terms for analytics data import.
  • Now give a name to your data and Click on Continue option. Now check the list of all successfully created Salesforce credentials. Is any of the credentials are in use currently then remove them before you use.

How to authorize the Salesforce Sales Cloud?

  • Now under the Salesforce credential option in the data source details panel, select the create new authorization option.
  • Don’t forget to give a name to credential before their actual use.
  • Now check access for
  • Now login to your Salesforce cloud account. Once you have logged in to your account successfully, you have successfully connected analytics with your Salesforce account.
Related Post:  How to Integrate Salesforce and Office 365?

If you wanted to import data from multiple accounts then repeat the same process by creating multiple Salesforce credentials.

How to track the Fields setup?

With this option, you can set up or map the configured Salesforce fields to corresponding Analytics Track IDs. If you wanted to use custom fields here then make sure that they are configured well across both Lead and Opportunity objects. Once tracking fields are configured well, click on Continue in the end.

Salesforce milestone configuration to import

Here, you will select the Salesforce Lead or Opportunity milestones to import. Practically, you will realize how often milestones are imported. From the data source details panel, select the milestones that you wanted to import. And Click on continue in the end.

Set up the product or attribute data (optional)

You can also import user attributes or product data too from your Salesforce account. Although this is optional but recommended for most of the integrations. It helps you to derive more audiences and generating new leads with the proper analytics.

Testing the Setup

In this step, you will check or test the set up by importing some data to your Salesforce account and view the same in the analytics report. Follow the same steps as discussed earlier for the data import, now locate the data sets, manage uploads, and fetch the data. In this way, you can upload data to your Salesforce account and once the upload is successful it will show the status as completed. If the upload is not complete then it will show a Failure message.

Imported data may take 26 hours before it is available in the reporting. Further, you should check the imported user attributes and product data too. Once you are sure on all parameters, the integration is successful and it can be used or your project now.

Define the limits or notes

Makes sure that the integration process is limited to the Privacy Policy and the Analytics Terms of Services. Don’t import any personal details. If you wanted to use the multi-currency option then you should configure your Salesforce account accordingly otherwise it can be utilized for single currency only. Empty fields are removed from the measurement protocol once the data is imported successfully.

The size of data should not exceed more than 8192 bytes. Don’t use commas in the imported because they are taken as separators. Further, the limit for enhanced e-commerce is set to maximum 200 unique product indexes.

With this discussion, you have a complete idea of Salesforce integration with google analytics in a few simple steps. Just follow the instructions as given and you would be able to import data from analytics to Salesforce account whenever needed.

Write a Comment

Notify of

Download & Learn from Our Whitepapers

Stop, read and acquire deep insights into complex issues