Send event to Google Analytics using API server sided
Asked Answered
P

3

27

I have a website where I send events to Google Analytics using javascript function:

ga('send', 'event', 'showphone', 'feedback', 'result');

However I also need to send some similar events from server-side using PHP. I tried this quick start tutorial: Hello Analytics API: PHP quickstart for service accounts and reporting works like a charm, but I have no idea how to send event.

Could you please show me step-by-step what I should code to send exactly same event like mentioned above.

Plerre answered 25/8, 2015 at 9:13 Comment(5)
Ok, i know, but anyway i haven't found any method in that GA API to send events and need help of master.Plerre
not that I'm a pro with GA, but as far as I am aware events on GA are only handled with JS (as most of them are front end actions). This is explained quite step-by-step here: developers.google.com/analytics/devguides/collection/…Dumbwaiter
I know about JS methods (as i mentioned above), but there have to be a way to send the same information using server-side.Plerre
@DaImTo - Feel free to do that, but OP asked for a "step-by-step". And while I would tell him if he's going the wrong way, it requires for me to know that he is in fact going to wrong way ;)Shinn
@DaImTo I disagree, but I'm not going to argue my point in the comment section of a question :)Shinn
J
39

Hello Analytics API: PHP quickstart for service accounts is not going to help you at all. That code uses the core reporting API the core reporting API is for requesting data from Google Analytics not sending data to Google Analytics.

To send data to Google Analytics we use the Measurement Protocol. The measurement protocol is used to send information to Google analytics the JS snippet you posted also uses the measurement protocol.

You can use the measurement protocol from any language that supports HTTP post or Http Get. That being said there is no PHP specific library for sending information to Google analytics you are going to have to format your post yourself. A tip would be to use Validating hits to check it before you send it to Google while you are developing this.

It will probably look something like this

http://www.google-analytics.com/collect?v=1&tid=UA-XXX-Y&cid=35009a79-1a05-49d7-b876-2b884d0f825b&an=My%20Awesom%20APP&aid=com.daimto.awesom.app&av=1.0.0&aiid=come.daimto.awesom.installer &t=event&ec=list&ea=accounts&userclicked&ev=10
Jalisajalisco answered 25/8, 2015 at 9:25 Comment(12)
Thank you! I've seen that but thought that there are some methods in mentioned API for sending. So, do i need just POST or GET URL and that's it? But what to do with authorisation? Or that should be sent only from registered server?Plerre
Nope, measurement protocol is the only way to send tracking data to Google analytics. The other APIs are just for requesting data and account administration.Jalisajalisco
It seems like everyone can take my URL and do something weird with my statsPlerre
well not your url but yes you can view the source of someone's website get there Google Analytics tracking id and send garbage data to there Google Analytics account using the Measurement protocol. There are already bots doing this its called referral spam. Google is working on the problem.Jalisajalisco
Now i'm trying but got nothing... I made a payload: v=1&t=event&tid=UA-XXXXXXXX-1&cid=b5d9730e-59bf-4d21-aef7-fe415f64e7eb&ec=test_category&el=test_label&ev=9999&ea=test_action using tool ga-dev-tools.appspot.com/hit-builder. After that i'm trying to send it (from the builder page) but have no event in my google analytics.Plerre
Try reading this its mostly for application google analytics but its valid if you just replace screenviews with page views it should give you a head start. I will remember to add another post about this specifically. daimto.com/monitoring-quota-usage-for-google-apisJalisajalisco
Remember to check the real-time reports events wont show up in the standard reports for 24 hours.Jalisajalisco
I tried and made valid hit payload, that event is also logged in GA. But how can I hit this payload from server side. How can I make CURL request to this payload in order to log events from server side.Cretonne
Its just a normal HTTP get request that shouldn't be hard to get working in curl. You may want to open another question for that if you are having issues.Jalisajalisco
@DaImTo Did you generate your own client UUID somehow, or collect the Google Analytics generated one from the client and send it to the server?Cataplasia
assuming you mean uid its just a string normally use your internal user idJalisajalisco
If you noticed this is a 7 year old answer and are looking for docs for Google Analytics v4 then see this: developers.google.com/analytics/devguides/collection/protocol/…Peep
D
17

There is a PHP library php-ga-measurement-protocol by theiconic on github which can be used to send data using Measurement Protocal.

use TheIconic\Tracking\GoogleAnalytics\Analytics;

// Instantiate the Analytics object
// optionally pass TRUE in the constructor if you want to connect using HTTPS
$analytics = new Analytics(true);

// Build the GA hit using the Analytics class methods
// they should Autocomplete if you use a PHP IDE
$analytics
    ->setProtocolVersion('1')
    ->setTrackingId('UA-26293728-11')
    ->setClientId('12345678')
    ->setDocumentPath('/mypage')
    ->setIpOverride("202.126.106.175");

// When you finish bulding the payload send a hit (such as an pageview or event)
$analytics->sendPageview();
Driblet answered 3/7, 2018 at 11:55 Comment(0)
A
2

Here is an example of how to do it with PHP.

First build your request with Google Analytics Hit Builder, test it with https://google-analytics.com/debug/collect?_query_here, and then send it with file_get_contents (see here).

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => 'v=1&t=transaction&tid=UA-xxxxxxx-x&cid=xxxxxx&ti=abcdef&tr=100&in=productname'
    )
);
$context  = stream_context_create($options);
$result = file_get_contents('https://www.google-analytics.com/collect', false, $context);
Appellation answered 27/11, 2020 at 13:51 Comment(1)
How should we set the CID?Modern

© 2022 - 2024 — McMap. All rights reserved.