Send event to Google Analytics using API server

I have a website where I post events to Google Analytics using the 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-up tutorial: Hello API Analytics: running PHP quickly for service accounts , and reporting works like a charm, but I have no idea how to send an event.

Could you show me step by step what I should write in order to send the exact same event as mentioned above.

+10
php google-api google-analytics measurement-protocol
source share
3 answers

Hello API Analytics: running PHP quickly for service accounts will not help you at all. This code uses the basic reporting API, the basic reporting API to request data from . Google Analytics does not send 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 fragment you posted also uses the measurement protocol.

You can use the measurement protocol from any language that supports the HTTP message or Http Get. However, there is no special library for sending information to Google analytics, which you will need to format yourself. A tip would be to use Validating hits to test it before posting it to Google while you are developing it.

It probably looks 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 
+14
source share

There is a related question that will give you an idea of ​​how to structure your PHP code to send [POST] event data to Google Analytics. Find it here . Hope this helps.

0
source share

There is a php-ga-measuring protocol of the by theiconic PHP library on github that 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
source share

All Articles