Server Side Google Analytics with ColdFusion

On November 25, 2011, in ColdFusion, by Anuj Gakhar

If you have an application/page that returns only XML or JSON repsonses and the end client is not a browser (an API e.g.) and you want to use Google Analytics for tracking your API calls, then you won’t be able to use the standard Google Analytics Javascript code. There is no browser involved and you can’t mix Javascript code with your XML/JSON responses. If you debug you Google Analytics Javascript code in Firebug e.g., you will notice that it actually makes a request to a .gif image on the Google Servers, with all the correct parameters passed to it, ofcourse. You can find out more about each of these parameters here.

Here is a little piece of code that will do the same thing in ColdFusion, using CFHTTP.

[cf]<cfset siteURl = "your_domain_name" />
<cfset path = "path_to_page"/>
<cfset trackingCode = "UA-GOOGLE-ID"/>

<cfset now = int(getTickCount()/1000)/>
<cfset rand = RandRange(10000000,99999999)/>
<cfset num = RandRange(1000000000,2147483647)/>
<cfset utmcc = "__utma=#rand#.#num#.#now#.#now#.#now#.1;"/>

<cfhttp url="http://www.google-analytics.com/__utm.gif">
<cfhttpparam type="url" name="utmwv" value="4.4sh"/>
<cfhttpparam type="url" name="utmn" value="#rand#"/>
<cfhttpparam type="url" name="utmhn" value="#siteURL#"/>
<cfhttpparam type="url" name="utmr" value="#cgi.HTTP_REFERER#"/>
<cfhttpparam type="url" name="utmp" value="#path#"/>
<cfhttpparam type="url" name="utmac" value="#trackingCode#"/>
<cfhttpparam type="url" name="utmcc" value="#utmcc#"/>
<cfhttpparam type="url" name="utmvid" value="#num#"/>
<cfhttpparam type="url" name="utmip" value="#cgi.remote_addr#"/>
</cfhttp>[/cf]

Bear in mind that you don’t need to use URLEncodedFormat() on the values in tags as CF will automatically encode them for you. Another thing to note here is that if you dont put random values in the utmcc variable, all your requests will be treated as the same visitor. So that bit is important if you want to treat each request separately.

Tagged with:  

9 Responses to Server Side Google Analytics with ColdFusion

  1. Pradeep says:

    Good one!

  2. Mike says:

    I copy+pasted your code, updated it to reflect my GA account, I get a 200 response from Google- everything looks right, but nothing’s showing up in my GA reports- no hits/traffic. Any suggestions?

    • Anuj Gakhar says:

      Hi Mike, not sure what’s wrong. this same code works for me. Are you trying to look at the real time stats or the standard stats. The standard ones don’t show up immediately, although I am sure you know that already.

    • Trevor says:

      I ran into the same issue. I ended up putting a packet sniffer to see what was getting sent to google and what was coming back. The problem with this example is when you use cfhttpparam with type url, coldfusion will use URLEncodedFormat on the value. In this scenario it encodes your account id and then google is unable to track it to your account.

      • Mike says:

        What would be the solution to this problem then? Thanks!!

        • Mike says:

          I might have answered my own question rather quickly, not sure why I didn’t try this before. It seems like I have it working, am seeing hits in GA Real-time tracking.. instead of doing the cfhttpparam, I just passed in all the variables over the cfhttp URL, so:

          This seems to be working great. I am working on a PhoneGap app using their Build service which doesn’t support Plugins yet. The app does make calls to my web server (JSON gets returned to the app), and now I am seeing which “pages” are getting traffic in GA. Excellent.

        • Mike says:

          It looks like my CF example didn’t make it into the post, so for those of you who want to know what I did.. all I did was take all the cfhttpparams listed in this example and appended them to the URL of the gif image, so:

          __utm.gif?utmvw=4.4sh&utmn=#rand#.. etc…

          reducing this snippet down to a couple lines of code- now my PhoneGap app has Google Analytics tracking.. it’s just done on my server instead of from the app itself.

          • Kevin says:

            I ran into the same problem, I could not use the cfhttpparam as it was url encoding certain parts of the variable string that should not be encoded. I had to manually put the variables into the URL encoding the parts that needed to be encoded.

Leave a Reply to Mike Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2011 Anuj Gakhar
%d bloggers like this: