Home > database >  Server side event tracking for Google Analytics with source & medium is not working
Server side event tracking for Google Analytics with source & medium is not working

Time:11-19

I am using a fairly simple piece of code to send events to a Google Analytics account:

$req = curl_init('https://www.google-analytics.com/collect');

curl_setopt_array($req, array(
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_POSTFIELDS =>
"v=1&t=event&tid=UA-40825301-52&cid=123456&ec=test&ea=test2&el=test3&ev=123&utmcsr=google&utmcmd=organic"
));

$response = curl_exec($req);

What I am trying to achieve is sending offline conversions to our Google Analytics as events. We do know the initial source of these conversions and want this data in Google Analytics too. utmcsr and utmcmd are supposed to be used to send source & medium data but.. all events end up as direct traffic. Any idea what might be the issue?

CodePudding user response:

What you're using is called Measurement protocol. There are multiple comfortable libraries to use it. You still can use it through curl, but then I don't recognize the utmcsr and utmcmd. Where are they from?

Here is the parameter reference for it: enter image description here

Disregard all cd parameters and the dp. You don't seem to be needing them for now.

Feel free to explore the measurement protocol properly starting from here: https://developers.google.com/analytics/devguides/collection/protocol/v1

CodePudding user response:

It appears that using the cm and cs parameters allow you to post source and medium to Google Analytics with server side analytics tracking.

What helped me tremendously:

https://ga-dev-tools.web.app/hit-builder/ https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters

My code:

$req = curl_init('https://www.google-analytics.com/collect');

curl_setopt_array($req, array(
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_POSTFIELDS =>
    
    "v=1&t=transaction&tid=UA-40825301-52&cid=e87f9f6f-fba1-4922-9319-98e3c1d6f7c6&dp=/receipt&dt=Receipt Page&ti=T12345&ta=Direct&tr=37.39&tt=2.85&ts=5.34&tcc=SUMMER2013&pa=purchase&pr1id=P12345&pr1nm=Android Warhol T-Shirt&pr1ca=Apparel&pr1br=Google&pr1va=Black&pr1ps=1&utm_source=google&utm_medium=ads&ds=web&cs=google&cm=organic"
));

$response = curl_exec($req);



$curl = curl_init();
  • Related