Skip to content

Commit

Permalink
hotjar
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagotalma committed May 30, 2016
1 parent 70a765c commit 5b55776
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 209 deletions.
87 changes: 11 additions & 76 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Google Analytics for Yii2
# Hotjar for Yii2

[![Join the chat at https://gitter.im/cybercog/yii2-google-analytics](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/cybercog/yii2-google-analytics?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
This extension provides easy way to add Hotjar tracking in your Yii2 application.

This extension provides easy way to add Universal Analytics tracking in your Yii2 application.
Based on https://github.com/cybercog/yii2-google-analytics

## Installation

Expand All @@ -11,13 +11,13 @@ The preferred way to install this extension is through [composer](http://getcomp
Either run

```bash
$ php composer.phar require cybercog/yii2-google-analytics "~0.2"
$ php composer.phar require thiagotalma/yii2-hotjar "*
```
or add
```json
"cybercog/yii2-google-analytics": "~0.2"
"thiagotalma/yii2-hotjar": "*"
```
to the require section of your `composer.json` file.
Expand All @@ -27,93 +27,28 @@ to the require section of your `composer.json` file.
In your `/views/layouts/main.php` add
```php
use cybercog\yii\googleanalytics\widgets\GATracking;
use talma\hotjar\widgets\HotjarTracking;
```
Then before `</head>` add following code
```php
<?= GATracking::widget(
<?= HotjarTracking::widget(
[
'trackingId' => 'UA-XXXXXXXX-X'
'trackingId' => 'XXXXXXXX'
]
) ?>
```
## Advanced usage
### Omit script tag

By default this script generated output:

```html
<script>
(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');
ga('create', 'UA-XXXXXXXX-X', "auto");
ga('send', 'pageview');
ga('set', 'anonymizeIp', true);
</script>
```

But sometimes we need the output without `script` tag to combined with `registerJs` or `registerJsFile` as `renderPartial` to add dependency or positioning configuration, you can use *omitScriptTag* **true** to disable `script` tag, example:
If you need the output without `script` tag to combined with `registerJs` or `registerJsFile` as `renderPartial` to add dependency or positioning configuration, you can use *omitScriptTag* **true** to disable `script` tag, example:
```php
<?= $this->registerJs(
GATracking::widget([
'trackingId' => 'UA-XXXXXXXX-X',
HotjarTracking::widget([
'trackingId' => 'XXXXXXXX',
'omitScriptTag' => true
]), \yii\web\View::POS_END
); ?>
```

### Example of advanced usage

You can configure tracking script for your needs:

```php
<?= GATracking::widget(
[
'trackingId' => 'UA-XXXXXXXX-X',
'trackingConfig' => [
'name' => 'myTracker',
'allowAnchor' => false
],
'omitScriptTag' => 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 |
| debug | boolean | false |
| debugTrace | boolean | false |

### [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)
13 changes: 6 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
{
"name": "cybercog/yii2-google-analytics",
"description": "Google Analytics extension for the Yii2 framework",
"name": "thiagotalma/yii2-hotjar",
"description": "Hotjar extension for the Yii2 framework",
"keywords": [
"yii2",
"google",
"analytics"
"hotjar"
],
"type": "yii2-extension",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Anton Komarev",
"email": "[email protected]"
"name": "Thiago Talma",
"email": "[email protected]"
}
],
"require": {
"yiisoft/yii2": "*"
},
"autoload": {
"psr-4": {
"cybercog\\yii\\googleanalytics\\": "src"
"talma\\hotjar\\": "src"
}
}
}
110 changes: 0 additions & 110 deletions src/widgets/GATracking.php

This file was deleted.

50 changes: 50 additions & 0 deletions src/widgets/HotjarTracking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* @link https://github.com/thiagotalma/yii2-hotjar
* @copyright Copyright (c) 2015 LLC CyberCog
* @license http://opensource.org/licenses/BSD-3-Clause
*/
namespace talma\hotjar\widgets;

use yii\base\Widget;

/**
* Class HotjarTracking
* @package talma\hotjar\widgets
* @author Thiago Talma <[email protected]>
*/
class HotjarTracking extends Widget
{

/**
* Render <script></script>
* @var bool
*/
public $omitScriptTag = false;

/**
* The Hotjar Tracking ID
* @var string
*/
public $trackingId = null;

/**
* @var array
*/
private $_viewParams;

public function init()
{
parent::init();

$this->_viewParams = [
'omitScriptTag' => $this->omitScriptTag,
'trackingId' => $this->trackingId,
];
}

public function run()
{
echo $this->render('tracking', $this->_viewParams);
}
}
24 changes: 8 additions & 16 deletions src/widgets/views/tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,19 @@
/**
* @var boolean $omitScriptTag
* @var string $trackingId
* @var array $trackingParams
* @var array $tackingPlugins
*/
?>
<?php if (!$omitScriptTag) {
echo '<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/<?= $trackingFilename ?>','ga');

ga('create', '<?= $trackingId ?>', <?= $trackingConfig ?>);
ga('send', 'pageview');
<?php foreach($fields as $field => $value) : ?>
ga('set', '<?= $field ?>', <?= $value ?>);
<?php endforeach ?>
<?php foreach($plugins as $plugin => $options) : ?>
ga('require', '<?= $plugin ?>', <?= $options ?>);
<?php endforeach ?>
(function(h,o,t,j,a,r){
h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
h._hjSettings={hjid:<?= $trackingId ?>,hjsv:5};
a=o.getElementsByTagName('head')[0];
r=o.createElement('script');r.async=1;
r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
a.appendChild(r);
})(window,document,'//static.hotjar.com/c/hotjar-','.js?sv=');
<?php if (!$omitScriptTag) {
echo '</script>';
} ?>

0 comments on commit 5b55776

Please sign in to comment.