Skip to content

Instantly share code, notes, and snippets.

@ahutchings
Created April 21, 2009 18:03
Show Gist options
  • Save ahutchings/99279 to your computer and use it in GitHub Desktop.
Save ahutchings/99279 to your computer and use it in GitHub Desktop.
Modification of Habari's Google Analytics plugin to allow for caching the tracking file locally. Should also validate as real XHTML.
<?php
class GoogleAnalytics extends Plugin {
function info()
{
return array(
'url' => 'http://iamgraham.net/plugins',
'name' => 'GoogleAnalytics',
'description' => 'Automatically adds Google Analytics code to the bottom of your webpage.',
'license' => 'Apache License 2.0',
'author' => 'Graham Christensen',
'authorurl' => 'http://iamgraham.net/',
'version' => '0.5.1'
);
}
public function filter_plugin_config($actions, $plugin_id)
{
if ($plugin_id == $this->plugin_id()) {
$actions[]= _t('Configure');
}
return $actions;
}
public function action_plugin_ui($plugin_id, $action)
{
if ( $plugin_id == $this->plugin_id() ) {
switch ($action) {
case _t('Configure'):
$form = new FormUI(strtolower(get_class($this)));
$form->append('text', 'clientcode', 'googleanalytics__clientcode', _t('Analytics Client Code'));
$form->append('checkbox', 'loggedintoo', 'googleanalytics__loggedintoo', _t('Track logged-in users too'));
$form->append('checkbox', 'cache', 'googleanalytics__cache', _t('Cache tracking code file locally'));
$form->append('submit', 'save', 'Save');
$form->out();
break;
}
}
}
/**
* Add update beacon support
**/
public function action_update_check()
{
Update::add( 'GoogleAnalytics', '7e57a660-3bd1-11dd-ae16-0800200c9a66', $this->info->version );
}
public function filter_rewrite_rules($db_rules)
{
$db_rules[] = RewriteRule::create_url_rule('"ga.js"', 'GoogleAnalytics', 'retrieve_cache');
return $db_rules;
}
public function act($action)
{
switch ($action)
{
case 'retrieve_cache':
self::retrieve_cache();
break;
}
}
public function retrieve_cache()
{
if (Cache::has('ga.js')) {
$js = Cache::get('ga.js');
} else {
$js = RemoteRequest::get_contents('http://www.google-analytics.com/ga.js');
Cache::set('ga.js', $js, 86400); // cache for 1 day
}
// Clean the output buffer, so we can output from the header/scratch
header( 'Content-Type: application/javascript' );
echo $js;
}
private function detect_ssl()
{
if ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1 || $_SERVER['SERVER_PORT'] == 443) {
return true;
}
return false;
}
function theme_footer()
{
if ( URL::get_matched_rule()->entire_match == 'user/login' ) {
// Login page; don't dipslay
return;
}
$clientcode = Options::get('googleanalytics__clientcode');
if ( Options::get('googleanalytics__cache') ) {
$ga_url = URL::get() . 'ga.js';
} else {
$ga_url = (self::detect_ssl()) ? 'https://ssl.google-analytics.com/ga.js' : 'http://www.google-analytics.com/ga.js';
}
// always output the first part, so things like the site overlay work for logged in users
echo <<<SCRIPT1
<script src="{$ga_url}" type="text/javascript"></script>
SCRIPT1;
echo <<<SCRIPT2
<script type="text/javascript">
//<![CDATA[
try {
var pageTracker = _gat._getTracker("{$clientcode}");
SCRIPT2;
// only actually track the page if we're not logged in, or we're told to always track
if ( User::identify()->loggedin == false || Options::get('googleanalytics__loggedintoo') ) {
echo <<<SCRIPT3
pageTracker._trackPageview();
SCRIPT3;
}
echo <<<SCRIPT4
} catch(err) {}
//]]>
</script>
SCRIPT4;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment