Integrating Google Analytics With a CRM

October 29, 2007 by Justin Cutroni

20071025-gears.jpgA lot of people ask about integrating Google Analytics data with other types of data. Most people are interested in some type of CRM integration so that’s what I’m going to write about.

Let me start by saying that there is no API to Google Analytics. There is no formal way to pull data out and integrate it into some other application. Sure, there are some hacks out there to manipulate the report URLs but, in my opinion, this is a pretty big hack and I don’t recommend it to our clients.

When we talk about integrating Google Analytics with a CRM we’re talking about pulling information about the visitor’s originating source and sending it to a CRM system. We’re not going to pull the visitor’s entire history, just where they came from and attach it to other information that they enter into a form.

Why do this?

Happy Customers!Pulling a visitor’s source information is very helpful to a sales team. Image if a sales rep could identify how a sales lead found the website before picking up the phone and contacting them? Remember, a visitor’s source info describes how the visitor found the site. Did they respond to a specific email with a certain offer? Or did they come from search and, if so, what was the keyword they used? This type of info can help a sales team understand the intent of the prospect but where in the buying process they are. That makes life for the team a little easier.

So how do we connect a visitor’s source to a visitor?

Conceptual Overview

Google Analytics stores a visitor’s source data in a cookie. That cookie is named __utmz. The data can be extracted from the cookie and added to a lead generation form. When the visitor submits the form the source information is connected to the other information that the visitor entered into the form (usually her name and other contact information).

If the contact form is integrated with a CRM application, like SalesForce.com or NetSuite, it may be possible to store the marketing information with the individual’s contact information. Direct CRM integration depends on your CRM system. Some systems allow form fields to be pulled directly into the application. Check with your CRM provider for information about your specific system.

I would like to note that you don’t need to use some fancy CRM to take advantage of this technique. You can use the technique below even if your lead form generates an email or dumps the data into a database. The key is that we’re leveraging the data in the Google Analytics cookies and connecting it with information that the visitor sends you.

Detailed Instructions

Ok, so how do you actually do this? It takes some coding, either client side coding (JavaScript) or server side code (PHP, ColdFusion, .NET, Java, etc.). My example uses JavaScript. Here’s a basic explanation of what the code needs to do:

1. Extract the visitor’s source data from the __utmz cookie
2. Manipulate data as needed
3. Place data in hidden form fields

When the visitor submits the form the source data in the hidden form fields will be sent back to the server where it can be used.

The sample code below extracts the source information from the cookie and places it in some hidden form fields. Then, when the form is submitted the information is passed to the server.


<html> 
<head> 

<script src="http://www.google-analytics.com/urchin.js"></script> 

<script> 
_uact="XXXXXX-X"; 
urchinTracker(); 
</script> 

<script> 
// 
// Get the __utmz cookie value. This is the cookies that 
// stores all campaign information. 
// 
var z = _uGC(document.cookie, '__utmz=', ';'); 
// 
// The cookie has a number of name-value pairs. 
// Each identifies an aspect of the campaign. 
// 
// utmcsr  = campaign source 
// utmcmd  = campaign medium 
// utmctr  = campaign term (keyword) 
// utmcct  = campaign content (used for A/B testing) 
// utmccn  = campaign name 
// utmgclid = unique identifier used when AdWords auto tagging is enabled 
// 
// This is very basic code. It separates the campaign-tracking cookie 
// and populates a variable with each piece of campaign info. 
// 
var source  = _uGC(z, 'utmcsr=', '|'); 
var medium  = _uGC(z, 'utmcmd=', '|'); 
var term    = _uGC(z, 'utmctr=', '|'); 
var content = _uGC(z, 'utmcct=', '|'); 
var campaign = _uGC(z, 'utmccn=', '|'); 
var gclid   = _uGC(z, 'utmgclid=', '|'); 
// 
// The gclid is ONLY present when auto tagging has been enabled. 
// All other variables, except the term variable, will be '(not set)'. 
// Because the gclid is only present for Google AdWords we can 
// populate some other variables that would normally 
// be left blank. 
// 
if (gclid !="-") { 
      source = 'google'; 
      medium = 'cpc'; 
} 
// Data from the custom segmentation cookie can also be passed 
// back to your server via a hidden form field 
var csegment = _uGC(document.cookie, '__utmv=', ';'); 
if (csegment != '-') { 
      var csegmentex = /[1-9]*?\.(.*)/;
      csegment    = csegment.match(csegmentex); 
      csegment    = csegment[1]; 
} else { 
      csegment = ''; 
} 
function populateHiddenFields(f) { 
      f.source.value  = source; 
      f.medium.value  = medium; 
      f.term.value    = term; 
      f.content.value = content; 
      f.campaign.value = campaign; 
      f.segment.value = csegment; 
      return true; 
} 
</script> 
</head> 
<body> 
<form name='contactform' 
onSubmit="javascript:populateHiddenFields(this);"> 
<input type='hidden' name='source' /> 
<input type='hidden' name='medium' /> 
<input type='hidden' name='term' /> 
<input type='hidden' name='content' /> 
<input type='hidden' name='campaign' /> 
<input type='hidden' name='segment' /> 
</form> 
</body> 
</html> 

