Integrate Google Adwords & Google Analytics with Salesforce Guide

salesforce_google_analytics_integration__ga_visitor_id_in_sf_lead_detail

How do you integrate Google Adwords and Salesforce?
How do you integrate Google Analytics and Salesforce?

The simple answer to any data integration question: you need a unique identifier that is shared across both systems. This post will give you detailed step by step instructions on how to set and capture the unique identifier to integrate Salesforce with Google Adwords and Google Analytics.

Google Adwords and Salesforce Integration

Here is an example scenario to help illustrate the value of integrating Google Adwords and Salesforce. Let’s say you are a software company that sources new leads from Google Adwords. Most of your sales happen offline in sales calls and meetings. How do you close the loop between a lead originated from Google Adwords that ultimately becomes an offline sale? In Salesforce, you need to capture the GCLID which is the unique identifier that Google passes on every Adwords click in a query parameter in your website destination URL.

www.ryanpraski.com/?gclid=Zj0DEQjw-ZOZZBRD

Capture Adwords GCLID In Salesforce

To capture the GCLID you need to first save the GCLID in a cookie on your website. Then when your prospect ultimately fills out and submits your lead form, you pull the GCLID from a cookie and pass it via an hidden form field to Salesforce along with the rest of the lead form information.

Salesforce Admin Setup

1) Login to Salesforce.com and click on Setup in the top navigation.

2) In the left navigation under the Build menu click on Customize > Leads > Fields.

3) Scroll down to the Lead Custom Fields & Relationships section of the page and click on New as shown in the screenshot below:

salesforce_adwords_integration_add_lead_field     

4) For Step 1. Click Text data type radio button then click next.

5) For Step 2. Enter the details type GCLID in the Field Label box. Then in the length field enter 255. The field name should be automatically populated with GCLID. Then click next.

6) For Step 3. Establish field-level security click the Read-Only checkbox on the header. We want GCLID to be read only for every type of profile so no one can accidentally overwrite the GCLID value in Salesforce. Then click next.

7) For Step 4. Add to page layouts make sure you are adding the field to your Lead Layout and click Save.

8) Now repeat the same process and add the GCLID as a custom field within Opportunities. In the left navigation under the Build menu click on Customize > Opportunities > Fields. Scroll down to the Opportunity Custom Fields & Relationships and click on New.

9) Repeat #4-7 above to add the GCLID as a custom field to Opportunities.

Now add another custom field called Adwords_Action that will be populated with the value “add” for all leads and opportunities that include an Adwords GCLID. This is used to make it easier to create the Salesforce report for conversions that include a GCLID later on. You’ll eventually feed this report back into Adwords as “offline conversions”.

10) In the left navigation under the Build menu click on Customize > Opportunities > Fields. Scroll down to the Opportunity Custom Fields & Relationships and click on New.

11) For Step 1. Click the Formula data type radio button then click next.

12) For Step 2. Enter the details type Adword_Action in the Field Label box. Then click the Text Formula Return Type radio button. Then click next.

13) For Step 3. Enter formula in the Adwords_Add (Text) = type in “add” as is shown in the screenshot below.

salesforce_adwords_integration_adwords_action_formula_field

14) For Step 4 click Next and For Step 5 click save.

15) Now repeat the same process and add the Adwords_Add as a custom field within Opportunities. In the left navigation under the Build menu click on Customize > Opportunities > Fields. Scroll down to the Opportunity Custom Fields & Relationships and click on New.

16) Repeat #11-14 above to add the Adwords_Add as a custom field to Opportunities.

Now we are going to use the Salesforce admin to map the GCLID Lead field to the GCLID Opportunity field so the GCLID value is passed through when the lead moves to an opportunity.

17) In the left navigation under the Build menu click on Customize > Leads > Fields then scroll down to the Lead Custom Fields & Relationships section and click on the Map Lead Fields button as shown in the screenshot below.

salesforce_adwords_integration_map_lead_field

18) Make sure the drop down next to the GCLID is Opportunity.GCLID as shown in the screenshot below and click Save.

salesforce_adwords_integration_map_lead_field_to_opportunity

Salesforce Web to Lead Form

To create your custom web-to-lead form that will capture your lead information and your GCLID and pass it to Salesforce. In the left navigation under the Build menu click on Customize > Leads > Web-to-Lead. Then click the Create Web-to-Lead Form button.

Select the fields you’d like included in your lead form. Make sure to select GCLID. In the example screenshot below I choose to keep it really simple with First Name and GCLID as selected fields.

salesforce_adwords_integration_web_to_lead_form_admin_gclid

Copy the web-to-lead form html code and make sure to save it. The important values you’ll need are your oid value and your GCLID id and name from your web-to-lead form code.

<input type=hidden name=”oid” value=”yyyyy“>

GCLID:<input  id=”xxxxx” maxlength=”255″ name=”xxxxx” size=”20″ type=”text” />

Customize the Web-to-Lead Form Code to Pass GCLID

To simplify the process of customizing the web-to-lead form to pass the GCLID, I’ve provided full sample page code below. To use this code you’ll have to replace the yyyyy oid value on line 17 with your oid value and the xxxxx GCLID id and name on line 22 with your value. And also replace the xxxxx on line 12 with your GCLID id value.

Here is a summary of what is happening and the corresponding lines in the code

A visitor clicks on an Google Adwords ad link and comes to your site with a unique click id (gclid) included in the destination URL. Lines 38-44 captures the value of the gclid query parameter and sets a 90 day cookie named gclid (line 42).

The function that sets the cookie is on lines 27-33 and the function that pulls the value of the query parameter out of the URL is on lines 34-37.

