1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProtoneMedia\AnalyticsEventTracking; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Blade; |
6
|
|
|
use Illuminate\Support\Facades\Event; |
7
|
|
|
use Illuminate\Support\Facades\Route; |
8
|
|
|
use Illuminate\Support\ServiceProvider as BaseServiceProvider; |
9
|
|
|
use ProtoneMedia\AnalyticsEventTracking\Analytics\BroadcastEvent; |
10
|
|
|
use ProtoneMedia\AnalyticsEventTracking\Analytics\EventBroadcaster; |
11
|
|
|
use ProtoneMedia\AnalyticsEventTracking\Http\ClientIdRepository; |
12
|
|
|
use ProtoneMedia\AnalyticsEventTracking\Http\ClientIdSession; |
13
|
|
|
use ProtoneMedia\AnalyticsEventTracking\Http\StoreClientIdInSession; |
14
|
|
|
use ProtoneMedia\AnalyticsEventTracking\Listeners\DispatchAnalyticsJob; |
15
|
|
|
use TheIconic\Tracking\GoogleAnalytics\Analytics; |
16
|
|
|
|
17
|
|
|
class ServiceProvider extends BaseServiceProvider |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Bootstrap the application services. |
21
|
|
|
*/ |
22
|
|
|
public function boot() |
23
|
|
|
{ |
24
|
|
|
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'analytics-event-tracking'); |
25
|
|
|
|
26
|
|
|
if ($this->app->runningInConsole()) { |
27
|
|
|
$this->publishes([ |
28
|
|
|
__DIR__ . '/../config/config.php' => config_path('analytics-event-tracking.php'), |
29
|
|
|
], 'config'); |
30
|
|
|
|
31
|
|
|
$this->publishes([ |
32
|
|
|
__DIR__ . '/../resources/views' => resource_path('views/vendor/analytics-event-tracking'), |
33
|
|
|
], 'views'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
Event::listen(ShouldBroadcastToAnalytics::class, DispatchAnalyticsJob::class); |
37
|
|
|
|
38
|
|
|
Blade::directive('sendAnalyticsClientId', function () { |
39
|
|
|
return "<?php echo view('analytics-event-tracking::sendCliendId'); ?>"; |
40
|
|
|
}); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Register the application services. |
45
|
|
|
*/ |
46
|
|
|
public function register() |
47
|
|
|
{ |
48
|
|
|
$this->mergeConfigFrom( |
49
|
|
|
__DIR__ . '/../config/config.php', |
50
|
|
|
'analytics-event-tracking' |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$this->app->singleton(EventBroadcaster::class, BroadcastEvent::class); |
54
|
|
|
|
55
|
|
|
$this->registerClientId(); |
56
|
|
|
$this->registerAnalytics(); |
57
|
|
|
$this->registerRoute(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function registerClientId() |
61
|
|
|
{ |
62
|
|
|
$this->app->singleton(ClientIdRepository::class, ClientIdSession::class); |
63
|
|
|
|
64
|
|
|
$this->app->bind('analytics-event-tracking.client-id', function () { |
65
|
|
|
return $this->app->make(ClientIdSession::class)->get(); |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
$this->app->singleton(ClientIdSession::class, function () { |
69
|
|
|
return new ClientIdSession( |
70
|
|
|
$this->app->make('session.store'), |
71
|
|
|
config('analytics-event-tracking.client_id_session_key') |
72
|
|
|
); |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function registerAnalytics() |
77
|
|
|
{ |
78
|
|
|
$this->app->bind(Analytics::class, function () { |
79
|
|
|
return tap(new Analytics(config('analytics-event-tracking.use_ssl')), function (Analytics $analytics) { |
80
|
|
|
$analytics->setProtocolVersion(1)->setTrackingId( |
81
|
|
|
config('analytics-event-tracking.tracking_id') |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
if (config('analytics-event-tracking.anonymize_ip')) { |
85
|
|
|
$analytics->setAnonymizeIp(1); |
86
|
|
|
} |
87
|
|
|
}); |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function registerRoute() |
92
|
|
|
{ |
93
|
|
|
if ($httpUri = config('analytics-event-tracking.http_uri')) { |
94
|
|
|
Route::post($httpUri, StoreClientIdInSession::class)->middleware('web'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|