Permalink
Browse files

APICHANGE: rewrite of the entire module

  • Loading branch information...
1 parent 5aacc44 commit 4c1e8ed7e8b7bbbde6820fcd32d496f5548bf18d Andreas Piening committed Nov 9, 2010
View
@@ -1,36 +0,0 @@
-# Google Analytics Module #
-
-## Maintainer Contact ##
-
-Andreas Piening (andreas at silverstripe dot com)
-
-## Requirements ##
-This module requires SilverStripe 2.4
-
-## Installation Instructions ##
-
-1. Copy the 'googleAnalytics' folder to the base of your silverstripe installation.
-2. Navigate to /db/build to create the table for this module (eg. http://mysite/db/build)
-
-## Usage ##
-
-### Analytics ###
-
-In the main CMS interface, an 'Analytics' report has been added to the 'Site
-Reports' feature (under the site tree)
-
-This report provides access to Google Analytics from within silverstripe (via
-the 'View Google Analytics' item). It also allows you to add the Google
-Analytics tracking "urchin" to your site without modifying your site template.
-To use this feature, copy the snippit of code provided in the google analytics
-interface, and paste in the the box provided by the 'Setup Analytics' item.
-Visitors to your site will now be tracked by Analtics.
-
-### Index Status ###
-
-A new piece of data has been added to every page in your silverstripe
-installation. In the main CMS, there is now an 'Index' tab under the 'Reports'
-tab. This provides a view of which search engines have indexed each page on
-your site, and if the latest version of your pages has been indexed by search
-engines.
-
View
@@ -1,13 +1,6 @@
<?php
-DataObject::add_extension('SiteTree', 'GoogleAnalytics');
+GoogleLogger::activate('SiteConfig');
+GoogleAnalyzer::activate('SiteConfig');
-/**
- * Register a callback function for all page views.
- */
-Object::add_extension('ContentController', 'CrawlerStatsDecorator');
-Object::add_extension('SiteConfig', 'AnalyticsSetupDecorator');
-
-SS_Report::register("ReportAdmin", "AnalyticsReport");
-
-?>
+// SS_Report::register("ReportAdmin", "AnalyticsReport");
View
@@ -1,65 +0,0 @@
-<?php
-/**
- * This class provides the side report in the main cms, and contains the front-end
- * code for the cms side-report items.
- *
- * @package googleanalytics
- */
-class AnalyticsReport extends SS_Report {
-
- function description() {
- return 'Google analytics report';
- }
-
- /**
- * Title is called to get the name of the side report,
- * this will need to happen during the initial loading
- * of the cms, so is a good time to load the additional
- * javascript.
- */
- function title() {
-
- $siteconfiglink = BASE_URL . '/admin/show/root#Root_GoogleAnalytics';
- $t1 = _t('AnalyticsReport.LOADINGFORM','Loading Form...');
- $t2 = _t('AnalyticsReport.SAVING','Saving...');
- $t3 = _t('AnalyticsReport.SAVED','Saved!');
- $script = <<<EOF
- function viewGoogleAnalytics() {
- $('Form_EditForm').innerHTML = "<iframe name='analytics' src='http://analytics.google.com/' border='0' style='width:100%; height:100%;'></iframe>";
- }
- function setupGoogleAnalytics() {
- statusMessage('{$t1}');
- window.location = '$siteconfiglink';
- }
-EOF;
- Requirements::customScript($script,"Analytics");
-
- return "Google Analytics";
- }
-
- function getCMSFields() {
- $fields = new FieldSet(
- new LiteralField(
- 'ReportTitle',
- "<h3>{$this->title()}</h3>"
- ),
- new LiteralField('ReportDescription', $this->description()),
- new LiteralField('ReportContent', $this->getHTML())
- );
- return $fields;
- }
-
- function getHTML() {
- $viewAnalytics = _t('AnalyticsReport.VIEWANALYTICS','View Google Analytics');
- $setupAnalytics = _t('AnalyticsReport.SETUPANALYTICS','Setup Google Analytics');
- $result = <<<ENDRESULT
- <ul class="{$this->class}"
- <li><a href="#" onclick='viewGoogleAnalytics();'>{$viewAnalytics}</a></li>
- <li><a href="#" onclick='setupGoogleAnalytics();'>{$setupAnalytics}</a></li>
- </ul>
-ENDRESULT;
- return $result;
- }
-}
-
-?>
@@ -1,48 +0,0 @@
-<?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 updateCMSFields($fields) {
- $fields->addFieldToTab('Root.GoogleAnalytics',
- new HeaderField("info", "Paste Google's code below to collect statistics for this site", 2));
- $fields->addFieldToTab('Root.GoogleAnalytics',
- new TextareaField("GoogleAnalyticsScript", "Analytics Tracking", 20, 5)
- );
- $codefield = new TextField("GoogleAnalyticsCode", "Analytics Tracking ID (will be calculated automatically)");
- $codefield->setReadonly(true);
- $codefield = $codefield->performReadonlyTransformation();
- $fields->addFieldToTab('Root.GoogleAnalytics', $codefield);
- }
-
- function onBeforeWrite() {
- parent::onBeforeWrite();
- $script = $this->owner->GoogleAnalyticsScript;
- $matches = array();
- $pattern = '/_gat\._getTracker \( " (UA[^"]+) "/x';
- if (empty($script)) {
- return;
- }
- if (preg_match($pattern, $script, &$matches)) {
- $this->owner->GoogleAnalyticsCode = $matches[1];
- $string = 'var pageTracker = _gat._getTracker("' . $matches[1] . '");';
- $string .= "\npageTracker._trackPageview();\n";
- $this->owner->GoogleAnalyticsScript = $string;
- } else {
- user_error("googleanalytics module: couldn't match '$pattern' against '$script'. Please update the googleanalytics module");
- }
- }
-
-}
View
@@ -1,91 +0,0 @@
-<?php
-/**
- * CrawlerStats is the class that keeps track of the index status of pages.
- * It is a data object, and encompasses a table contain pages, and when they were
- * last indexed by search engines.
- *
- * Other engines can be added by modifying the spiders property in the form of
- * name => user-agent-string
- *
- * @package googleanalytics
- */
-class CrawlerStats extends DataObject {
- static $db = array(
- "Page" => "Varchar",
- "Data" => "Varchar");
-
- static $defaults = array(
- "Data" => "a:0:{}");
-
- static $spiders = array(
- "Google" => "Googlebot",
- "Yahoo" => "Yahoo",
- "MSN" => "msnbot");
-
- /**
- * UpdateCrawls should be called on a page load with the user-agent loading that page
- * if the user agent matches one of the known spiders, then it's crawler entry will be
- * updated appropriately.
- */
- public function UpdateCrawls($agent) {
- $data = unserialize($this->Data);
-
- $index = 0;
- $updated = false;
- foreach (self::$spiders as $pattern) {
- if(stristr($agent,$pattern)) {
- $data[$index] = time();
- $updated = true;
- }
- ++$index;
- }
- $this->Data = serialize($data);
-
- return $updated;
- }
-
- /**
- * PrintCrawls will return an html table containing the crawler status of the current
- * data object.
- *
- * @param since the last time the page was updated
- * @return an html table with a human-readable description of the pages index status
- */
- public function printCrawls($since) {
- $result = "<h2>This page has been indexed by the following search engines:</h2>";
- $result .= "<table cellpadding=2><tr><th>Engine</th><th>Status</th><th>Last Indexed</th></tr>";
- $data = unserialize($this->Data);
-
- foreach (array_keys(self::$spiders) as $index => $display) {
- 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>";
- if (strtotime($since) < $data[$index]) {
- $result .= "<img src = 'googleanalytics/images/tick.png' alt='Current Version' style='float:left;padding-right:2px;'/> Latest Version";
- } elseif ($data[$index]) {
- $result .= "<img src = 'googleanalytics/images/cross.png' alt='Old Version' style='float:left;padding-right:2px;'/> Old Version";
- }
- $result .= "<td>" . date("F j, g:i a",$data[$index]) . "</td>";
- $result .= "</td></tr>";
- }
-
- return $result . "</table>";
- }
-
- /**
- * Provides a CrawlerStats object for a page
- * if the page has never been crawled, a new object is created
- */
- public static function GetFor($page) {
- $try = DataObject::get_one("CrawlerStats","\"Page\" = '$page'");
- if($try)
- return $try;
-
- $new = new CrawlerStats();
- $new->Page = $page;
- return $new;
- }
-}
-
-?>
@@ -1,30 +0,0 @@
-<?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();
- }
-}
-
-?>
View
@@ -1,69 +0,0 @@
-<?php
-/**
- * This class ties the module into the main interface.
- * _config.php allows this class to modify the cms fields of all pages
- * and to call a function on every page load.
- *
- * @package googleanalytics
- */
-class GoogleAnalytics extends DataObjectDecorator {
-
- /**
- * Returns a description of the indexing status of the current page.
- */
- static function getIndexStatus() {
- $controller = Controller::curr();
- if(method_exists($controller,"currentPage") && method_exists($controller->currentPage(),"ElementName"))
- $page = $controller->currentPage()->ElementName();
- else
- $page = "unknown";
-
- $indexResults = DataObject::get_one("CrawlerStats","\"Page\" = '$page'");
-
- $lastVersion = strtotime("now");
-
- if($page != "unknown") {
- $lastVersion = $controller->currentPage()->LastEdited;
- }
-
- if(!$indexResults)
- return "This Page has not been crawled by any search engines";
-
- return $indexResults->printCrawls($lastVersion);
- }
-
- /**
- * Modifies the main cms for all pages to include a description of their status in search engines
- */
- public function updateCMSFields($fields) {
- if (!$fields->fieldByName('Root.Reports')) {
- $fields->addFieldToTab("Root",new TabSet('Reports'));
- }
- $fields->addFieldToTab("Root.Reports.Index",new LiteralField("IndexStatus",self::getIndexStatus()));
-
- return $fields;
- }
-
- /**
- * Add google analytics javascript code to a page
- */
- static function addAnalytics() {
- $config = DataObject::get_one('SiteConfig');
- if (!$config) {
- return;
- }
- $script = $config->GoogleAnalyticsScript;
- if (empty($script)) {
- return;
- }
- Requirements::insertHeadTags("<script src='http://www.google-analytics.com/ga.js' type='text/javascript'></script>", "GA");
- Requirements::customScript($script, "ga");
- }
-
- function link($action = null) {
- return "admin/sidereport/GoogleAnalytics/?mode=$action";
- }
-
-
-}
-?>
Oops, something went wrong.

0 comments on commit 4c1e8ed

Please sign in to comment.