简体   繁体   中英

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

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?

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: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters I don't see there the parameters you're trying to pass in your request.

The utm params are a part of the dl parameter. The document location. You can inspect an existing collect call and see how this info is passed. Here, I used SO's existing tracking, just faked the utm params: 在此处输入图像描述

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

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=%2Freceipt&dt=Receipt%20Page&ti=T12345&ta=Direct&tr=37.39&tt=2.85&ts=5.34&tcc=SUMMER2013&pa=purchase&pr1id=P12345&pr1nm=Android%20Warhol%20T-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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM