Skip to content

Commit

Permalink
Added debugging mode #5
Browse files Browse the repository at this point in the history
Added tracking object configuration #10
Add plugins support (partial) #6
  • Loading branch information
antonkomarev committed Apr 19, 2015
1 parent 5e929a8 commit 02bfc2e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ Then before `</head>` add following code
<?= GATracking::widget(
[
'trackingId' => 'UA-XXXXXXXX-X',
'trackingConfig' => [
'name' => 'myTracker',
'allowAnchor' => false
],
'debug' => true,
'debugTrace' => true,
'anonymizeIp' => true,
'plugins' => [
'linkid' => [
Expand Down
50 changes: 46 additions & 4 deletions src/widgets/GATracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,48 @@ class GATracking extends Widget
{
/**
* The GA tracking ID
* @var null
* @var string
*/
public $trackingId = null;

/**
* Anonymize IP
* @var boolean
* Tracking object configuration.
* @var array
*/
public $trackingConfig = 'auto';

/**
* Anonymize the IP address of the hit (http request) sent to GA.
* @var bool
*/
public $anonymizeIp = true;

/**
* Output debug information to the console.
* @var bool
*/
public $debug = false;

/**
* Trace debugging will output more verbose information to the console.
* @var bool
*/
public $debugTrace = false;

/**
* Plugins list
* @var array
*/
public $plugins = [];

/**
* GA script filename
* @var string
*/
private $_trackingFilename = 'analytics.js';

private $_trackingDebugTraceInit = '';

/**
* @var array
*/
Expand All @@ -42,13 +68,29 @@ public function init()
{
parent::init();

if ($this->debug) {
$this->_trackingFilename = 'analytics_debug.js';
}
if ($this->debugTrace) {
$this->_trackingDebugTraceInit = 'window.ga_debug = {trace: true};';
}

$this->trackingConfig = json_encode($this->trackingConfig);
$this->anonymizeIp = json_encode($this->anonymizeIp);
foreach ($this->plugins as $plugin => &$options) {
$options = json_encode($options);
}

$this->_viewParams = [
'trackingId' => $this->trackingId,
'trackingConfig' => $this->trackingConfig,
'trackingFilename' => $this->_trackingFilename,
'trackingDebugTraceInit' => $this->_trackingDebugTraceInit,
'fields' => [
'anonymizeIp' => $this->anonymizeIp
// :TODO: Add more params
],
// :TODO: Add availability to assign options
// :TODO: Add availability to configure events
'plugins' => $this->plugins
];
}
Expand Down
15 changes: 8 additions & 7 deletions src/widgets/views/tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
*/
?>
<script>
<?= $trackingDebugTraceInit ?>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
})(window,document,'script','//www.google-analytics.com/<?= $trackingFilename ?>','ga');

ga('create', '<?= $trackingId ?>', 'auto');
ga('create', '<?= $trackingId ?>', <?= $trackingConfig ?>);
ga('send', 'pageview');
<?php foreach($trackingFields as $field => $value) : ?>
ga('set', '<?= $field ?>', <?= $value ?>);
<?php foreach($fields as $field => $value) : ?>
ga('set', '<?= $field ?>', <?= $value ?>);
<?php endforeach ?>
<?php foreach($trackingPlugins as $plugin => $scriptFilename) : ?>
ga('require', '<?= $plugin ?>', '<?= $scriptFilename ?>');
<?php foreach($plugins as $plugin => $options) : ?>
ga('require', '<?= $plugin ?>', <?= $options ?>);
<?php endforeach ?>
</script>
</script>

0 comments on commit 02bfc2e

Please sign in to comment.