Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54311c6
commit 2c55539
Showing
4 changed files
with
226 additions
and
1 deletion.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Copyright (c) 2012 TagPla.net | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the "Software"), | ||
to deal in the Software without restriction, including without limitation the | ||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
sell copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
yii-analytics-piwik | ||
=================== | ||
|
||
A Piwik component for the Yii framework | ||
The Piwik Analytics component for Yii. See our [Wiki](https://github.com/TagPlanet/yii-analytics-piwik/wiki) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
<?php | ||
/** | ||
* Piwik Component | ||
* | ||
* @author Philip Lawrence <philip@misterphilip.com> | ||
* @link http://misterphilip.com | ||
* @link http://tagpla.net | ||
* @link https://github.com/TagPlanet/yii-analytics-piwik | ||
* @copyright Copyright © 2012 Philip Lawrence | ||
* @license http://tagpla.net/licenses/MIT.txt | ||
* @version 1.0.0 | ||
*/ | ||
class TPPiwikAnalytics extends CApplicationComponent | ||
{ | ||
/** | ||
* Site ID | ||
* @var string | ||
*/ | ||
public $siteID; | ||
|
||
/** | ||
* Tracker URL | ||
* @var string | ||
*/ | ||
public $trackerURL; | ||
|
||
/** | ||
* Auto render or return the JS | ||
* @var bool | ||
*/ | ||
public $autoRender = false; | ||
|
||
/** | ||
* Automatically add trackPageView when render is called | ||
* @var bool | ||
*/ | ||
public $autoPageview = true; | ||
|
||
/** | ||
* JS Variable name | ||
* @var string | ||
*/ | ||
public $variableName = '_paq'; | ||
|
||
/** | ||
* Type of quotes to use for values | ||
*/ | ||
const Q = "'"; | ||
|
||
/** | ||
* Available options, pulled (Oct 16, 2012) from | ||
* http://piwik.org/docs/javascript-tracking/#toc-list-of-all-methods-available-in-the-tracking-api | ||
* @var array | ||
*/ | ||
protected $_availableOptions = array | ||
( | ||
'enableLinkTracking', | ||
'setRequestMethod', | ||
'trackGoal', | ||
'trackPageView', | ||
'setDocumentTitle', | ||
'setDomains', | ||
'setCustomUrl', | ||
'setReferrerUrl', | ||
'setSiteId', | ||
'setTrackerUrl', | ||
'setDownloadClasses', | ||
'setDownloadExtensions', | ||
'addDownloadExtensions', | ||
'setIgnoreClasses', | ||
'setLinkClasses', | ||
'setLinkTrackingTimer', | ||
'setCountPreRendered', | ||
'discardHashTag', | ||
'setCustomVariable', | ||
'deleteCustomVariable', | ||
'setCampaignNameKey', | ||
'setCampaignKeywordKey', | ||
'setConversionAttributionFirstReferrer', | ||
'setDoNotTrack', | ||
'disableCookies', | ||
'killFrame', | ||
'redirectFile', | ||
'setHeartBeatTimer', | ||
'setCookieNamePrefix', | ||
'setCookieDomain', | ||
'setCookiePath', | ||
'setVisitorCookieTimeout', | ||
'setSessionCookieTimeout', | ||
'setReferralCookieTimeout', | ||
); | ||
|
||
/** | ||
* An array of all the methods called for _gaq | ||
* @var array | ||
*/ | ||
protected $_calledOptions = array(); | ||
|
||
/** | ||
* Method data to be pushed into the _gaq object | ||
* @var array | ||
*/ | ||
|
||
private $_data = array(); | ||
|
||
/** | ||
* init function - Yii automaticall calls this | ||
*/ | ||
public function init() | ||
{ | ||
// Verify we have the basics | ||
if($this->siteID == '') | ||
throw new CException('Missing required parameter "Site ID" for TPPiwikAnalytics'); | ||
if($this->trackerURL == '') | ||
throw new CException('Missing required parameter "Tracker URL" for TPPiwikAnalytics'); | ||
|
||
$this->setSiteId($this->siteID); | ||
$this->setTrackerUrl($this->trackerURL); | ||
} | ||
|
||
/** | ||
* Render and return the Piwik code | ||
* @return mixed | ||
*/ | ||
public function render() | ||
{ | ||
// Check to see if we need to throw in the trackPageview call | ||
if(!in_array('trackPageView', $this->_calledOptions) && $this->autoPageview) | ||
{ | ||
$this->trackPageView(); | ||
} | ||
|
||
// Start the JS string | ||
$js = 'var ' . $this->variableName . ' = ' . $this->variableName . ' || [];' . PHP_EOL; | ||
$js.= '(function() { ' . PHP_EOL; | ||
|
||
foreach($this->_data as $data) | ||
{ | ||
// Clean up each item | ||
foreach($data as $key => $item) | ||
{ | ||
|
||
if(is_string($item)) | ||
{ | ||
$data[$key] = self::Q . preg_replace('~(?<!\\\)'. self::Q . '~', '\\' . self::Q, $item) . self::Q; | ||
} | ||
else if(is_bool($item)) | ||
{ | ||
$data[$key] = ($item) ? 'true' : 'false'; | ||
} | ||
|
||
$prefixed = true; | ||
} | ||
|
||
$js.= ' ' . $this->variableName . '.push([' . implode(',', $data) . ']);' . PHP_EOL; | ||
} | ||
$js.= <<<EOJS | ||
// Call the file | ||
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; | ||
g.type='text/javascript'; g.defer=true; g.async=true; g.src='{$this->trackerURL}'; | ||
s.parentNode.insertBefore(g,s); | ||
})(); | ||
// Piwik Extension provided by TagPla.net | ||
// https://github.com/TagPlanet/yii-analytics-piwik | ||
// Copyright 2012, TagPla.net & Philip Lawrence | ||
EOJS; | ||
// Should we auto add in the analytics tag? | ||
if($this->autoRender) | ||
{ | ||
Yii::app()->clientScript | ||
->registerScript('TPPiwikAnalytics', $js, CClientScript::POS_HEAD); | ||
return; | ||
} | ||
else | ||
{ | ||
return $js; | ||
} | ||
} | ||
|
||
/** | ||
* Magic Method for options | ||
* @param string $name | ||
* @param array $arguments | ||
*/ | ||
public function __call($name, $arguments) | ||
{ | ||
if(in_array($name, $this->_availableOptions)) | ||
{ | ||
$this->_push($name, $arguments); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Push data into the array | ||
* @param string $variable | ||
* @param array $arguments | ||
* @protected | ||
*/ | ||
protected function _push($variable, $arguments) | ||
{ | ||
$data = array_merge(array($variable), $arguments); | ||
array_push($this->_data, $data); | ||
$this->_calledOptions[] = $variable; | ||
} | ||
} |