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

Adding is_disabled option #9

Merged
merged 2 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ class OrderWasPaid implements ShouldBroadcastToAnalytics
You can configure some additional settings in the `config/analytics-event-tracking.php` file:

* `use_ssl`: Use SSL to make calls to GA
* `is_enabled`: Set to `false` to prevent events from being sent to GA
* `anonymize_ip`: Anonymizes the last digits of the user's IP
* `send_user_id`: Send the ID of the authenticated user to GA
* `queue_name`: Specify a queue to perform the calls to GA
Expand Down
5 changes: 5 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
*/
'tracking_id' => env('GOOGLE_ANALYTICS_TRACKING_ID'),

/**
* Enable sending events to GA.
*/
'is_enabled' => true,

/**
* Use SSL to make calls to GA.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ private function registerClientId()
});
}

private function registerAnalytics()
public function registerAnalytics()
{
$this->app->bind(Analytics::class, function () {
return tap(new Analytics(config('analytics-event-tracking.use_ssl')), function (Analytics $analytics) {
return tap(new Analytics(config('analytics-event-tracking.use_ssl'), !config('analytics-event-tracking.is_enabled', true)), function (Analytics $analytics) {
$analytics->setProtocolVersion(1)->setTrackingId(
config('analytics-event-tracking.tracking_id')
);
Expand Down
19 changes: 19 additions & 0 deletions tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ProtoneMedia\AnalyticsEventTracking\Tests;

use ProtoneMedia\AnalyticsEventTracking\ServiceProvider;
use TheIconic\Tracking\GoogleAnalytics\Analytics;

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

/** @test */
public function it_can_disable_sending_events()
{
config(['analytics-event-tracking.is_enabled' => false]);

$serviceProvider = new ServiceProvider($this->app);
$serviceProvider->registerAnalytics();

$analytics = $this->app->make(Analytics::class);

// Make 'isDisabled' protected property accessible
$reflection = new \ReflectionClass($analytics);
$property = $reflection->getProperty('isDisabled');
$property->setAccessible(true);

$this->assertTrue($property->getValue($analytics));
}

/** @test */
public function it_can_disable_ssl()
{
Expand Down