It’s fairly simple to integrate Google Analytics so that your campaign variables get saved behind the scenes when someone submits your wufoo form.

The code is based on the original post at analytics talk, with some small modifications for wufoo.

All we need to do is include a small javascript file that will parse our google analytics cookie and pass it to wufoo using url modification.

Here’s a 4 minute screencast of what we’ll be achieving:

Note that the code relies on the old version of the google analytics code (urchin.js update: you can find updated javascript for the new version of the tracking code at Analytics Talk, however I have not yet integrated it with wufoo) and I’m using jquery for $(document).ready functionality, but you could use your library of choice if you don’t use jQuery.

The complete javascript is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
$(document).ready(function(){
    //
    // 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 wufoo via url modification
    var csegment = _uGC(document.cookie, '__utmv=', ';');
    if (csegment != '-') {
          var csegmentex = /[1-9]*?\.(.*)/;
          csegment    = csegment.match(csegmentex);
          csegment    = csegment[1];
    } else {
          csegment = '';
    }
    //modify the wufoo iframe location to pass the analytics values
    frames[0].location.href = 'https://youraccount.wufoo.com/embed/123/def/field30=' + source + '&field36=' + medium + '&field35=' + term + '&field34=' + content + '&field33=' + campaign + '&field32=' + gclid;
 
});