Skip to content

Commit

Permalink
Initial Version (v.1.0.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterPhilip committed Oct 17, 2012
1 parent 54311c6 commit 2c55539
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 1 deletion.
Empty file added .gitignore
Empty file.
18 changes: 18 additions & 0 deletions LICENSE
@@ -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.
2 changes: 1 addition & 1 deletion README.md
@@ -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.
207 changes: 207 additions & 0 deletions components/TPPiwikAnalytics.php
@@ -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 &copy; 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;
}
}

0 comments on commit 2c55539

Please sign in to comment.