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

Commit 5d56625

Browse files
author
Robert Kummer
committed
make use of illuminate support helpers, using https for measurement url by default
1 parent 19df981 commit 5d56625

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

src/Ipunkt/LaravelAnalytics/Providers/GoogleAnalytics.php

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
/**
1111
* Class GoogleAnalytics
12+
*
1213
* @package Ipunkt\LaravelAnalytics\Providers
1314
*/
1415
class GoogleAnalytics implements AnalyticsProviderInterface
@@ -48,6 +49,13 @@ class GoogleAnalytics implements AnalyticsProviderInterface
4849
*/
4950
private $trackingBag;
5051

52+
/**
53+
* use https for the tracking measurement url
54+
*
55+
* @var bool
56+
*/
57+
private $secureTrackingUrl = true;
58+
5159
/**
5260
* setting options via constructor
5361
*
@@ -57,10 +65,10 @@ class GoogleAnalytics implements AnalyticsProviderInterface
5765
*/
5866
public function __construct(array $options = array())
5967
{
60-
$this->trackingId = (isset($options['tracking_id'])) ? $options['tracking_id'] : null;
61-
$this->trackingDomain = (isset($options['tracking_domain'])) ? $options['tracking_domain'] : 'auto';
62-
$this->anonymizeIp = (isset($options['anonymize_ip'])) ? $options['anonymize_ip'] : false;
63-
$this->autoTrack = (isset($options['auto_track'])) ? $options['auto_track'] : false;
68+
$this->trackingId = array_get($options, 'tracking_id');
69+
$this->trackingDomain = array_get($options, 'tracking_domain', 'auto');
70+
$this->anonymizeIp = array_get($options, 'anonymize_ip', false);
71+
$this->autoTrack = array_get($options, 'auto_track', false);
6472

6573
if ($this->trackingId === null) {
6674
throw new InvalidArgumentException('Argument tracking_id can not be null');
@@ -85,7 +93,7 @@ public function trackPage($page = null, $title = null, $hittype = null)
8593
$hittype = $allowedHitTypes[0];
8694
}
8795

88-
if (! in_array($hittype, $allowedHitTypes)) {
96+
if ( ! in_array($hittype, $allowedHitTypes)) {
8997
return;
9098
}
9199

@@ -165,7 +173,7 @@ public function render()
165173
{
166174
$script[] = $this->_getJavascriptTemplateBlockBegin();
167175

168-
if (App::environment() === 'local') {
176+
if (App::environment('local')) {
169177
$script[] = "ga('create', '{$this->trackingId}', { 'cookieDomain': 'none' });";
170178
} else {
171179
$script[] = "ga('create', '{$this->trackingId}', '{$this->trackingDomain}');";
@@ -208,6 +216,30 @@ protected function _getJavascriptTemplateBlockEnd()
208216
return '</script>';
209217
}
210218

219+
/**
220+
* make the tracking measurement url unsecure
221+
*
222+
* @return $this
223+
*/
224+
public function unsecureMeasurementUrl()
225+
{
226+
$this->secureTrackingUrl = false;
227+
228+
return $this;
229+
}
230+
231+
/**
232+
* use the secured version of the tracking measurement url
233+
*
234+
* @return $this
235+
*/
236+
public function secureMeasurementUrl()
237+
{
238+
$this->secureTrackingUrl = false;
239+
240+
return $this;
241+
}
242+
211243
/**
212244
* assembles an url for tracking measurement without javascript
213245
*
@@ -236,8 +268,10 @@ public function trackMeasurementUrl($metricName, $metricValue, Event $event, Cam
236268
$campaign->setName('Campaign ' . date('Y-m-d'));
237269
}
238270

271+
$protocol = $this->secureTrackingUrl ? 'https' : 'http';
272+
239273
$defaults = [
240-
'url' => 'http://www.google-analytics.com/collect?',
274+
'url' => $protocol . '://www.google-analytics.com/collect?',
241275
'params' => [
242276
'v' => 1, // protocol version
243277
'tid' => $this->trackingId, // tracking id
@@ -262,7 +296,7 @@ public function trackMeasurementUrl($metricName, $metricValue, Event $event, Cam
262296
$params = array_merge($defaults['params'], $params);
263297
$queryParams = [];
264298
foreach ($params as $key => $value) {
265-
if (! empty($value))
299+
if ( ! empty($value))
266300
$queryParams[] = sprintf('%s=%s', $key, $value);
267301
}
268302

0 commit comments

Comments
 (0)