Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.

Commit 2c11a11

Browse files
Adding is_enabled option (#9)
* Adding is_disabled option * Flip boolean Co-authored-by: Pascal Baljet <pascal@pascalbaljet.nl>
1 parent 0964445 commit 2c11a11

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ class OrderWasPaid implements ShouldBroadcastToAnalytics
236236
You can configure some additional settings in the `config/analytics-event-tracking.php` file:
237237

238238
* `use_ssl`: Use SSL to make calls to GA
239+
* `is_enabled`: Set to `false` to prevent events from being sent to GA
239240
* `anonymize_ip`: Anonymizes the last digits of the user's IP
240241
* `send_user_id`: Send the ID of the authenticated user to GA
241242
* `queue_name`: Specify a queue to perform the calls to GA

config/config.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
*/
88
'tracking_id' => env('GOOGLE_ANALYTICS_TRACKING_ID'),
99

10+
/**
11+
* Enable sending events to GA.
12+
*/
13+
'is_enabled' => true,
14+
1015
/**
1116
* Use SSL to make calls to GA.
1217
*/

src/ServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ private function registerClientId()
7373
});
7474
}
7575

76-
private function registerAnalytics()
76+
public function registerAnalytics()
7777
{
7878
$this->app->bind(Analytics::class, function () {
79-
return tap(new Analytics(config('analytics-event-tracking.use_ssl')), function (Analytics $analytics) {
79+
return tap(new Analytics(config('analytics-event-tracking.use_ssl'), !config('analytics-event-tracking.is_enabled', true)), function (Analytics $analytics) {
8080
$analytics->setProtocolVersion(1)->setTrackingId(
8181
config('analytics-event-tracking.tracking_id')
8282
);

tests/ServiceProviderTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ProtoneMedia\AnalyticsEventTracking\Tests;
44

5+
use ProtoneMedia\AnalyticsEventTracking\ServiceProvider;
56
use TheIconic\Tracking\GoogleAnalytics\Analytics;
67

78
class ServiceProviderTest extends TestCase
@@ -12,6 +13,24 @@ public function it_sets_the_tracking_id_from_the_configuration()
1213
$this->assertStringContainsString('tid=UA-11111111-11', app(Analytics::class)->getUrl());
1314
}
1415

16+
/** @test */
17+
public function it_can_disable_sending_events()
18+
{
19+
config(['analytics-event-tracking.is_enabled' => false]);
20+
21+
$serviceProvider = new ServiceProvider($this->app);
22+
$serviceProvider->registerAnalytics();
23+
24+
$analytics = $this->app->make(Analytics::class);
25+
26+
// Make 'isDisabled' protected property accessible
27+
$reflection = new \ReflectionClass($analytics);
28+
$property = $reflection->getProperty('isDisabled');
29+
$property->setAccessible(true);
30+
31+
$this->assertTrue($property->getValue($analytics));
32+
}
33+
1534
/** @test */
1635
public function it_can_disable_ssl()
1736
{

0 commit comments

Comments
 (0)