Last active
September 7, 2017 07:28
-
-
Save quinncomendant/6ce94a4f995d093488486e23ebea61ff to your computer and use it in GitHub Desktop.
Class for server-side submission of data to 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 | |
/* | |
* Analytics.inc.php | |
* | |
* Class for server-side submission of data to Google Analytics. | |
* TODO: Would be better to save the request to a queue to process submissions in the background. Currently, requests are blocked until the POST to GA completes or timeout is reached. | |
* | |
* @author Quinn Comendant <quinn@strangecode.com> | |
* @version 1.0 | |
* @since 24 Aug 2014 15:35:43 | |
* | |
* Example of use: | |
--------------------------------------------------------------------- | |
require_once 'lib/Analytics.inc.php'; | |
$ga = new Analytics(array( | |
'tid' => $google_analytics_property_id, | |
)); | |
$ga->submit($page_title); | |
--------------------------------------------------------------------- | |
*/ | |
class Analytics { | |
// Configuration parameters for this object. | |
protected $_params = array( | |
// 'url' => 'http://www.google-analytics.com/collect', // The Google Analytics Measurement Protocol URL. | |
'url' = 'https://ssl.google-analytics.com/collect', // The Google Analytics Measurement Protocol URL. | |
'tid' => '', // The ID that distinguishes to which Google Analytics property to send data. | |
'timeout' => 5, | |
); | |
/** | |
* Analytics constructor. | |
* | |
* @access public | |
* @param string Namespace for this object, used to avoid collisions in global contexts. | |
* @param string Configuration parameters for this object. | |
*/ | |
public function __construct($params=null) | |
{ | |
// Set custom parameters. | |
$this->setParam($params); | |
} | |
/** | |
* Set the params of this object. | |
* | |
* @access public | |
* @param array $params Array of param keys and values to set. | |
*/ | |
public function setParam($params=null) | |
{ | |
if (isset($params) && is_array($params)) { | |
// Merge new parameters with old overriding only those passed. | |
$this->_params = array_merge($this->_params, $params); | |
} | |
} | |
/** | |
* Return the value of a parameter, if it exists. | |
* | |
* @access public | |
* @param string $param Which parameter to return. | |
* @return mixed Configured parameter value. | |
*/ | |
public function getParam($param) | |
{ | |
if (isset($this->_params[$param])) { | |
return $this->_params[$param]; | |
} else { | |
return null; | |
} | |
} | |
/* | |
* Send a tracking request to the Measurement Protocol API. This is not an asynchronous request, and will delay page loading times. | |
* TODO: modify this to use a non-blocking POST to the API. | |
* | |
* @access public | |
* @param string $dt Document title. | |
* @param string $dl The full URL (document location) of the page on which content resides. | |
* @param string $t The type of hit. Must be one of 'pageview', 'screenview', 'event', 'transaction', 'item', 'social', 'exception', 'timing'. | |
* @return bool True or false indicating success or error. | |
* @author Quinn Comendant <quinn@strangecode.com> | |
* @version 1.0 | |
* @since 24 Aug 2014 15:33:52 | |
*/ | |
public function submit($dt='', $dl=null, $t='pageview') | |
{ | |
// Use the cid provided by GA if it exists. | |
if (isset($_COOKIE['_ga']) && substr_count($_COOKIE['_ga'], '.')) { | |
$cid = join('.', array_slice(explode('.', $_COOKIE['_ga']), 2)); | |
} else { | |
// Otherwise use the fp (fingerprint) cookie, which may be set by fingerprint.js or here by uniqid(). | |
if (isset($_COOKIE['fp'])) { | |
$cid = $_COOKIE['fp']; | |
} else { | |
$cid = uniqid('', true); | |
setcookie('fp', $cid, strtotime('+10 years'), '/'); | |
} | |
} | |
$dl = is_null($dl) ? sprintf('%s://%s%s', $_SERVER['REQUEST_SCHEME'], $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI']) : $dl; | |
$params = array( | |
'v' => '1', // The protocol version. The value should be 1. | |
'tid' => $this->getParam('tid'), // The ID that distinguishes to which Google Analytics property to send data. | |
'cid' => $cid, // An ID unique to a particular user. | |
't' => $t, // The type of hit. Must be one of 'pageview', 'screenview', 'event', 'transaction', 'item', 'social', 'exception', 'timing'. | |
'dl' => $dl, // The full URL (document location) of the page on which content resides. | |
'dt' => $dt, // Title. | |
'dr' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '', | |
'z' => mt_rand(), | |
); | |
$context = stream_context_create(array('http' => | |
array( | |
'method' => 'POST', | |
'header' => array('Content-Type: text/html'), | |
'content' => http_build_query($params), | |
'timeout' => $this->getParam('timeout'), | |
) | |
)); | |
return file_get_contents($this->getParam('url'), false, $context); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment