Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ENHANCEMENT: get performance for the entire site, not only per page, …

…APICHANGE: move getgoogleconfig to googleconfig, ENHANCEMENT: add google report
  • Loading branch information...
commit cd399cde8228d27eea8f275259e36061130f4c03 1 parent 5a60448
Andreas Piening authored
View
5 _config.php
@@ -1 +1,6 @@
<?php
+
+GoogleLogger::activate('SiteConfig');
+GoogleAnalyzer::activate('SiteConfig');
+
+// SS_Report::register("ReportAdmin", "AnalyticsReport");
View
22 code/GoogleAnalyzer.php
@@ -44,30 +44,10 @@ public static function activate($profile = 'SiteConfig', $email = null, $passwor
self::$email = $email;
self::$password = $password;
}
-
+ SS_Report::register("ReportAdmin", "GoogleReport");
Object::add_extension('SiteTree', 'GoogleAnalyzer');
}
- /**
- * Return various configuration values
- *
- * @param $key String:
- * 'profile' the Google Analytics profile id or
- * 'email' the Google Analytics account's email address or
- * 'password' the password for the above Google Analytics account
- * @return String the config value
- **/
- public function getGoogleConfig($key) {
- 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::$profile_id;
- case 'email': return !empty($config) && $config->GoogleAnalyticsEmail ? $config->GoogleAnalyticsEmail : GoogleAnalyzer::$email;
- case 'password': return !empty($config) && $config->GoogleAnalyticsPassword ? $config->GoogleAnalyticsPassword : GoogleAnalyzer::$password;
- }
- }
-
public function updateCMSFields(FieldSet $fields) {
$fields->addFieldToTab('Root', new Tab('GoogleAnalytics', 'Google Analytics'));
View
25 code/GoogleConfig.php
@@ -14,10 +14,33 @@ function extraStatics() {
}
public function updateCMSFields(FieldSet $fields) {
- $fields->addFieldToTab('Root', new Tab('GoogleAnalytics', 'Google Analytics'));
+
+ $fields->addFieldToTab("Root", new Tab('GoogleAnalytics'));
$fields->addFieldToTab('Root.GoogleAnalytics', new TextField('GoogleAnalyticsCode', 'Google Analytics Code (UA-XXXXXX-X)'));
$fields->addFieldToTab('Root.GoogleAnalytics', new TextField('GoogleAnalyticsProfileId', 'Google Analytics Profile ID (hidden in the URL parameter "id" of the "View Report" link inside Google Analytics)'));
$fields->addFieldToTab('Root.GoogleAnalytics', new TextField('GoogleAnalyticsEmail', 'GoogleAnalyticsEmail (the email address of the Google Analytics account to use)'));
$fields->addFieldToTab('Root.GoogleAnalytics', new TextField('GoogleAnalyticsPassword', 'Google Analytics Password (the password for the above account)'));
}
+
+ /**
+ * Return various configuration values
+ *
+ * @param $key String:
+ * 'profile' the Google Analytics profile id or
+ * 'email' the Google Analytics account's email address or
+ * 'password' the password for the above Google Analytics account
+ * @return String the config value
+ **/
+ public static function get_google_config($key) {
+ if(class_exists('SiteConfig') && Object::has_extension('SiteConfig', 'GoogleConfig')) {
+ $config = SiteConfig::current_site_config();
+ }
+ switch($key) {
+ case 'code': return !empty($config) && $config->GoogleAnalyticsCode ? $config->GoogleAnalyticsCode : GoogleLogger::$google_analytics_code;
+ 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
37 code/GoogleDataController.php
@@ -4,30 +4,39 @@ class GoogleDataController extends Controller {
function performance($request) {
- $page = DataObject::get_by_id('SiteTree', (int)$request->param('ID'));
- $metrics = $request->requestVar('metrics');
+ $markers = array();
$q = substr(GoogleAnalyzer::get_sapphire_version(), 0, 3) == '2.3' ? '`' : '"';
- $markers = array();
- $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.');
- $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.');
+
+ $metrics = $request->requestVar('metrics');
+
+ $filters = null;
+ $eventfiltersql = "{$q}GoogleLogEvent{$q}.{$q}PageID{$q} = 0";
+ $page = DataObject::get_by_id('SiteTree', (int)$request->param('ID'));
+ if($page) {
+ $url = trim($page->Link(), '/');
+ if(!empty($url)) $url .= '/';
+ $filters = 'ga:pagePath==/' . $url;
+ $eventfiltersql .= " OR {$q}GoogleLogEvent{$q}.{$q}PageID{$q} = " . (int)$page->ID;
+ $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() as $version) {
+ $markers[] = array(strtotime($version->LastEdited) * 1000, 'Updated', 'Long descr.');
+ }
}
+
+ $events = DataObject::get('GoogleLogEvent', $eventfiltersql);
+ if($events) foreach($events as $event) $markers[] = array(strtotime($event->Created) * 1000, $event->Title, 'Long descr.');
- $store = new GoogleDataStore($page->getGoogleConfig('profile'), $page->getGoogleConfig('email'), $page->getGoogleConfig('password'));
+ $store = new GoogleDataStore(GoogleConfig::get_google_config('profile'), GoogleConfig::get_google_config('email'), GoogleConfig::get_google_config('password'));
- $url = trim($page->Link(), '/');
- if(!empty($url)) $url .= '/';
$data = $store->fetchPerformance(array(
'dimensions' => 'ga:date',
'metrics' => 'ga:visits,ga:pageviews',
'sort' => '-ga:date',
- 'filters' => 'ga:pagePath==/' . $url,
+ 'filters' => $filters,
));
return json_encode(array('series' => $data, 'markers' => $markers));
View
21 code/GoogleLogger.php
@@ -35,23 +35,6 @@ public static function activate($code = null) {
if(substr(GoogleAnalyzer::get_sapphire_version(), 0, 3) == '2.3') Director::add_callback(array("GoogleLogger","onAfterInit23"));
}
- /**
- * Return various configuration values
- *
- * @param $key set:
- * String 'code' the Google Analytics code to be used in the JS snippet or
- * @return String the config value
- *
- **/
- protected function getGoogleConfig($key) {
- if(class_exists('SiteConfig') && Object::has_extension('SiteConfig', 'GoogleConfig')) {
- $config = SiteConfig::current_site_config();
- }
- switch($key) {
- case 'code': return !empty($config) && $config->GoogleAnalyticsCode ? $config->GoogleAnalyticsCode : GoogleLogger::$google_analytics_code;
- }
- }
-
public function onAfterInit23() {
if(Controller::curr() instanceof ContentController) Controller::curr()->onAfterInit();
}
@@ -59,8 +42,8 @@ public function onAfterInit23() {
public function onAfterInit() {
// include the JS snippet into the frontend page markup
- if($this->getGoogleConfig('code')) {
- $googleanalyticsjssnippet = new ArrayData(array('GoogleAnalyticsCode' => $this->getGoogleConfig('code')));
+ if(GoogleConfig::get_google_config('code')) {
+ $googleanalyticsjssnippet = new ArrayData(array('GoogleAnalyticsCode' => GoogleConfig::get_google_config('code')));
Requirements::customScript($googleanalyticsjssnippet->renderWith('GoogleAnalyticsJSSnippet'));
}
View
10 code/GooglePerformanceChart.php
@@ -4,10 +4,12 @@ class GooglePerformanceChart extends Compositefield {
protected $page;
- function __construct($page) {
+ function __construct($page = null) {
parent::__construct();
- if(!($page instanceof SiteTree)) $page = DataObject::get_by_id('SiteTree', $page);
- $this->page = $page;
+ switch(true) {
+ case $page instanceof SiteTree: $this->page = $page; break;
+ case is_numeric($page): $page = DataObject::get_by_id('SiteTree', (int)$page); break;
+ }
}
function FieldHolder() {
@@ -15,6 +17,6 @@ function FieldHolder() {
}
function PageID() {
- return $this->page->ID;
+ if($this->page) return $this->page->ID;
}
}
View
23 code/GoogleReport.php
@@ -0,0 +1,23 @@
+<?php
+
+class GoogleReport extends SS_Report {
+
+ function title() {
+ return "Google Analytics";
+ }
+
+ function description() {
+ return "add description";
+ }
+
+ function getCMSFields() {
+ $fields = new FieldSet();
+
+ $chart = new GooglePerformanceChart();
+
+ $fields->push(new LiteralField('ReportTitle', "<h3>{$this->title()}</h3>"));
+ $fields->push(new LiteralField('ReportDescription', $chart->renderWith('GooglePerformanceChart')));
+
+ return $fields;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.