1
votes

Is it possible to implement User ID tracking with the php-ga API library? I'd like to tie it in with our client-side tracking.

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

User ID - Web Tracking (via analytics.js) - https://developers.google.com/analytics/devguides/collection/analyticsjs/user-id

Thanks

1
As far as I know only Universal Analytics supports this. I dont know wether the the PHP-GA is up-to-date for universal analytics, or if there is a separate PHP-library for this. Just to point you in a direction - Ole Haugset
considering that it states php-ga is no longer maintained and user_id is something that was added recently I would have to guess no. - DaImTo
Ah, that's a shame - is there an alternative PHP library you guys can recommend that supports this feature? - Phil
The beauty of the measurement protocol - the basis of universal analytics - is that you do not need a library to send calls to the google server. It's just a url with a few parameters now, you can send it serverside via CURL. As already stated UserId does not work with classic analytics, so you'd need to upgrade in any case. - Eike Pierstorff

1 Answers

1
votes

You can push events without any library. Just use measurment protocol: https://developers.google.com/analytics/devguides/collection/protocol/v1/ Here is how I do it form server side:

<?php 
$data = array(
    'v=' . 1,
    't=' . 'approved', //Page
    'tid=' . 'UA-XXXXXXXX-X', //GA account code
    'cid=' . $ga_user_id, // 1267034788.1479821336 needs to be extracted from $_COOKIE['_ga']
    'ec=' . 'credit', // for event only - event category
    'ea=' . 'denied',   // for event only - event action
    'el=' . '12345', // for event only - event label
  );

  $ch = curl_init('www.google-analytics.com/collect');

  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $data));

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  curl_exec($ch);
  curl_close($ch);

Here is builder tool that I've been using for url building: https://ga-dev-tools.appspot.com/hit-builder/