Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Analytics track custom events in new Web+App

I used to track custom events (API hits) with google analytics and PHP via cURL, but now analytics is deprecating this method. I understood that the new analytics Web+App is used to track this kind of events, but i cannot find anything that allows me to track those events. my current code:

$response = $client->post('https://www.google-analytics.com/collect', [
    'form_params'   => [
        'v'     => 1,
        't'     => 'event',
        'ec'    => 'ap1-v1-xxx',
        'ea'    => 'invoke',
        'el'    => 'MY-API',
        'tid'   => 'XXXXXXXX',
        'cid'   => '555'
    ]
]);

Doing this, i was able to track every hit and have statistics about API usage, this is what the analytics panel looked-like:

enter image description here

But, as i said, analytics is deprecating this method, and it stopped tracking my hits:

https://support.google.com/firebase/answer/9167112?ref_topic=6386699

There is a way to keep track these custom events? I can't find anything alike in PHP or cURL whatsoever.

Thanks!

like image 243
Gumma Mocciaro Avatar asked Oct 15 '22 08:10

Gumma Mocciaro


1 Answers

It's not so much the method (measurement protocol) has been deprecated, it's that it is using a new, as of yet apparently undocumented, version 2 of the measurement protocol.

That makes sense - you cannot sent hit type anymore (because there is now a single type event, whose name can be customized), and you cannot send event category, action and label, since those no longer exist, and have been replaced by event parameters.

Since there does not seem to be documentation yet, you can do a bit of reverse engineering. I looked at the request issued by the code from a web&app property (actually gtag.js) for a pageview:

https://www.google-analytics.com/g/collect? // endpoint, remains the same
v=2 // protocol version, v2
&tid=G-XXXXXXXXXX // tracking id
&_p=1253409603 // no idea, don't think this needs to be set
&sr=1920x1080 // screen resolution, not applicable to a serverside call
&ul=de-de // user agent language, probably not relevant for a serverside call
&cid=533127994.1575982871 // client id
&_s=1  // no idea
&en=pageview // event - this corresponds broadly to hit type t in the previous version
&dl=http://localhost/test2.html // document location
&dr= // document referrer, not relevant for a serverside call
&dt=Title // document title
&sid=1575982870 // no idea
&sct=1 // no idea
&seg=1 // no idea

I think for a serverside application you can ignore all parameters marked with "no idea" (I assume that is something determined by the Javascript tracking code).

Instead of "v=1" you need to set "v=2", and instead of "t" for hit type you need "en" for event name. I will see if I can work out how to sent event parameters (I am in the office and don't really have time to experiment), but in any case this should be enough to get you started (I tested a call via curl and it showed up in the realtime section of a web&app property, so it should work for you, too).

like image 159
Eike Pierstorff Avatar answered Oct 20 '22 04:10

Eike Pierstorff