Permalink
Browse files

FEATURE: Make it work with 2.4, improve UI and add tests

  • Loading branch information...
lingo committed Jan 20, 2010
1 parent 87eee88 commit b176f9c04bd2a2d5d8112f39fed5fe1ba01d706b
@@ -5,6 +5,7 @@
/**
* Register a callback function for all page views.
*/
Director::add_callback(array("GoogleAnalytics","initialize"));
Object::add_extension('ContentController', 'CrawlerStatsDecorator');
Object::add_extension('SiteConfig', 'AnalyticsSetupDecorator');
?>
@@ -23,16 +23,7 @@ function viewGoogleAnalytics() {
}
function setupGoogleAnalytics() {
statusMessage('{$t1}');
$('Form_EditForm').loadURLFromServer('admin/sidereport/GoogleAnalytics/?mode=display');
}
function saveGoogleAnalytics(form) {
statusMessage('{$t2}');
var text = form.Analytics.value.split('"');
var urchin = text[text.length -2];
$('Form_EditForm').loadURLFromServer('admin/sidereport/GoogleAnalytics/?mode=save&urchin='+urchin);
statusMessage('{$t3}',"good");
return false;
window.location = '/admin/0';
}
EOF;
Requirements::customScript($script,"Analytics");
@@ -50,9 +41,9 @@ function fieldsToShow() {
function getHTML() {
$result = "<ul class=\"$this->class\">\n";
$result .= "<li>\n";
$result .= "<span onclick='viewGoogleAnalytics();'>" . _t('AnalyticsReport.VIEWANALYTICS','View Google Analytics') . "</span>\n";
$result .= "<span onclick='viewGoogleAnalytics();' style=\"cursor:pointer\">" . _t('AnalyticsReport.VIEWANALYTICS','View Google Analytics') . "</span>\n";
$result .= "</li><li>\n";
$result .= "<span onclick='setupGoogleAnalytics();'>" . _t('AnalyticsReport.SETUPANALYTICS','Setup Analytics') . "</span>\n";
$result .= "<span onclick='setupGoogleAnalytics();' style=\"cursor:pointer\">" . _t('AnalyticsReport.SETUPANALYTICS','Setup Analytics') . "</span>\n";
$result .= "</li>\n";
$result .= "</ul>\n";
return $result;
@@ -0,0 +1,47 @@
<?php
/**
* This class decorates SiteConfig (see ../_config.php)
* and provides storage and UI for the GoogleAnalyticsCode value.
*/
class AnalyticsSetupDecorator extends DataObjectDecorator {
function extraStatics() {
return array(
'db' => array(
'GoogleAnalyticsCode' => 'Varchar',
'GoogleAnalyticsScript' => 'Text'
)
);
}
function updateEditFormFields($fields) {
$fields->addFieldToTab('Root.GoogleAnalytics',
new HeaderField("info", "Paste Google's code below to collect statistics for this site", 2));
$codefield = new TextField("GoogleAnalyticsCode", "Analytics Tracking ID (will be calculated automatically)");
$codefield->setReadonly(true);
$codefield = $codefield->performReadonlyTransformation();
$fields->addFieldToTab('Root.GoogleAnalytics', $codefield);
$fields->addFieldToTab('Root.GoogleAnalytics',
new TextareaField("GoogleAnalyticsScript", "Analytics Tracking", 20, 5)
);
}
function onBeforeWrite() {
parent::onBeforeWrite();
$script = $this->owner->GoogleAnalyticsScript;
$matches = array();
$pattern = '/_gat\._getTracker \( " (UA[^"]+) "/x';
if (preg_match($pattern, $script, &$matches)) {
$this->owner->GoogleAnalyticsCode = $matches[1];
$string = "<script type='text/javascript'>\n";
$string .= 'var pageTracker = _gat._getTracker("' . $matches[1] . '");';
$string .= "\npageTracker._trackPageview();\n";
$string .= "</script>";
$this->owner->GoogleAnalyticsScript = $string;
} else {
Debug::log("couldn't match $pattern against $script");
}
}
}
@@ -57,7 +57,7 @@ public function printCrawls($since) {
$data = unserialize($this->Data);
foreach (array_keys(self::$spiders) as $index => $display) {
if(!$data[$index]) {continue;}
if(!isset($data[$index])) {continue;}
$result .= "<tr><td><img src='googleanalytics/images/$display.png' alt='$display' style='float:left;padding-right:2px;'/>";
$result .= $display;
$result .= "</td><td>";
@@ -88,4 +88,4 @@ public static function GetFor($page) {
}
}
?>
?>
@@ -0,0 +1,30 @@
<?php
/**
* This decorates the ContentController (see ../_config.php)
* and tracks page views, by User Agent.
*/
class CrawlerStatsDecorator extends Extension {
/**
* Run on every page load to inject google analyics code if set, and to record
* search engine crawls.
*/
public function onAfterInit() {
/* Record Crawlers */
$page = $this->owner->URLSegment;
$crawlers = CrawlerStats::GetFor($page);
if($crawlers->UpdateCrawls($_SERVER['HTTP_USER_AGENT'])) {
$crawlers->write();
}
/* Launch Analytics */
GoogleAnalytics::addAnalytics();
}
}
?>
@@ -18,7 +18,7 @@ static function getIndexStatus() {
else
$page = "unknown";
$indexResults = DataObject::get_one("CrawlerStats","Page = '$page/'");
$indexResults = DataObject::get_one("CrawlerStats","Page = '$page'");
$lastVersion = strtotime("now");
@@ -42,87 +42,25 @@ public function updateCMSFields($fields) {
}
/**
* Run on every page load to inject google analyics code if set, and to record
* search engine crawls.
* Add google analytics javascript code to a page
*/
static function initialize() {
/* Record Crawlers */
$page = Controller::curr()->URLSegment;
$crawlers = CrawlerStats::GetFor($page);
if($crawlers->UpdateCrawls($_SERVER['HTTP_USER_AGENT'])) {
$crawlers->write();
static function addAnalytics() {
$config = DataObject::get_one('SiteConfig');
if (!$config) {
return;
}
/* Launch Analytics */
$urchinid = DataObject::get_one("CrawlerStats","Page = '!!AnalyticsUrchinID'");
if($urchinid && $urchinid->Data[0]=="U") {
self::addAnalytics($urchinid->Data);
$script = $config->GoogleAnalyticsScript;
if (empty($script)) {
return;
}
}
/**
* Add google analytics javascript code to a page
*/
static function addAnalytics($uid) {
Requirements::insertHeadTags("<script src='http://www.google-analytics.com/ga.js' type='text/javascript'></script>", "GA");
$script = <<<END
try {
var pageTracker = _gat._getTracker("$uid");
pageTracker._trackPageview();
} catch(err) {}
END;
Requirements::customScript($script, "ga");
}
function link($action = null) {
return "admin/sidereport/GoogleAnalytics/?mode=$action";
}
/**
* Provide the current analytics snippit as google provides it
* (The snippit is modifies when we actually include it on the page (the addAnalytics function))
* because we are limited to use the Requirements interface, and therefore need to include the code
* in the head, rather than at the end of the page. The modified code makes up for this change.
*/
function currentJS() {
$uid = DataObject::get_one("CrawlerStats", "Page = '!!AnalyticsUrchinID'");
if($uid && $uid->Data[0]=="U") {
$string = "<script src='http://www.google-analytics.com/ga.js' type='text/javascript'></script>\n";
$string .= "<script type='text/javascript'>\n";
$string .= 'var pageTracker = _gat._getTracker("' . $uid->Data . '");';
$string .= "\npageTracker._trackPageview();\n";
$string .= "</script>";
return $string;
}
return "";
}
/**
* getHTML returns the html for setting up a google analytics urchin in the CMS.
*/
function getHTML() {
$mode = $_GET['mode'];
if($mode =='save') {
$urchin = DataObject::get_one("CrawlerStats", "Page = '!!AnalyticsUrchinID'");
if(!$urchin) {
$urchin = new CrawlerStats();
$urchin->Page = "!!AnalyticsUrchinID";
}
$urchin->Data = $_GET['urchin'];
$urchin->write();
}
$response = new Form($this, "AnalyticsForm", new FieldSet(
new LiteralField("info", "<h2>Paste Google's code below to collect statistics for this site</h2>"),
new TextareaField("Analytics", "Analytics Tracking", 5, 20, $this->currentJS()),
new LiteralField("save", "<button type='submit' onclick='return saveGoogleAnalytics(this.form);' value='Save' />Save</button>")
), new FieldSet());
return $response->forTemplate();
}
}
?>
?>
@@ -0,0 +1,43 @@
<?php
/**
* Test the class @see AnalyticsSetupDecorator
* @covers AnalyticsSetupDecorator
*/
class AnalyticsSetupDecoratorTest extends SapphireTest {
/**
* Test the function @see AnalyticsSetupDecorator::onBeforeWrite
* @covers AnalyticsSetupDecorator::onBeforeWrite
*/
function testOnBeforeWrite() {
$config = new SiteConfig();
$config->GoogleAnalyticsScript = <<<ENDSCRIPT
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-6967217-2");
pageTracker._trackPageview();
} catch(err) {}</script>
ENDSCRIPT;
$config->write();
$this->assertEquals($config->GoogleAnalyticsCode, 'UA-6967217-2');
$desiredScript = <<<ENDSCRIPT
<script type='text/javascript'>
var pageTracker = _gat._getTracker("UA-6967217-2");
pageTracker._trackPageview();
</script>
ENDSCRIPT;
$this->assertEquals($config->GoogleAnalyticsCode, 'UA-6967217-2');
$this->assertEquals($config->GoogleAnalyticsScript, $desiredScript);
}
}
?>
@@ -0,0 +1,42 @@
<?php
/**
* Test the class @see CrawlerStatsDecorator
* @covers CrawlerStatsDecorator
*/
class CrawlerStatsDecoratorTest extends FunctionalTest {
static $fixture_file = 'googleanalytics/tests/CrawlerStatsDecoratorTest.yml';
// This saves having to publish all the fixture pages first
static $use_draft_site = true;
/**
* Test the function @see CrawlerStatsDecorator::onAfterInit
* @covers CrawlerStatsDecorator::onAfterInit
*/
function testOnAfterInit() {
$page = $this->objFromFixture('Page', 'testpage1');
$stats = DataObject::get_one('CrawlerStats', "Page = '{$page->URLSegment}'");
$this->assertEquals($stats, false, "Obtained CrawlerStats when fixture should have none");
// Fake the user agent
$_SERVER['HTTP_USER_AGENT'] = 'Googlebot';
$response = $this->get("{$page->URLSegment}");
$this->assertEquals($response->getStatusCode(), 200, "Failed to retrieve the URL '{$page->URLSegment}'");
$stats = DataObject::get_one('CrawlerStats', "Page = '{$page->URLSegment}'");
if ($stats) {
$data = unserialize($stats->Data);
$this->assertTrue(is_array($data) && isset($data[0]), "No relevant stats were found after page visit");
} else {
$this->assertFalse(true, "Failed to obtain CrawlerStats after visiting page");
}
}
}
?>
@@ -0,0 +1,7 @@
Page:
testpage1:
Title: My test page
URLSegment: testpage1
testpage2:
Title: Another test page
URLSegment: anothertest

0 comments on commit b176f9c

Please sign in to comment.