If the same visitor comes back on the same computer or mobile device using the same web browser without deleting their cookies within 90 days and submits your web-to-lead form the gclid stored in the cookie is passed. Line 22 is the hidden form field that passes the gclid. The script on lines 10-15 that gets the value stored in the gclid cookie and passes it to the gclid hidden form value input field. This is why it is essential that the element id on line 12 matches the gclid element id on line 22. Recall that this id is specific to your Salesforce account and comes from the code generated when you created your web-to-lead form in Salesforce.

Salesforce Web-to-Lead Adwords Integration

Now let’s test that your Salesforce web-to-lead form is properly capturing the gclid. On your page with your customized web-to-lead form add a test glcid query parameter value to the destination URL. For example: www.ryanpraski.com/?gclid=ryanpraskitest. To be sure that the cookie was set properly reload your page without your gclid parameter present in the URL. Add your first name to the form field and submit your web to lead form.

To check to see that the blank form field gclid value is passed you can use your web browser developer tools. Go to your developer tools in your web browser and click on Network > All and find servlet.WebToLead?encoding=UTF-8 (you may have to refresh the browser for data to show up). The screenshot below shows my sample form submission which is a POST request. In the right pane click on header and scroll down to the Form Data. Here you should see the names and values passed on your form field submission. The oid value yyyyy should have your Salesforce oid value and the gclid form field name xxxxx should have your Salesforce GCLID field name. If the test gclid Google Adwords click id has been passed successfully to Salesforce you will see the value of the gclid query parameter you set.

salesforce_adwords_integration_debug_form_fields

Then you can login to your Salesforce account and run a lead report for the past day. Find the lead you’ve just submitted in the report and click into it. In the lead detail section you should see the GCLID field populated with your test gclid query parameter value. See the screenshot below. 

salesforce_adwords_integration_gclid_in_sf_lead

Offline Sales/Conversion Data in Google Adwords

To close the loop from Google Adwords traffic source to offline sale you’ll need to export your closed sales data from Salesforce. If necessary you can use a data export tool like the Salesforce Apex data Loader to pull large amounts of Salesforce data. Make sure to only export sales where the GCLID is present. You’ll then import the Salesforce sales data into Adwords as Offline Conversions. Recall that the key that joins the Salesforce data back to the Google Adwords data is GCLID. For more information on this process see Step 4 in this Google Support article or contact me with your questions.

Google Analytics Salesforce Integration

Just like the Google Adwords to Salesforce integration described above, the Google Analytics to Salesforce integration requires a common key that is shared across both systems. In this case the common is the Google Analytics visitor identifier called the client id.

Follow the Salesforce admin steps from above to create a new field in both lead and opportunities called “GA Visitor ID”.

You will also need to create a new user scope Google Analytics custom dimension to capture GA Visitor ID in Google Analytics. Click on Admin in Google Analytics then within your Property under Custom Definitions click on Custom Dimensions. Add a New Custom Dimension called GA Visitor ID and set the scope to User as shown in the screenshot below.

Google_Analytics_Admin_GA_Visitor_ID

Now that you have a custom dimension setup you’ll need to update your Google Analytics tracking code to pass the GA Visitor ID on all page loads. In my example I set the GA Visitor ID (clientId) to dimension 100. See the code snippet below.

ga('create', 'UA-XXXXX-Y', 'auto');
ga(function(tracker) {
  var clientId = tracker.get('clientId');
  tracker.set('dimension100',clientId);
});
ga('send', 'pageview');

Now go back into Salesforce to finish the process. Create your web-to-lead form with the GA Visitor ID field included. Make sure to grab the GA Visitor ID field id from your web-to-lead code. Use the sample page level code below to test that the Google Analytics visitor id (client id) is being passed on the lead form submission. Note that the code below includes both Adwords gclid and Google Analytics visitor id (client id) integration. Make sure to substitute your Salesforce GA Visitor ID field id in line 30 and line 42 for zzzzz in the code.

The code below is a full sample with both the Salesforce web-to-lead customized to capture both GA Visitor ID and Google Adwords GCLID with sample Google Analytics tracking code.

Salesforce Web-to-Lead Google Analytics Integration

Now let’s test to make sure the Google Analytics to Salesforce integration is working. First use a debugger like the Google Analytics Tag Assistant or Charles Proxy to check that your custom dimension for GA Visitor ID is being passed to Google Analytics. For me I set custom dimension 100 so in the debugger it looks like this: cd100:1015969174.1466108841

Next let’s submit the web-to-lead form test to make sure the GA Visitor ID is being sent in a hidden field to Salesforce. The screenshot below shows my sample form submission. In the right pane click on header and scroll down to the Form Data. Here you should see the names and values passed on your form field submission. The oid value yyyyy should have your Salesforce oid value and the GA Visitor Id form field name zzzzz should have your Salesforce GA Visitor Id field name. If the test GA Visitor ID has been passed successfully to Salesforce you will see the value of the Google Analytics Visitor that is also passed to your Google Analytics Custom Dimension.

salesforce_google_analytics_integration_debug_form_fields

Now jump back into Salesforce and run a leads report. Find the test lead you submitted. You should now see the GA Visitor ID Lead field populated with the Visitor ID and it should match what you saw in your web analytics debugger.

salesforce_google_analytics_integration__ga_visitor_id_in_sf_lead

Import Salesforce Data into Google Analytics or Import GA data into Salesforce

You can now use this Google Analytics Visitor ID as your key to import custom visitor data into Google Analytics or import Google Analytics data into Salesforce. For more information on how to import user data into Google Analytics check out the Analytics help center article or contact me.