Skip to content
This repository was archived by the owner on Feb 17, 2022. It is now read-only.

Commit b3356e4

Browse files
author
Robert Kummer
committed
adding debug configuration property and nonInteraction tracking mode
1 parent 5d56625 commit b3356e4

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/Ipunkt/LaravelAnalytics/Providers/GoogleAnalytics.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ class GoogleAnalytics implements AnalyticsProviderInterface
4242
*/
4343
private $autoTrack = false;
4444

45+
/**
46+
* debug mode
47+
*
48+
* @var bool
49+
*/
50+
private $debug = false;
51+
52+
/**
53+
* for event tracking it can mark track as non-interactive so the bounce-rate calculation ignores that tracking
54+
*
55+
* @var bool
56+
*/
57+
private $nonInteraction = false;
58+
4559
/**
4660
* session tracking bag
4761
*
@@ -69,6 +83,7 @@ public function __construct(array $options = array())
6983
$this->trackingDomain = array_get($options, 'tracking_domain', 'auto');
7084
$this->anonymizeIp = array_get($options, 'anonymize_ip', false);
7185
$this->autoTrack = array_get($options, 'auto_track', false);
86+
$this->debug = array_get($options, 'debug', false);
7287

7388
if ($this->trackingId === null) {
7489
throw new InvalidArgumentException('Argument tracking_id can not be null');
@@ -173,7 +188,7 @@ public function render()
173188
{
174189
$script[] = $this->_getJavascriptTemplateBlockBegin();
175190

176-
if (App::environment('local')) {
191+
if ($this->debug || App::environment('local')) {
177192
$script[] = "ga('create', '{$this->trackingId}', { 'cookieDomain': 'none' });";
178193
} else {
179194
$script[] = "ga('create', '{$this->trackingId}', '{$this->trackingDomain}');";
@@ -183,6 +198,10 @@ public function render()
183198
$script[] = "ga('set', 'anonymizeIp', true);";
184199
}
185200

201+
if ($this->nonInteraction) {
202+
$script[] = "ga('set', 'nonInteraction', true);";
203+
}
204+
186205
$trackingStack = $this->trackingBag->get();
187206
if (count($trackingStack)) {
188207
$script[] = implode("\n", $trackingStack);
@@ -196,14 +215,37 @@ public function render()
196215
return implode('', $script);
197216
}
198217

218+
/**
219+
* sets or gets nonInteraction
220+
*
221+
* setting: $this->nonInteraction(true)->render();
222+
* getting: if ($this->nonInteraction()) echo 'non-interaction set';
223+
*
224+
* @param boolean|null $value
225+
*
226+
* @return bool|$this
227+
*/
228+
public function nonInteraction($value = null)
229+
{
230+
if (null === $value) {
231+
return $this->nonInteraction;
232+
}
233+
234+
$this->nonInteraction = ($value === true);
235+
236+
return $this;
237+
}
238+
199239
/**
200240
* returns start block
201241
*
202242
* @return string
203243
*/
204244
protected function _getJavascriptTemplateBlockBegin()
205245
{
206-
return "<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');";
246+
$appendix = $this->debug ? '_debug' : '';
247+
248+
return "<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{$appendix}.js','ga');";
207249
}
208250

209251
/**

src/config/analytics.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* - tracking_domain (string:auto) - default will be 'auto' if config property not exists
1919
* - anonymize_ip (bool) - default will be false if no config property exists
2020
* - auto_track (bool) - default will be false if no config property exists
21+
* - debug (bool) - default will be false if no config property exists
2122
*/
2223
'GoogleAnalytics' => [
2324

@@ -43,6 +44,10 @@
4344
*/
4445
'auto_track' => true,
4546

47+
/**
48+
* Enable the debugging version of the
49+
*/
50+
'debug' => false,
4651
]
4752

4853
],

0 commit comments

Comments
 (0)