Last active
October 16, 2015 05:35
-
-
Save chrisblakley/6fc57bc7cc6e29b3d40e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<noscript> | |
<iframe class="hidden" src="includes/no-js.php?h=HOME_URL&p=PAGE_URI&t=PAGE_TITLE" width="0" height="0" style="display:none;position:absolute;"></iframe> | |
</noscript> | |
<!-- Notes: Use server-side scripting to fill in the HOME_URL, CURRENT_PAGE_URI, and PAGE_TITLE. Be sure to encode the page title to avoid spaces! If you are using Wordpress, you can use the following snippet: --> | |
<noscript> | |
<iframe class="hidden" src="<?php echo get_template_directory_uri(); ?>/includes/no-js.php?h=<?php echo home_url('/'); ?>&p=<?php echo get_page_uri(); ?>&t=<?php urlencode(get_the_title()); ?>" width="0" height="0" style="display:none;position:absolute;"></iframe> | |
</noscript> |
This file contains hidden or 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
<?php | |
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; | |
} | |
function gaGenerateUUID() { | |
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', | |
mt_rand(0, 0xffff), mt_rand(0, 0xffff), //32 bits for "time_low" | |
mt_rand(0, 0xffff), //16 bits for "time_mid" | |
mt_rand(0, 0x0fff) | 0x4000, //16 bits for "time_hi_and_version", Four most significant bits holds version number 4 | |
mt_rand(0, 0x3fff) | 0x8000, //16 bits, 8 bits for "clk_seq_hi_res", 8 bits for "clk_seq_low", Two most significant bits holds zero and one for variant DCE1.1 | |
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) //48 bits for "node" | |
); | |
} | |
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; | |
} | |
//First, log the pageview | |
$data = array( | |
'v' => 1, | |
'tid' => 'UA-0000000-1', //Add your own Google Analytics account number here! | |
'cid' => gaParseCookie(), | |
't' => 'pageview', | |
'dh' => $_GET['h'], //Document Hostname | |
'dp' => $_GET['p'], //Page | |
'dt' => $_GET['t'] //Title | |
); | |
gaSendData($data); | |
//Send the "JavaScript Disabled" event | |
$data = array( | |
'v' => 1, | |
'tid' => 'UA-0000000-1', //Add your own Google Analytics account number here! | |
'cid' => gaParseCookie(), | |
't' => 'event', | |
'ec' => 'JavaScript Disabled', //Category | |
'ea' => $_GET['t'], //Action | |
//'el' => 'label' //Label | |
); | |
gaSendData($data); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey,
I found your solution interesting and ended up making my own. I was looking for a solution to tracking browsers with JS disabled; however, your solution assigns a new UUID for every page hit since you don't store it in a cookie. You might want to consider that if you do implement this.
Regards,
Tiernan