Now, if this form is directly connected to a CRM then the data in the hidden form fields will go directly into the CRM along with other form info. That’s the magic. Again, you can use this method to collect source information even if the form is not connected to a CRM. Any type of lead generation form will be more valuable if you use this technique.

How The Code Works

When the above page loads in the browser the JavaScript starts to execute and extracts data from the cookies. First, it extracts the value of the __utmz cookie and stores it in a variable named z. Then it parses the z variable and looks for information about the visitor’s source. The __utmz cookie has a number of name-value pairs separated by a pipe (’|') character. Each name=value pair holds a different attribute of the visitor’s source. Here’s an example of the __utmz cookie.

12454562.1193706926.14.5.utmcsr=google|utmccn=(organic)|
utmcmd=organic|utmctr=google%2Banalytics%2Bshortcut

20071029-z.pngYou can see that the name value pairs look very similar to the parameters we use for link tagging. Just by looking at the above cookie you can figure out that the visitor performed an organic search on Google for the term ‘google%2Banalytics%2Bshortcut’ or ‘google analytics shortcut’. That’s the type of information that we want to put in hidden form fields and send back to the server. [You can learn more about the __utmz cookie in the reference section below.]

Getting back to the code, we were talking about how the code extracts the information from the z variable. It uses a function named _uGC(), which is found in the urchin.js JavaScript, to do all of the work. __uGC() extracts the value part of all the name-value pairs in the cookie. We call _uGC() for each name-value pair that exists in the cookie. It parses the cookie and pulls out the information that we want. [If you want to know more about _uGC() please see the reference section at the end of this post.]

Once the information is out of the z variable the populateHiddenFileds() functions puts the data in a series of hidden form fields. Then, when the form is submitted, the data is sent to the server.

You’ll notice a few things about the above code. I’ve added some logic to deal with AdWords auto-tagging. Auto tagging populates part of the cookie with a value named gclid. This variable hides some of the info that we need, like source and medium. The logic in the above code populates data that would otherwise be missing. I’ve also added some code that extracts the custom segment value which is stored in the __utmv cookie. I thought it would be useful to send this info back to the server as well.

Conclusion

Pulling visitor source data and connecting it with a visitor is very valuable. While your implementation will almost certainly be different, the concept illustrated above is the foundation for all implementations. Regardless of your implementation the business use for this data is fantastic.

Good luck!

20071029-reference.png

Reference

About the _uGC() Function

_uGC() takes three arguments:

_uGC(string, start-string, end-string)

• A string to search (target string)
• A start string
• An end string

The function will return the string between start string and end string. If the start string is not found then the function will return a dash (-).

About The _utmz Cookie

The __utmz cookie is the referral-tracking cookie. It tracks all referral information regardless of the referral medium or source. This means that all organic, CPC, campaign, or plain referral information is stored in the __utmz cookie. By default the cookie expires in six months, but that can be customized by changing the tracking code.

Cookie Format:


domain-hash.ctime.nsessions.nresponses.utmcsr= X(|utmccn=X|utmctr=X|utmcmd=X|utmci

Data about the referrer is stored in a number of name-value pairs, one for each attribute of the referral:

utmcsr
Identifies a search engine, newsletter name, or other source specified in the
utm_source query parameter See the “Marketing Campaign Tracking”
section for more information about query parameters.

utmccn
Stores the campaign name or value in the utm_campaign query parameter.

utmctr
Identifies the keywords used in an organic search or the value in the utm_term query parameter.

utmcmd
A campaign medium or value of utm_medium query parameter.

utmcct
Campaign content or the content of a particular ad (used for A/B testing)
The value from utm_content query parameter.

utmgclid
A unique identifier used when AdWords auto tagging is enabled This value
is reconciled during data processing with information from AdWords.


Share:
These icons link to social bookmarking sites where readers can share and discover new web pages.

  • Digg
  • Technorati
  • del.icio.us
  • Slashdot
  • StumbleUpon
  • Ma.gnolia
  • YahooMyWeb
  • co.mments
  • Reddit
Subscribe:
  1. 19 Responses to “Integrating Google Analytics With a CRM”

  2. Salesforce.com has built in visitor source (lead) tracking technology, an extension of the Google AdWords integration that is available to all salesforce customers. Find more information on the Salesforce for Google AdWords blog at: http://blogs.salesforce.com/adwords/2007/01/new_feature_upd.html

    Your Google Analytics integration script presents an interesting method of dissecting the analytics cookie. We will certainly have a look at your code and possibly package a solution for our users to take advantage of.

    Thanks,
    -Kraig

    By Kraig on Oct 29, 2007

  3. Hi Kraig,

    Thanks for the information. I knew that there was some integration between AdWords and SalesForce, but I did not know the details. It looks like you guys have this integration thing wrapped up! :)

    The one great thing about the approach above is that it can be used with almost any type of lead generation form. You don’t need a CRM to collect and store the data. You could use an email or a simple text file.

    Thanks again for the information and for reading.

    Justin

    Thanks again for the comment and thanks for reading.

    By Justin on Oct 30, 2007

  4. Man, I just spent 6 hours last week writing this exact same functionality for our CRM!

    Great work, as always, Justin. I think your code is cleaner than mine, so I’ll borrow a few lines.

    Cheers,
    Tyson @ NMG

    By Tyson Kirksey on Oct 30, 2007

  5. Hey Tyson,

    Sorry I did not get this post out sooner! I’m glad you took the initiative to try this. Let me know how it works out.

    Thanks for reading and thanks for the feedback.

    Justin

    By Justin on Oct 30, 2007

  6. Haven’t had to deal with this one yet, but its good to know where to look in case I ever have to! I love the cookie/variable definitions at the end - great!

    Thanks Justin!

    By Jeremy on Oct 31, 2007

  7. Hey Justin,

    Kudos for such an excellent article for GA power users.

    - Ophir

    By Ophir on Oct 31, 2007

  8. Justin,

    In your code, if(gclid ==”-”) is always evaluating to true, meaning every visitor gets set to google(cpc). I’ve tried with multiple browsers and systems, all with the same result. Should it be != instead?

    Also, I prefer to pass this.id as the argument, and add the hidden fields dynamically using the form.appendChild() function. This eliminates having to add the form fields in the HTML, which means less manual edits. Just another option, I guess.

    Tyson

    By Tyson Kirksey on Oct 31, 2007

  9. Hi Tyson,

    Thanks for catching that. I copied an old version of the code that was incorrect. I’ve update the post to reflect the code.

    Also, thanks for the suggestion for improvements. I’m really glad that you’ve taken my idea and made it better! :) I am by no means the best programmer out there. My goal is to come up with some ideas, and have smarter people clean them up.

    Thanks for the feedback,

    Justin

    By Justin on Oct 31, 2007

  10. Ophir,

    Thanks! I’ve been reading your blog for a while and am flattered that you like the post.

    Thanks,

    Justin

    By Justin on Oct 31, 2007

  11. Justin,
    I can’t thank you enough for writing about this subject! I can’t wait to get this implemented using our contact form.

    Also, you mention some good reasons for WHY someone would want this info which are all valid. Our biggest thing is knowing whether a banner ad or some other form of advertising works, so by including the source on the contact form, we’ll be able to track whether that lead converted to a sale. For us, it may only take one or two sales to know that the banner ad, etc. paid off and had a positive return on investment.

    Thanks again for the great post.

    Leslie

    By Leslie Trosset on Oct 31, 2007

  12. Justin

    This is awesome code, we are using it on our site. However we still get quite a lot of visitors where it says “google | cpc | - “.

    Any ideas why this might be?

    By alex on Nov 3, 2007

  13. Hello,
    Thanks for your post, it will be very useful. In trying to implement your code, I get the following error in Firefox/2.0.0.9:

    Error: syntax error
    Line: 243, Column: 38
    Source Code:
    csegment = csegment.match([1-9]*?\.(.*));

    I never really came to grips with regexps so would appreciate your help :)

    Thanks
    Blair

    By Blair on Nov 4, 2007

  14. Hi Blair,

    There was a missing character that was causing the error in FF 2.0.0.9. Things should run smoothly now.

    Thanks for the heads up and thanks for reading the blog.

    Justin

    By Justin on Nov 17, 2007

  15. Alex,

    The ‘-’ is hsowing up in the term location, that means that there must be an issue with getting the term. The only thing I can think of is that this is what happens when there is traffic coming from the content network.

    Other than that, I would just double check to make sure the code is configured the right way.

    Thanks for reading and thanks for sharing your experience.

    Justin

    By Justin on Nov 17, 2007

  16. We tried it and can’t get the data to print on the form. Can anyone offer suggestions on what we are doing wrong? We are running coldfusion and we inserted the code Justin provided, but the fields are not getting populated on the emailed form.

    Tracking Data ========================================================================
    =========
    Source :
    Medium :
    Term :
    Content :
    Campaign :
    Segment :

    By Leslie Trosset on Dec 5, 2007

  17. Hi Justin

    Yes this is a great function and we have been using it for over 2 years.

    Do you know if this will still work (uGC) if we switch to the new google analytics cookie ga.js

    Thanks

    By Tom on Jan 9, 2008

  18. Hi Tom,

    Unfortunately, the new GA tracking code does not include the _uGC() function. If you switch to the new ga.js the best course of action is to manually add the _uGC function to your web pages.

    Hope that helps,

    Justin

    By Justin Cutroni on Jan 11, 2008

  1. 2 Trackback(s)

  2. Oct 31, 2007: Kirksey Solutions » Blog Archive » Using Google Analytics Cookies in Web Forms
  3. Nov 4, 2007: Bloglinks der Woche (KW 44) | Webanalyse & SEO - News

Post a Comment