Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ENHANCEMENT: added support for 2.3

  • Loading branch information...
commit bef9ed4016e8939539e45ebdcd4a38203388b7fb 1 parent 4c1e8ed
Andreas Piening authored
View
4 _config.php
@@ -1,6 +1,6 @@
<?php
-GoogleLogger::activate('SiteConfig');
-GoogleAnalyzer::activate('SiteConfig');
+GoogleLogger::activate('UA-84547-4');
+GoogleAnalyzer::activate('13358486', 'andreas@silverstripe.com', 's=7bNT10');
// SS_Report::register("ReportAdmin", "AnalyticsReport");
View
22 code/GoogleAnalyzer.php
@@ -2,8 +2,10 @@
class GoogleAnalyzer extends DataObjectDecorator {
+ static public $sapphire_version;
+
// credentials for the Google Analytics API
- static protected $pofile_id;
+ static protected $profile_id;
static protected $email;
static protected $password;
@@ -16,6 +18,15 @@ function extraStatics() {
}
/**
+ * for legacy reasons
+ * @return String, version number, e.g. 2.4 or 2.3.6
+ */
+ public static function get_sapphire_version() {
+ if(self::$sapphire_version) return self::$sapphire_version;
+ return method_exists('SiteTree', 'nested_urls') ? '2.4' : '2.3';
+ }
+
+ /**
* Activate the GoogleAnalyzer
*
* @param $profile String:
@@ -28,7 +39,10 @@ public static function activate($profile = 'SiteConfig', $email = null, $passwor
switch($profile) {
case 'SiteConfig': Object::add_extension('SiteConfig', 'GoogleConfig'); break;
- default: self::$profile_id = $profile; self::$email = $email; self::$password = $password;
+ default:
+ self::$profile_id = $profile;
+ self::$email = $email;
+ self::$password = $password;
}
Object::add_extension('SiteTree', 'GoogleAnalyzer');
@@ -44,11 +58,11 @@ public static function activate($profile = 'SiteConfig', $email = null, $passwor
* @return String the config value
**/
public function getGoogleConfig($key) {
- if(Object::has_extension('SiteConfig', 'GoogleConfig')) {
+ if(class_exists('SiteConfig') && Object::has_extension('SiteConfig', 'GoogleConfig')) {
$config = SiteConfig::current_site_config();
}
switch($key) {
- case 'profile': return !empty($config) && $config->GoogleAnalyticsProfileId ? $config->GoogleAnalyticsProfileId : GoogleAnalyzer::$pofile_id;
+ case 'profile': return !empty($config) && $config->GoogleAnalyticsProfileId ? $config->GoogleAnalyticsProfileId : GoogleAnalyzer::$profile_id;
case 'email': return !empty($config) && $config->GoogleAnalyticsEmail ? $config->GoogleAnalyticsEmail : GoogleAnalyzer::$email;
case 'password': return !empty($config) && $config->GoogleAnalyticsPassword ? $config->GoogleAnalyticsPassword : GoogleAnalyzer::$password;
}
View
10 code/GoogleDataController.php
@@ -7,16 +7,20 @@ function performance($request) {
$page = DataObject::get_by_id('SiteTree', (int)$request->param('ID'));
$metrics = $request->requestVar('metrics');
+ $q = substr(GoogleAnalyzer::get_sapphire_version(), 0, 3) == '2.3' ? '`' : '"';
$markers = array();
- $events = DataObject::get('GoogleLogEvent', "\"GoogleLogEvent\".\"PageID\" = 0 OR \"GoogleLogEvent\".\"PageID\" = " . (int)$page->ID);
+ $events = DataObject::get('GoogleLogEvent', "{$q}GoogleLogEvent{$q}.{$q}PageID{$q} = 0 OR {$q}GoogleLogEvent{$q}.{$q}PageID{$q} = " . (int)$page->ID);
if($events) foreach($events as $event) $markers[] = array(strtotime($event->Created) * 1000, $event->Title, 'Long descr.');
- foreach($page->allVersions('"WasPublished" = 1 AND ' . DB::getConn()->datetimeDifferenceClause('"LastEdited"', date('Y-m-d 23:59:59', strtotime('-1 Year'))) . ' > 0') as $version) {
+ $allversions = $q == '"' ?
+ '"WasPublished" = 1 AND ' . DB::getConn()->datetimeDifferenceClause('"LastEdited"', date('Y-m-d 23:59:59', strtotime('-1 Year'))) . ' > 0' :
+ "{$q}WasPublished{$q} = 1 AND {$q}LastEdited{$q} > '" . date('Y-m-d 23:59:59', strtotime('-1 Year')) . "'";
+ foreach($page->allVersions($allversions) as $version) {
$markers[] = array(strtotime($version->LastEdited) * 1000, 'Updated', 'Long descr.');
}
$store = new GoogleDataStore($page->getGoogleConfig('profile'), $page->getGoogleConfig('email'), $page->getGoogleConfig('password'));
- $url = trim($page->Linky(), '/');
+ $url = trim($page->Link(), '/');
if(!empty($url)) $url .= '/';
$data = $store->fetchPerformance(array(
View
5 code/GoogleDataStore.php
@@ -54,12 +54,15 @@ public function setDefaults(array $defaults) {
**/
public function fetchData($query) {
+ require_once('../googleanalytics/thirdparty/analytics_api.php');
+
// prep query params
$query = array_merge($this->query, $query);
// get cache object
+ $q = substr(GoogleAnalyzer::get_sapphire_version(), 0, 3) == '2.3' ? '`' : '"';
$hash = hash('md5', serialize(array_merge($this->setup, $query)));
- $cache = DataObject::get_one('GoogleCachedQuery', "\"GoogleCachedQuery\".\"Hash\" = '$hash'");
+ $cache = DataObject::get_one('GoogleCachedQuery', "{$q}GoogleCachedQuery{$q}.{$q}Hash{$q} = '$hash'");
if(!$cache) $cache = new GoogleCachedQuery(array('Hash' => $hash));
// poll fresh data if cached query is empty or outdated
View
10 code/GoogleLogger.php
@@ -29,8 +29,10 @@ public static function activate($code = null) {
case 'SiteConfig': Object::add_extension('SiteConfig', 'GoogleConfig'); break;
default: self::$google_analytics_code = $code;
}
-
+
Object::add_extension('ContentController', 'GoogleLogger');
+
+ if(substr(GoogleAnalyzer::get_sapphire_version(), 0, 3) == '2.3') Director::add_callback(array("GoogleLogger","onAfterInit23"));
}
/**
@@ -42,7 +44,7 @@ public static function activate($code = null) {
*
**/
protected function getGoogleConfig($key) {
- if(Object::has_extension('SiteConfig', 'GoogleConfig')) {
+ if(class_exists('SiteConfig') && Object::has_extension('SiteConfig', 'GoogleConfig')) {
$config = SiteConfig::current_site_config();
}
switch($key) {
@@ -50,6 +52,10 @@ protected function getGoogleConfig($key) {
}
}
+ public function onAfterInit23() {
+ if(Controller::curr() instanceof ContentController) Controller::curr()->onAfterInit();
+ }
+
public function onAfterInit() {
// include the JS snippet into the frontend page markup
View
3  docs/README.md
@@ -10,7 +10,8 @@ The Google Logger injects the google analytics javascript snippet into your sour
The Analyzer adds the GA UI to your CMS
## Requirements
- * SilverStripe 2.4 or newer
+
+ * SilverStripe 2.3 or newer (for 2.3 use the hardcode activation since SiteConfig doesn't exist in 2.3)
## DB Adapter Support
* MySQL
Please sign in to comment.
Something went wrong with that request. Please try again.