Forked from akashsky44/server-side-google-analytics-for-sending-pageviews.php
Created
September 27, 2019 16:51
-
-
Save Shoora/5216793f62373e25eebe7bab95455308 to your computer and use it in GitHub Desktop.
Using server-side Google Analytics for sending pageviews and event tracking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
} | |
// To send a pageview, use this syntax: | |
// ga_send_pageview($hostname, $page, $title); | |
// Actual example: | |
// ga_send_pageview('akashkumar.live', 'http://akashkumar.live/blog/', 'Akash Kumar'); | |
// To send an event, use this syntax: | |
// ga_send_pageview($category, $action, $label); | |
// Actual example: | |
// ga_send_pageview('Contact Form', 'Submit', 'Feature Request'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment