admin管理员组

文章数量:1430478

GA Events in Javascript we can do by

ga('send', 'event', 'test', 'test','value');

I need to implement this in entire project. But there are some sections where I need to do it via PHP.

So looking for PHP library that can be added as a module via poser.

I found

Google Analytics Measurement Protocol library for PHP

PHP Analytics Events

But this one is not best. That which I'm looking for.

Tracking Id & Website only specified by once. Then just need to pass event parameters as we do in Javascript.

Does anyone e across any kind of this library?

Found this one very helpful

/

GA Events in Javascript we can do by

ga('send', 'event', 'test', 'test','value');

I need to implement this in entire project. But there are some sections where I need to do it via PHP.

So looking for PHP library that can be added as a module via poser.

I found

Google Analytics Measurement Protocol library for PHP

PHP Analytics Events

But this one is not best. That which I'm looking for.

Tracking Id & Website only specified by once. Then just need to pass event parameters as we do in Javascript.

Does anyone e across any kind of this library?

Found this one very helpful

https://gearside./using-server-side-google-analytics-sending-pageviews-event-tracking/

Share Improve this question edited Apr 19, 2018 at 9:14 Test asked Apr 19, 2018 at 8:20 TestTest 631 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

The Google analytics javascript snippet sends data to google analytics via the measurment protocol

The measurement protocol accepts http post and http gets. This can easly be done with PHP but if you are looking for something out of the box created to do it there isnt anything official written in php that i am aware of. Your going to have to code this yourself.

Using

https://github./thomasbachem/php-ga

Install via

poser require united-prototype/php-ga

Below is the code

  use UnitedPrototype\GoogleAnalytics; 

  $tracker = new GoogleAnalytics\Tracker('UA-XXXXXX-1', 'example.');

  $session = new GoogleAnalytics\Session();

  // Setup visitor
  $visitor = new GoogleAnalytics\Visitor();
  $visitor->setIpAddress($server->get('REMOTE_ADDRESS'));
  $visitor->setUserAgent($server->get('HTTP_USER_AGENT'));

  //Set Event
  $event = new GoogleAnalytics\Event();
  $event->setCategory('Category'); // Event Category (Required)
  $event->setAction('Event'); // Event Action (Required)
  $event->setLabel('Label'); // Event Label (Optional)
  $event->setValue(1);    //integer, not required

  //track event
  $response = $tracker->trackEvent($event,$session,$visitor);

Works fine for me.

本文标签: javascriptHow to add Google Analytics Event via PHPStack Overflow