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)); }
/** * fetchData is just pulling in raw data like it is coming form the API and is supposed to be a data provider * for functions that return certain formats like GoogleDataStore::fetchPerformance() * * @param Query assoc Array containing all necessary query parameters that are not already set, @see GoogleDataStore::$query * @return Query Array containing raw data retrieved from the analytics_api.php **/ 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', "{$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 if (date('Y-m-d', strtotime($cache->LastEdited)) != date('Y-m-d')) { $api = new analytics_api(); if ($api->login($this->setup['email'], $this->setup['password'])) { // $xml = $api->call($this->setup['url']); $data = $api->data('ga:' . $this->setup['ids'], $query['dimensions'], $query['metrics'], $query['sort'], $query['start-date'], $query['end-date'], $query['max-results'], $query['start-index'], $query['filters']); $cache->Data = serialize($data); $cache->write(); } else { trigger_error('ERROR: failed to connect remote server.'); } } else { $data = unserialize($cache->Data); } return $data; }
/** * 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': SiteConfig::add_extension('GoogleConfig'); break; default: self::$profile_id = $profile; self::$email = $email; self::$password = $password; } if (class_exists('SiteTree')) { SiteTree::add_extension('GoogleAnalyzer'); } }
/** * Activate the GoogleLogger * * @param $code mixed: * String the Google Analytics code to be used in the JS snippet or * String 'SiteConfig' for using the SiteConfig to configure this value or * Null if you hardcode the JS snippet into your template. The JS snippet will not be included through Requirements * **/ public static function activate($code = null) { switch ($code) { case null: self::$google_analytics_code = null; break; case 'SiteConfig': SiteConfig::add_extension('GoogleConfig'); break; default: self::$google_analytics_code = $code; } Controller::add_extension('GoogleLogger'); if (substr(GoogleAnalyzer::get_sapphire_version(), 0, 3) == '2.3') { Director::add_callback(array("GoogleLogger", "onAfterInit23")); } }
function testGetSapphireVersion() { $this->assertEquals('2.4', GoogleAnalyzer::get_sapphire_version()); }
<?php GoogleAnalyzer::activate('SiteConfig'); GoogleLogger::activate('SiteConfig');