PHP code example of schenke-io / laravel-ga4-marketing
1. Go to this page and download the library: Download schenke-io/laravel-ga4-marketing library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
schenke-io / laravel-ga4-marketing example snippets
return [
/*
* Full path to the service account credentials JSON file.
* This is * Your Google Analytics 4 Measurement ID (e.g., G-XXXXXXXXXX).
* Found in GA4 Admin > Data Streams > [Your Stream].
*/
'measurement_id' => env('GA4_MEASUREMENT_ID', config('services.google.ga4.measurement_id')),
/*
* The Measurement Protocol API Secret.
* Created in GA4 Admin > Data Streams > [Your Stream] > Measurement Protocol API secrets.
*/
'api_secret' => env('GA4_API_SECRET', config('services.google.ga4.api_secret')),
/*
* When enabled, events /*
* The maximum number of events allowed per client within
* the specified decay period.
*/
'max_attempts' => env('GA4_RATE_LIMIT_MAX_ATTEMPTS', 30),
/*
* The time window (in seconds) for rate limiting. After this period,
* the attempt count for a client is reset.
*/
'decay_seconds' => env('GA4_RATE_LIMIT_DECAY_SECONDS', 60),
],
/*
* The duration (in seconds) of inactivity before a new session is started.
* Defaults to 1800 seconds (30 minutes).
*/
'session_lifetime' => env('GA4_SESSION_LIFETIME', 1800),
/*
* How events are processed: 'api' for immediate sending or 'job' to
* queue them for background processing.
*/
'event_handling' => env('GA4_EVENT_HANDLING', 'api'),
/*
* When enabled, the client ID is automatically set to a hashed
* version of the authenticated user's ID.
*/
'client_from_user_id' => env('GA4_CLIENT_FROM_USER_ID', false),
/*
* The name of the cookie used to store the visitor ID.
*/
'cookie_name' => env('GA4_COOKIE_NAME', 'visitor'),
/*
* The lifetime of the visitor cookie in minutes (144000 = 100 days).
*/
'cookie_lifetime' => env('GA4_COOKIE_LIFETIME', 144000),
],
/*
* Add additional bot user-agent fragments here to be excluded from tracking.
* Useful for filtering out custom crawlers or internal monitoring tools.
*/
'extra_bots' => [
// 'custom-bot',
],
];
Route::middleware(['track-page-view'])->group(function () {
// your routes
});
Route::middleware(['capture-ad-parameters'])->group(function () {
// your routes
});
Route::middleware(['track-page-view'])->group(function () {
// your routes
});