Skip to content

Commit

Permalink
Merge pull request #10 from ARNHOE/master
Browse files Browse the repository at this point in the history
SS3 fixes & language added
  • Loading branch information
Julian Seidenberg committed Dec 3, 2012
2 parents 3db433b + 9933c62 commit 9f9329a
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 194 deletions.
110 changes: 55 additions & 55 deletions code/GoogleAnalyzer.php
@@ -1,56 +1,56 @@
<?php

/**
* @package googleanalytics
*/
class GoogleAnalyzer extends DataExtension {

static public $sapphire_version;

// credentials for the Google Analytics API
public static $profile_id;
public static $email;
public static $password;

static $has_many = array(
'Events' => 'GoogleLogEvent',
);

/**
* 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:
* the Google Analytics profile ID or
* 'SiteConfig' for using the SiteConfig to configure this value
* @param $email String email address of the account to use (can be left blank if configured with SiteConfig)
* @param $password String password for the above account (can be left blank if configured with SiteConfig)
**/
public static function activate($profile = 'SiteConfig', $email = null, $password = null) {
switch($profile) {
case 'SiteConfig': Object::add_extension('SiteConfig', 'GoogleConfig'); break;
default:
self::$profile_id = $profile;
self::$email = $email;
self::$password = $password;
}

Object::add_extension('SiteTree', 'GoogleAnalyzer');
}

public function updateCMSFields(FieldList $fields) {
$fields->addFieldToTab('Root', new Tab('GoogleAnalytics', 'Google Analytics'));
$fields->addFieldToTab("Root.GoogleAnalytics", new TabSet('Stats'));
$fields->addFieldToTab('Root.GoogleAnalytics.Stats', new Tab('Performance', 'Performance'));
$fields->addFieldToTab("Root.GoogleAnalytics.Stats.Performance", new GooglePerformanceChart($this->owner));
}
<?php

/**
* @package googleanalytics
*/
class GoogleAnalyzer extends DataExtension {

static public $sapphire_version;

// credentials for the Google Analytics API
public static $profile_id;
public static $email;
public static $password;

static $has_many = array(
'Events' => 'GoogleLogEvent',
);

/**
* 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:
* the Google Analytics profile ID or
* 'SiteConfig' for using the SiteConfig to configure this value
* @param $email String email address of the account to use (can be left blank if configured with SiteConfig)
* @param $password String password for the above account (can be left blank if configured with SiteConfig)
**/
public static function activate($profile = 'SiteConfig', $email = null, $password = null) {

switch($profile) {
case 'SiteConfig': Object::add_extension('SiteConfig', 'GoogleConfig'); break;
default:
self::$profile_id = $profile;
self::$email = $email;
self::$password = $password;
}

Object::add_extension('SiteTree', 'GoogleAnalyzer');
}

public function updateCMSFields(FieldList $fields) {
$fields->addFieldToTab('Root', Tab::create("GoogleAnalytics", _t('GoogleAnalyzer.TABTITLE',"Google Analytics")));
$fields->addFieldToTab("Root.GoogleAnalytics", TabSet::create("Stats", _t('GoogleAnalyzer.STATS',"Stats")));
$fields->addFieldToTab('Root.GoogleAnalytics.Stats', Tab::create("Performance", _t('GoogleAnalyzer.PERFORMANCE',"Performance")));
$fields->addFieldToTab("Root.GoogleAnalytics.Stats.Performance", new GooglePerformanceChart($this->owner));
}
}
88 changes: 44 additions & 44 deletions code/GoogleConfig.php
@@ -1,45 +1,45 @@
<?php

/**
* @package googleanalytics
*/
class GoogleConfig extends DataExtension {

static $db = array(
'GoogleAnalyticsCode' => 'Varchar',
'GoogleAnalyticsProfileId' => 'Varchar',
'GoogleAnalyticsEmail' => 'Varchar',
'GoogleAnalyticsPassword' => 'Varchar',
);

public function updateCMSFields(FieldList $fields) {

$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 PasswordField('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;
}
}
<?php

/**
* @package googleanalytics
*/
class GoogleConfig extends DataExtension {

static $db = array(
'GoogleAnalyticsCode' => 'Varchar',
'GoogleAnalyticsProfileId' => 'Varchar(255)',
'GoogleAnalyticsEmail' => 'Varchar',
'GoogleAnalyticsPassword' => 'Varchar',
);

public function updateCMSFields(FieldList $fields) {

$fields->addFieldToTab("Root", new Tab('GoogleAnalytics'));
$fields->addFieldToTab('Root.GoogleAnalytics', TextField::create("GoogleAnalyticsCode")->setTitle(_t('GoogleConfig.CODE',"Google Analytics Code"))->setRightTitle("(UA-XXXXXX-X)"));
$fields->addFieldToTab('Root.GoogleAnalytics', TextField::create("GoogleAnalyticsProfileId")->setTitle(_t('GoogleConfig.PROFILEID',"Google Analytics Profile ID"))->setRightTitle(_t('GoogleConfig.PROFILEIDEXPLANATION','Hidden in the URL parameter "id" of the "View Report" link inside Google Analytics')));
$fields->addFieldToTab('Root.GoogleAnalytics', TextField::create("GoogleAnalyticsEmail")->setTitle(_t('GoogleConfig.EMAIL',"Google Analytics Email"))->setRightTitle(_t('GoogleConfig.EMAILEXPLANATION',"The email address of the Google Analytics account to use")));
$fields->addFieldToTab('Root.GoogleAnalytics', PasswordField::create("GoogleAnalyticsPassword")->setTitle(_t('GoogleConfig.PASSWORD',"Google Analytics Password"))->setRightTitle(_t('GoogleConfig.PASSWORDEXPLANATION',"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;
}
}

}
88 changes: 44 additions & 44 deletions code/GoogleDataController.php
@@ -1,45 +1,45 @@
<?php

class GoogleDataController extends Controller {

function performance($request) {
$markers = array();

$q = substr(GoogleAnalyzer::get_sapphire_version(), 0, 3) == '2.3' ? '`' : '"';

$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(GoogleConfig::get_google_config('profile'), GoogleConfig::get_google_config('email'), GoogleConfig::get_google_config('password'));
$data = $store->fetchPerformance(array(
'dimensions' => 'ga:date',
'metrics' => 'ga:visits,ga:pageviews',
'sort' => '-ga:date',
'filters' => $filters,
));

return json_encode(array('series' => $data, 'markers' => $markers));
}

<?php

class GoogleDataController extends Controller {

function performance($request) {

$markers = array();

$q = substr(GoogleAnalyzer::get_sapphire_version(), 0, 3) == '2.3' ? '`' : '"';

$metrics = $request->requestVar('metrics');

$filters = null;
$eventfiltersql = "{$q}GoogleLogEvent{$q}.{$q}PageID{$q} = 0";
$page = SiteTree::get()->byID((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(GoogleConfig::get_google_config('profile'), GoogleConfig::get_google_config('email'), GoogleConfig::get_google_config('password'));


$data = $store->fetchPerformance(array(
'dimensions' => 'ga:date',
'metrics' => 'ga:visits,ga:pageviews',
'sort' => '-ga:date',
'filters' => $filters,
));

return json_encode(array('series' => $data, 'markers' => $markers));
}

}
42 changes: 21 additions & 21 deletions code/GooglePerformanceChart.php
@@ -1,22 +1,22 @@
<?php

class GooglePerformanceChart extends Compositefield {

protected $page;

function __construct($page = null) {
parent::__construct();
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($properties = array()) {
return $this->renderWith('GooglePerformanceChart');
}
function PageID() {
if($this->page) return $this->page->ID;
}
<?php

class GooglePerformanceChart extends Compositefield {

protected $page;

function __construct($page = null) {
parent::__construct();
switch(true) {
case $page instanceof SiteTree: $this->page = $page; break;
case is_numeric($page): $page = SiteTree::get()->byID((int)$page); break;
}
}

function FieldHolder($properties = array()) {
return $this->renderWith('GooglePerformanceChart');
}

function PageID() {
if($this->page) return $this->page->ID;
}
}
40 changes: 18 additions & 22 deletions code/GoogleReport.php
@@ -1,23 +1,19 @@
<?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;
}
<?php

class GoogleReport extends SS_Report {

function description() {
return "add description";
}

function getCMSFields() {
$fields = new FieldList();

$chart = new GooglePerformanceChart();

$fields->push(new LiteralField('ReportTitle', "<h3>{" . _t('GoogleReport.REPORTTITLE',"Google Analytics") ."}</h3>"));
$fields->push(new LiteralField('ReportDescription', $chart->renderWith('GooglePerformanceChart')));

return $fields;
}
}
15 changes: 15 additions & 0 deletions lang/en.yml
@@ -0,0 +1,15 @@
en:
GoogleConfig:
CODE: 'Google Analytics Code'
PROFILEID: 'Google Analytics Profile ID'
PROFILEIDEXPLANATION: 'Hidden in the URL parameter "id" of the "View Report" link inside Google Analytics'
EMAIL: 'Google Analytics Email'
EMAILEXPLANATION: 'The email address of the Google Analytics account to use'
PASSWORD: 'Google Analytics Password'
PASSWORDEXPLANATION: 'Password of the Google Analytics account'
GoogleAnalyzer:
TABTITLE: 'Google Analytics'
STATS: 'Stats'
PERFORMANCE: 'Performance'
GoogleReport:
REPORTTITLE: 'Google Analytics'

0 comments on commit 9f9329a

Please sign in to comment.