Skip to content

Commit

Permalink
ENHANCEMENT: get performance for the entire site, not only per page, …
Browse files Browse the repository at this point in the history
…APICHANGE: move getgoogleconfig to googleconfig, ENHANCEMENT: add google report
  • Loading branch information
Andreas Piening committed Nov 10, 2010
1 parent 5a60448 commit cd399cd
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 59 deletions.
5 changes: 5 additions & 0 deletions _config.php
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
<?php

GoogleLogger::activate('SiteConfig');
GoogleAnalyzer::activate('SiteConfig');

// SS_Report::register("ReportAdmin", "AnalyticsReport");
22 changes: 1 addition & 21 deletions code/GoogleAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
25 changes: 24 additions & 1 deletion code/GoogleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

}
37 changes: 23 additions & 14 deletions code/GoogleDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
21 changes: 2 additions & 19 deletions code/GoogleLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,15 @@ 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();
}

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'));
}

Expand Down
10 changes: 6 additions & 4 deletions code/GooglePerformanceChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ 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() {
return $this->renderWith('GooglePerformanceChart');
}

function PageID() {
return $this->page->ID;
if($this->page) return $this->page->ID;
}
}
23 changes: 23 additions & 0 deletions code/GoogleReport.php
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit cd399cd

Please sign in to comment.