26

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.

5
  • Ok, i know, but anyway i haven't found any method in that GA API to send events and need help of master.
    – aokozlov
    Aug 25, 2015 at 9:20
  • 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/…
    – Auris
    Aug 25, 2015 at 9:21
  • I know about JS methods (as i mentioned above), but there have to be a way to send the same information using server-side.
    – aokozlov
    Aug 25, 2015 at 9:25
  • @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 ;)
    – Epodax
    Aug 25, 2015 at 9:41
  • @DaImTo I disagree, but I'm not going to argue my point in the comment section of a question :)
    – Epodax
    Aug 25, 2015 at 9:44

3 Answers 3

38

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
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?
    – aokozlov
    Aug 25, 2015 at 9:28
  • 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. Aug 25, 2015 at 9:30
  • 1
    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. Aug 25, 2015 at 9:32
  • 1
    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.
    – aokozlov
    Aug 25, 2015 at 9:43
  • 2
    Remember to check the real-time reports events wont show up in the standard reports for 24 hours. Aug 25, 2015 at 9:47
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();
0
1

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);
1
  • How should we set the CID?
    – Philip
    Jul 3, 2022 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.