Skip to content

Instantly share code, notes, and snippets.

@chrisblakley
Last active August 16, 2022 19:22
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chrisblakley/e1f3d79b6cecb463dd8a to your computer and use it in GitHub Desktop.
Save chrisblakley/e1f3d79b6cecb463dd8a to your computer and use it in GitHub Desktop.
PHP functions to send data to Google Analytics
//Parse the GA Cookie
function gaParseCookie() {
if (isset($_COOKIE['_ga'])) {
list($version, $domainDepth, $cid1, $cid2) = explode('.', $_COOKIE["_ga"], 4);
$contents = array('version' => $version, 'domainDepth' => $domainDepth, 'cid' => $cid1 . '.' . $cid2);
$cid = $contents['cid'];
} else {
$cid = gaGenerateUUID();
}
return $cid;
}
//Generate UUID
//Special thanks to stumiller.me for this formula.
function gaGenerateUUID() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
//Send Data to Google Analytics
//https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#event
function gaSendData($data) {
$getString = 'https://ssl.google-analytics.com/collect';
$getString .= '?payload_data&';
$getString .= http_build_query($data);
$result = wp_remote_get($getString);
return $result;
}
//Send Pageview Function for Server-Side Google Analytics
function ga_send_pageview($hostname=null, $page=null, $title=null) {
$data = array(
'v' => 1,
'tid' => 'UA-000000-1', //@TODO: Change this to your Google Analytics Tracking ID.
'cid' => gaParseCookie(),
't' => 'pageview',
'dh' => $hostname, //Document Hostname "gearside.com"
'dp' => $page, //Page "/something"
'dt' => $title //Title
);
gaSendData($data);
}
//Send Event Function for Server-Side Google Analytics
function ga_send_event($category=null, $action=null, $label=null) {
$data = array(
'v' => 1,
'tid' => 'UA-000000-1', //@TODO: Change this to your Google Analytics Tracking ID.
'cid' => gaParseCookie(),
't' => 'event',
'ec' => $category, //Category (Required)
'ea' => $action, //Action (Required)
'el' => $label //Label
);
gaSendData($data);
}
@xgretsch
Copy link

Works like a charm, that you. Note that if you're not in a Wordpress environment, you'll need to use something in place of wp_remote_get: I tend to use Curl for remote accesses and that worked fine for me.

@radekandrzejewicz
Copy link

What about 'purchase' event? I mean how $data array keys should look like while sending purchase event (parameters etc?) ??

@bbeckford
Copy link

bbeckford commented Sep 17, 2021

Hi all, I've put together a similar solution for the new Google Analytics 4 over here if anyone's is interested - https://github.com/bbeckford/google_analytics_4_serverside_event_tracker

Just thought I would share in case it's helpful or if anyone can help decipher the rest of the fields :)

@nilsbosman
Copy link

@bbeckford I think your Gist is set to private.

@bbeckford
Copy link

Ah it was just the wrong link, thanks for pointing that out @nilsbosman, it's public here - https://github.com/bbeckford/google_analytics_4_serverside_event_tracker

@arvai
Copy link

arvai commented Jan 1, 2022

Hey guys, Should I be able to see these events in the real time section ?

@m-ovs
Copy link

m-ovs commented Jan 18, 2022

Why do you use Wordpress function wp_remote_get()? You'd better use file_get_contents() and code will be valid anywhere, not only for Wordpress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment