Skip to content

Commit

Permalink
API CHANGE: Upgrade to new gapi interface, remove set_account(), use …
Browse files Browse the repository at this point in the history
…yaml and key file
  • Loading branch information
Uncle Cheese committed Jul 29, 2015
1 parent d677a02 commit 66cfebb
Show file tree
Hide file tree
Showing 5 changed files with 622 additions and 571 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,12 @@ eg;

## Note on Google Analytics Panel

You need to add your Google Analytics config information to the project _config.php:
```php
DashboardGoogleAnalyticsPanel::set_account($email, $password, $profileID);
You need to add your Google Analytics config information to the project config.php:
```yaml
DashboardGoogleAnalyticsPanel:
email: [XXXXX]@developer.gserviceaccount.com
profile: 123456
key_file_path: google_oauth.p12
```
To locate your profile ID, visit the Google Analytics website, login and select the website. At the end of the URL will be fragment similar to this:
```
Expand All @@ -160,4 +163,6 @@ To locate your profile ID, visit the Google Analytics website, login and select
```
The 8 digits that follow the "p" are your profile ID. In the example above, this would be 12345678.

NOTE: To use the Google Analytics panel, you have to enable access for less secure apps in the account permissions section of [https://www.google.com/settings/security](https://www.google.com/settings/security).
NOTE: To use the Google Analytics panel, you have to enable access for less secure apps in the account permissions section of [https://www.google.com/settings/security](https://www.google.com/settings/security).

For more information about settting up a developer account and obtaining a key file, visit https://github.com/erebusnz/gapi-google-analytics-php-interface#instructions-for-setting-up-a-google-service-account-for-use-with-gapi
6 changes: 5 additions & 1 deletion _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ SiteConfig:
[DashboardSiteConfig]
GridFieldDetailForm_ItemRequest:
extensions:
[DashboardItemEditForm]
[DashboardItemEditForm]
DashboardGoogleAnalyticsPanel:
email: ''
profile: ''
key_file_path: google_oauth.p12
58 changes: 32 additions & 26 deletions code/panels/DashboardGoogleAnalyticsPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,17 @@ class DashboardGoogleAnalyticsPanel extends DashboardPanel {



protected static $gapi_email;



protected static $gapi_password;



protected static $gapi_profile;



/**
* @var gapi A stored instantiation of the Google Analytics API object
*/
protected $gapi;


/**
* Stores any exceptions coming from GAPI for easy rendering on the template
* @var string
*/
protected $error;


/**
Expand All @@ -67,17 +60,10 @@ public static function seconds_to_minutes($seconds) {


/**
* Sets the username and password for the Google Analytics account
*
* @param string The email address of the account
* @param string The password of the account
* @param string The profile ID of the account
*
* DEPRECATED: Sets the username and password for the Google Analytics account
*/
public static function set_account($email, $password, $profile) {
self::$gapi_email = $email;
self::$gapi_password = $password;
self::$gapi_profile = $profile;
throw new Exception("DashboardGoogleAnalyticsPanel::set_account() is no longer supported. Use the config settings 'email', 'profile', and 'key_file_path' in the DashboardGoogleAnalyticsPanel config. More info on oauth key: https://github.com/erebusnz/gapi-google-analytics-php-interface#instructions-for-setting-up-a-google-service-account-for-use-with-gapi");
}


Expand All @@ -94,6 +80,10 @@ public function getLabel() {
}


public function getError() {
return $this->error;
}


/**
* Gets the title for this panel
Expand All @@ -112,12 +102,17 @@ public function getDescription() {
*
* @return gapi
*/
public function api() {
public function api() {
if(!$this->gapi) {
try {
$this->gapi = new gapi(self::$gapi_email, self::$gapi_password);
$this->gapi = new gapi(
self::config()->email,
Director::getAbsFile(self::config()->key_file_path)
);
}
catch(Exception $e) {
$this->error = $e->getMessage();

return $this->gapi;
}
}
Expand Down Expand Up @@ -231,7 +226,14 @@ public function IsConnected() {
* @return bool
*/
public function IsConfigured() {
return self::$gapi_email && self::$gapi_password && self::$gapi_profile;
$c = self::config();

return (
$c->email &&
$c->key_file_path &&
$c->profile &&
Director::fileExists($c->key_file_path)
);
}


Expand Down Expand Up @@ -322,7 +324,7 @@ public function Chart() {
if(!$this->isValid()) return false;
try {
$this->api()->requestReportData(
self::$gapi_profile,
self::config()->profile,
array('date'),
array('pageviews'),
'date',
Expand All @@ -331,6 +333,8 @@ public function Chart() {
);
}
catch(Exception $e) {
$this->error = $e->getMessage();

return false;
}

Expand Down Expand Up @@ -375,7 +379,7 @@ public function PageResults() {
if(!$this->isValid()) return false;
try {
$this->api()->requestReportData(
self::$gapi_profile,
self::config()->profile,
'pagePath',
array('pageviews', 'uniquePageviews', 'exitRate', 'avgTimeOnPage', 'entranceBounceRate'),
null,
Expand All @@ -384,6 +388,8 @@ public function PageResults() {
);
}
catch(Exception $e) {
$this->error = $e->getMessage();

return false;
}
$set = ArrayList::create(array());
Expand Down
Loading

0 comments on commit 66cfebb

Please sign in to comment.