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.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
aokozlov
  • 711
  • 2
  • 7
  • 21
  • 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 '15 at 09: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: https://developers.google.com/analytics/devguides/collection/analyticsjs/events – Auris Aug 25 '15 at 09: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 '15 at 09: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 '15 at 09:41
  • @DaImTo I disagree, but I'm not going to argue my point in the comment section of a question :) – Epodax Aug 25 '15 at 09:44

3 Answers3

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
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • 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 '15 at 09: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. – Linda Lawton - DaImTo Aug 25 '15 at 09:30
  • It seems like everyone can take my URL and do something weird with my stats – aokozlov Aug 25 '15 at 09:31
  • 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. – Linda Lawton - DaImTo Aug 25 '15 at 09: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 https://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 '15 at 09:43
  • 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. http://www.daimto.com/monitoring-quota-usage-for-google-apis/ – Linda Lawton - DaImTo Aug 25 '15 at 09:45
  • 2
    Remember to check the real-time reports events wont show up in the standard reports for 24 hours. – Linda Lawton - DaImTo Aug 25 '15 at 09:47
  • 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. – Amol Apr 09 '18 at 11:29
  • 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. – Linda Lawton - DaImTo Apr 09 '18 at 11:33
  • @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? – le3th4x0rbot Jul 25 '19 at 01:28
  • assuming you mean uid its just a string normally use your internal user id – Linda Lawton - DaImTo Jul 25 '19 at 12:14
  • If you noticed this is a 7 year old answer and are looking for docs for Google Analytics v4 then see this: https://developers.google.com/analytics/devguides/collection/protocol/ga4/sending-events?client_type=gtag – Manstie Feb 01 '23 at 07:44
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();
Adarsh Madrecha
  • 6,364
  • 11
  • 69
  • 117
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);
Basj
  • 41,386
  • 99
  • 383
  • 673