Google Analytics - Integration for Marketo

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce tellus odio, dapibus id fermentum quis, suscipit id erat. Mauris metus. Maecenas aliquet accumsan leo. Fusce tellus. Duis pulvinar. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Duis pulvinar.

If you use a marketing automation software (like Marketo or ClickDimensions), you'll likely be collecting user-level data (ie: pageviews, email activity, etc) in that system. One huge benefit to these systems is that they can consolidate anonymous visitors when they're identified. So, if someone fills out a form or logs into your website, you can track that. If they change browsers and identify themselves again, you can combine those records into a single contact record and let Google know who they are - so Google Analytics can also consolidate users.

If you don't have a budget or need for a marketing automation platform but still want to track and consolidate users by "User ID" in Google Analytics, you could totally create a custom solution or purchase a specific solution for this purpose.

In any case, my Marketo integration has two parts to maximize consolidation of anonymous visitors...

Part 1: Processing URL parameters.

Whenever we send an email from Marketo, we tag all the links in that email with the user's Marketo Lead ID (side note: Marketo refers to people and leads as the same thing - and they're transitioning to calling everything "people", but the API docs and macros may refer to people as leads). To do this, we just add "mkto_id={{}}" to all links in our email templates. When a user adds a new link that didn't exist in the template, they have to manually tag the link with UTM parameters and the mkto_id. Note: In the image below, we also have a parameter for email address. We use this for a different system. It is against Google Analytics terms of service to store personally identifiable information in Google Analytics.

Next - when a user lands on our page with a tagged URL, we process this value into a custom dimension and set the User ID. This example code uses Universal Analytics (aka analytics.js). It's possible to do it using gtag implementation or natively in Google Tag Manager as well. Here's the Javascript code to do it... There are two parts - getParameterByName and the Google Analytics call. The getParameterByName function just gets a URL parameter value by the name. SO, in this case, our parameter is called "mkto_id". If that value is there, then the Google Analytics code will be processed with this ID. In my case, the Marketo Person ID dimension is dimension5, but it might be different for you.
// Part 1 - Param Function
function getParameterByName(name, url) {
  if (!url) url = window.location.href;
  name = name.replace(/[\[\]]/g, "\\$&");
  var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
      results = regex.exec(url);
  if (!results) return null;
  if (!results[2]) return '';
  return decodeURIComponent(results[2].replace(/\+/g, " "));

// Part 2 - Google Analytics
if(getParameterByName("mkto_id") != null) {
  ga('create', 'UA-712548192-1', 'auto', {
  userId: getParameterByName("mkto_id")
  ga('set', 'dimension5', getParameterByName("mkto_id"));
else {
  ga('create', 'UA-712548192-1', 'auto');

Part 2: Marketo Webhooks

In the previous section, we identified users who we are sending emails so that when they click to a landing page, we immediately know who they are on our website.

Next, we will track anonymous visitors who identify themselves. One annoying thing about Marketo is that their "LeadID" value isn't available on the front end of the website via their Munchkin tracking. Other marketing automation systems can be different and save you some greif. For example, ClickDimensions has their primary user ID available in their cookie. So, you have to get a little creative with Marketo - and I'm honestly disappointed this integration isn't provided out of the box - it's fairly straightforward.

We use Marketo as the central repository for de-anonymizing users. For us, users are mostly de-anonymized via forms. Any form where a user fills out their email will be sent to Marketo so that we have as few anonymous visitors as possible. This includes gated content (where a user has to fill in their email to download a document or view a page) and logins (where a user logs into one of our systems). These two use cases done very differently though.

We use Marketo forms on all of our websites for non-authentication forms. So, individuals will fill out a form to "contact us" or download gated content. In these forms, we have to do a few things:

1.) Create a custom, string field for Google Analytics ClientID on the lead in Marketo. In my case, I call it "FormGoogleAnalyticsClientID" - which organizes it with other form-specific values.

2.) Add this field to all of your forms as a hidden field.

3.) On all your form pages, you'll get the Google Analytics Client ID from Google Analytics, set the value into a custom dimension in Google Analytics, and then set the form value. (Again, this is UA/analytics.js implementation). Also, you'll have to run the form field update part after the Marketo Form has run.

var mktoGAClientID = "";
ga(function (tracker) {
  mktoGAClientID = tracker.get('clientId');
  ga('set', 'dimension1', mktoGAClientID);

var GAID_MField = $("#formGoogleAnalyticsClientID");
if (GAID_MField[0] == null) {
  GAID_MField = $('input[name=formGoogleAnalyticsClientID]');
if (GAID_MField != null & mktoGAClientID != null) { GAID_MField.val(mktoGAClientID); }

4.) Whenever someone fills out a form, you send a webhook call to Google Analytics, saying an event occured for the person with GA Client ID and User ID. So - first, you set up a webhook to send to Google Analytics. You can see, it consumes the Google Analytics Client ID. Here's the Webhook URL:{{lead.FormGoogleAnalyticsClientID}}&t=event&ec=Data%20Sync&ea=Marketo&el=Marketo%20Form&cd5={{lead.Id}}&dp=%2Fmarketo-lead-update&uid={{lead.Id}}&
You can test out by sending hits to this webhook and viewing the events in the "Real Time" section of Google Analytics.

Next, you create a campaign to send the webhook. The basic logic is: whenever someone fills out a form, and they have a google analytics client ID (ie: not empty), you call the webhook.

... And just like that, you're now integrated!
Back To Blog

Google Maps