Created
October 17, 2018 13:04
-
-
Save akashsky44/8c7f54b024d754695e4454eb93eb3c34 to your computer and use it in GitHub Desktop.
Track when users have JavaScript disabled with Google Analytics
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
<?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); | |
//add one of the following <noscript> into the top of the <body> to trigger the above file. The first uses placeholders for page data, and the second shows how that data can be passed using WordPress. | |
//<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> | |
echo <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> | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment