//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); | |
} |
What about 'purchase' event? I mean how $data array keys should look like while sending purchase event (parameters etc?) ??
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 :)
@bbeckford I think your Gist is set to private.
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
Hey guys, Should I be able to see these events in the real time section ?
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.
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.