There is some custom google analytics events I’ve specified for my application.
Trigering these events is sometimes not very easy.
For example: User is redirected to home page after purchase and I have to forward some data that this home page is displayed after purchase to push something to _gaq based on that.
Is there any way to trigger google analytics from backend e.g. in a controller without taking care if the action is ajax request or plain request and response has redirect or Ok status?
Yeah, all data gathered by Google Analytics comes from GIF requests, which is nothing more than a single pixel gif with a very large query string attached. Google processes the servers’ logs and the data goes to your analytics. If you inspect the resources loaded by your page, you will find something like this:
http://www.google-analytics.com/__utm.gif?utmwv=4&utmn=769876874&utmhn=example.com&utmcs=ISO-8859-1&utmsr=1280x1024&utmsc=32-bit&utmul=en-us&utmje=1&utmfl=9.0%20%20r115&utmcn=1&utmdt=GATC012%20setting%20variables&utmhid=2059107202&utmr=0&utmp=/auto/GATC012.html?utm_source=www.gatc012.org&utm_campaign=campaign+gatc012&utm_term=keywords+gatc012&utm_content=content+gatc012&utm_medium=medium+gatc012&utmac=UA-30138-1&utmcc=__utma%3D97315849.1774621898.1207701397.1207701397.1207701397.1%3B...The meaning of each
utmvalue can be found here, but this one is more complete.Because of that, it is possible to track things using Google Analytics in server side, if your code handles the query string construction by itself, with the appropriate parameters, and then fires to that gif-url.
There is a very good project to do that in PHP: Server-Side Google Analytics PHP Client. I think you can implement it in your php ajax file to handle those tricky custom variables.