Skip to content

Commit

Permalink
Merge pull request #11 from cybercog/develop
Browse files Browse the repository at this point in the history
More flexibility in widget configuration
  • Loading branch information
Pe Ell committed Apr 19, 2015
2 parents 78334d2 + fc2ecfd commit a7b0015
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 14 deletions.
52 changes: 48 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ The preferred way to install this extension is through [composer](http://getcomp
Either run

```bash
$ php composer.phar require cybercog/yii2-google-analytics "*"
$ php composer.phar require cybercog/yii2-google-analytics "~0.2"
```

or add

```json
"cybercog/yii2-google-analytics": "*"
"cybercog/yii2-google-analytics": "~0.2"
```

to the require section of your `composer.json` file.
Expand All @@ -30,11 +30,55 @@ use cybercog\yii\googleanalytics\widgets\GATracking;

Then before `</head>` add following code

```php
<?= GATracking::widget(
[
'trackingId' => 'UA-XXXXXXXX-X'
]
) ?>
```

## Advanced usage

```php
<?= GATracking::widget(
[
'trackingId' => 'UA-XXXXXXXX-X',
'anonymizeIp' => '[true|false]',
'trackingConfig' => [
'name' => 'myTracker',
'allowAnchor' => false
],
'debug' => true,
'debugTrace' => true,
'anonymizeIp' => true,
'plugins' => [
'linkid' => [
'cookieName' => '_ccli',
'duration' => 45,
'levels' => 5
]
]
]
) ?>
```
```

## Available fields (parameters)


| Field Name | Value Type | Default Value |
| :--------- | :--------- | :------------ |
| anonymizeIp | boolean | true |

### [Official field reference](https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference)

## Available plugins

### linkid (Enhanced Link Attribution)

| Option Name | Default Value | Description |
| :---------- | :------------ | :---------- |
| cookieName | _gali | Cookie name |
| duration | 30 | Cookie duration (seconds) |
| levels | 3 | Max DOM levels from link to look for element ID |

### [Creating your own plugins](https://developers.google.com/analytics/devguides/collection/analyticsjs/plugins)
62 changes: 56 additions & 6 deletions src/widgets/GATracking.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* @link https://github.com/cybercog/yii2-twittable
* @link https://github.com/cybercog/yii2-google-analytics
* @copyright Copyright (c) 2015 LLC CyberCog
* @license http://opensource.org/licenses/BSD-3-Clause
*/
Expand All @@ -17,15 +17,47 @@ class GATracking extends Widget
{
/**
* The GA tracking ID
* @var null
* @var string
*/
public $trackingId = null;

/**
* Anonymize IP
* 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
*/
public $anonymizeIp = 'true';
private $_trackingFilename = 'analytics.js';

private $_trackingDebugTraceInit = '';

/**
* @var array
Expand All @@ -36,12 +68,30 @@ 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,
'trackingParams' => [
'trackingId' => $this->trackingId,
'trackingConfig' => $this->trackingConfig,
'trackingFilename' => $this->_trackingFilename,
'trackingDebugTraceInit' => $this->_trackingDebugTraceInit,
'fields' => [
'anonymizeIp' => $this->anonymizeIp
// :TODO: Add more params
],
// :TODO: Add availability to configure events
'plugins' => $this->plugins
];
}

Expand Down
15 changes: 11 additions & 4 deletions src/widgets/views/tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
/**
* @var string $trackingId
* @var array $trackingParams
* @var array $tackingPlugins
*/
?>
<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');
ga('set', 'anonymizeIp', <?= $trackingParams['anonymizeIp'] ?>);
</script>
<?php foreach($fields as $field => $value) : ?>
ga('set', '<?= $field ?>', <?= $value ?>);
<?php endforeach ?>
<?php foreach($plugins as $plugin => $options) : ?>
ga('require', '<?= $plugin ?>', <?= $options ?>);
<?php endforeach ?>
</script>

0 comments on commit a7b0015

Please sign in to comment.