-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
270 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
resources/views/components/measurement-protocol-client-id.blade.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
@if($isEnabled) | ||
<!-- Google Tag Manager Client ID --> | ||
<script> | ||
function getGoogleAnalyticsClientId() { | ||
var cookie = {}; | ||
document.cookie.split(';').forEach(function(el) { | ||
var splitCookie = el.split('='); | ||
var key = splitCookie[0].trim(); | ||
var value = splitCookie[1]; | ||
cookie[key] = value; | ||
}); | ||
storeGoogleAnalyticsClientId(cookie["_ga"].substring(6)); | ||
} | ||
function storeGoogleAnalyticsClientId(clientId) { | ||
var data = new FormData(); | ||
data.append('client_id', clientId); | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('POST', '/tagmanager/store-measurement-protocol-client-id', true); | ||
xhr.setRequestHeader('X-CSRF-TOKEN', '{{ csrf_token() }}'); | ||
xhr.send(data); | ||
} | ||
</script> | ||
|
||
@if(session(config('tagmanager.measurement_protocol_client_id_session_key')) === null) | ||
<script> | ||
getGoogleAnalyticsClientId(); | ||
</script> | ||
@endif | ||
<!-- End Google Tag Manager Client ID --> | ||
@endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Route; | ||
use Label84\TagManager\Http\Controllers\StoreMeasurementProtocolClientIdController; | ||
|
||
Route::post('/tagmanager/store-measurement-protocol-client-id', StoreMeasurementProtocolClientIdController::class) | ||
->middleware('web') | ||
->name('tagmanager.store-measurement-protocol-client-id'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Label84\TagManager\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
class MeasurementProtocol extends Facade | ||
{ | ||
protected static function getFacadeAccessor(): string | ||
{ | ||
return 'measurement_protocol'; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Http/Controllers/StoreMeasurementProtocolClientIdController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Label84\TagManager\Http\Controllers; | ||
|
||
class StoreMeasurementProtocolClientIdController | ||
{ | ||
public function __invoke(): void | ||
{ | ||
session([ | ||
config('tagmanager.measurement_protocol_client_id_session_key') => request('client_id'), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace Label84\TagManager; | ||
|
||
use Illuminate\Foundation\Auth\User; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
class MeasurementProtocol | ||
{ | ||
protected string $clientId; | ||
|
||
protected string|int|null $userId = null; | ||
|
||
protected bool $isDebugEnabled = false; | ||
|
||
public function __construct() | ||
{ | ||
$this->clientId = session(config('tagmanager.measurement_protocol_client_id_session_key')); | ||
} | ||
|
||
public function event(string $name, array $params = null): array | ||
{ | ||
$event = [ | ||
'name' => $name, | ||
]; | ||
|
||
if ($params !== null) { | ||
$event['params'] = $params; | ||
} | ||
|
||
$response = Http::withHeaders([ | ||
'content-type' => 'application/json', | ||
]) | ||
->withQueryParameters([ | ||
'measurement_id' => config('tagmanager.measurement_id'), | ||
'api_secret' => config('tagmanager.measurement_protocol_api_secret'), | ||
]) | ||
->post($this->route(), array_merge( | ||
[ | ||
'client_id' => $this->clientId, | ||
'events' => [$event], | ||
], | ||
$this->getUserIdArray(), | ||
)); | ||
|
||
if($this->isDebugEnabled) { | ||
return $response->json(); | ||
} | ||
|
||
return [ | ||
'status' => $response->successful() ? 'success' : 'error', | ||
]; | ||
} | ||
|
||
public function debug(): self | ||
{ | ||
$this->isDebugEnabled = true; | ||
|
||
return $this; | ||
} | ||
|
||
public function user(User $user): self | ||
{ | ||
$this->userId = "{$user->{config('tagmanager.user_id_key')}}"; | ||
|
||
return $this; | ||
} | ||
|
||
private function route(): string | ||
{ | ||
$url = 'www.google-analytics.com'; | ||
|
||
if ($this->isDebugEnabled) { | ||
$url = 'www.google-analytics.com/debug'; | ||
} | ||
|
||
return "https://{$url}/mp/collect"; | ||
} | ||
|
||
private function getUserIdArray(): array | ||
{ | ||
if ($this->userId === null) { | ||
return []; | ||
} | ||
|
||
return [ | ||
'user_id' => $this->userId, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Label84\TagManager\View\Components; | ||
|
||
use Illuminate\View\Component; | ||
use Illuminate\View\View; | ||
|
||
class MeasurementProtocolClientId extends Component | ||
{ | ||
public bool $isEnabled; | ||
|
||
public function __construct() | ||
{ | ||
$this->isEnabled = config('tagmanager.enabled'); | ||
} | ||
|
||
public function render(): View | ||
{ | ||
return view('tagmanager::components.measurement-protocol-client-id'); | ||
} | ||
} |