23

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?

Filippo Vitale
  • 7,597
  • 3
  • 58
  • 64
Bogdan Gusiev
  • 8,027
  • 16
  • 61
  • 81

3 Answers3

37

June 2022 Update

Measurement Protocol current version: https://developers.google.com/analytics/devguides/collection/protocol/ga4

(h/t @Rafael Gomes Francisco)


To trigger Google Analytics from the backend you should use: Measurement Protocol.

That is part of the Analytics Collection:

Web Tracking (ga.js) Measure user interaction with websites or web applications.

Android Measure user interaction with Android applications.

iOS Measure user interaction with iOS applications.

Measurement Protocol Measure user interaction in any environment with this low-level protocol.

At the moment it is available as dev preview. To signing up simply request access to the beta.

Filippo Vitale
  • 7,597
  • 3
  • 58
  • 64
13

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 utm value 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.

Victor Schröder
  • 6,738
  • 2
  • 42
  • 45
2

Doing this could trigger anti-spam measures on Google's end. If they see a single IP sending so many statistics, they might put it on a blacklist.

I'd simply set a cookie that says "Just made a purchase", then check for it in the home page's Javascript, send the appropriate event from there and delete it.

Tomas Andrle
  • 13,132
  • 15
  • 75
  • 92