Coder Social home page Coder Social logo

laravel-google-analytics-4-measurement-protocol's Introduction

Plant a Tree

Laravel Google Analytics 4 Measurement Protocol

A Laravel package to use Measurement Protocol for Google Analytics 4.

Introduction

This package allows you to post events to Google Analytics 4 from your Laravel backend.

Supported Laravel versions

  • Laravel 7.x
  • Laravel 8.x
  • Laravel 9.x
  • Laravel 10.x

Installation

  1. Install the package by running this command in your terminal/cmd:
composer require freshbitsweb/laravel-google-analytics-4-measurement-protocol
  1. Set MEASUREMENT_ID and MEASUREMENT_PROTOCOL_API_SECRET in your .env file. You can get them from: Google Analytics > Admin > Data Streams > [Select Site] > Measurement Protocol API secrets

  2. Optional: You can publish the config file by running this command in your terminal/cmd:

php artisan vendor:publish --tag=google-analytics-4-measurement-protocol-config
  1. client_id is required to post an event to Google Analytics. This package provides a Blade component which you can put in your layout file after the Google Analytics Code tracking code. It makes a POST request to the backend to store the client id in the session which is later used to post events to Google Analytics 4.
<!-- Google Analytics Code -->
<x-google-analytics-client-id />
<!-- </head> -->

The other option is to call the setClientId($clientId) method on the GA4 facade everytime before calling the postEvent() method.

Usage

You can simple call GA4::postEvent($eventData) from anywhere in your backend to post event to Google Analytics 4. $eventData contains the name and params of the event as per this reference page. For example:

GA4::postEvent([
    'name' => 'login',
    'params' => [
        'method' => 'Google',
    ],
]);

postEvent() method will return an array with the status of the request.

Debugging Mode

You can also enable debugging mode by calling enableDebugging() method before calling the postEvent() method. Like so - GA4::enableDebugging()->postEvent($eventData). The postEvent() method will return the response (array) from Google Analytics request in that case.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

Treeware

You're free to use this package, but if it makes it to your production environment I would highly appreciate you buying the world a tree.

It’s now common knowledge that one of the best tools to tackle the climate crisis and keep our temperatures from rising above 1.5C is to plant trees. If you contribute to our forest you’ll be creating employment for local families and restoring wildlife habitats.

You can buy trees at for our forest here offset.earth/treeware

Read more about Treeware at treeware.earth

Special Thanks to

laravel-google-analytics-4-measurement-protocol's People

Contributors

gauravmak avatar hirenkeraliya avatar jaggy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

laravel-google-analytics-4-measurement-protocol's Issues

SSL issue

I'm running Ubuntu 22.04 and php8.1. on Calling postEvent, it fails with an SSL error, like this

[previous exception] [object] (ErrorException(code: 0): fread(): SSL operation failed with code 1. OpenSSL Error messages:
error:0A000126:SSL routines::unexpected eof while reading at /mnt/sdb1/releases/20220411151410/vendor/guzzlehttp/psr7/src/Stream.php:232)
[stacktrace]
#0 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError()
#1 /mnt/sdb1/releases/20220411151410/vendor/guzzlehttp/psr7/src/Stream.php(232): fread()
#2 /mnt/sdb1/releases/20220411151410/vendor/guzzlehttp/psr7/src/Utils.php(53): GuzzleHttp\\Psr7\\Stream->read()
#3 /mnt/sdb1/releases/20220411151410/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php(210): GuzzleHttp\\Psr7\\Utils::copyToStream()
#4 /mnt/sdb1/releases/20220411151410/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php(140): GuzzleHttp\\Handler\\StreamHandler->drain()
#5 /mnt/sdb1/releases/20220411151410/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php(56): GuzzleHttp\\Handler\\StreamHandler->createResponse()
#6 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Http/Client/PendingRequest.php(944): GuzzleHttp\\Handler\\StreamHandler->__invoke()
#7 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Http/Client/PendingRequest.php(914): Illuminate\\Http\\Client\\PendingRequest->Illuminate\\Http\\Client\\{closure}()
#8 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Http/Client/PendingRequest.php(900): Illuminate\\Http\\Client\\PendingRequest->Illuminate\\Http\\Client\\{closure}()
#9 /mnt/sdb1/releases/20220411151410/vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php(64): Illuminate\\Http\\Client\\PendingRequest->Illuminate\\Http\\Client\\{closure}()
#10 /mnt/sdb1/releases/20220411151410/vendor/guzzlehttp/guzzle/src/Middleware.php(37): GuzzleHttp\\PrepareBodyMiddleware->__invoke()
#11 /mnt/sdb1/releases/20220411151410/vendor/guzzlehttp/guzzle/src/RedirectMiddleware.php(71): GuzzleHttp\\Middleware::GuzzleHttp\\{closure}()
#12 /mnt/sdb1/releases/20220411151410/vendor/guzzlehttp/guzzle/src/Middleware.php(61): GuzzleHttp\\RedirectMiddleware->__invoke()
#13 /mnt/sdb1/releases/20220411151410/vendor/guzzlehttp/guzzle/src/HandlerStack.php(75): GuzzleHttp\\Middleware::GuzzleHttp\\{closure}()
#14 /mnt/sdb1/releases/20220411151410/vendor/guzzlehttp/guzzle/src/Client.php(331): GuzzleHttp\\HandlerStack->__invoke()
#15 /mnt/sdb1/releases/20220411151410/vendor/guzzlehttp/guzzle/src/Client.php(168): GuzzleHttp\\Client->transfer()
#16 /mnt/sdb1/releases/20220411151410/vendor/guzzlehttp/guzzle/src/Client.php(187): GuzzleHttp\\Client->requestAsync()
#17 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Http/Client/PendingRequest.php(767): GuzzleHttp\\Client->request()
#18 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Http/Client/PendingRequest.php(698): Illuminate\\Http\\Client\\PendingRequest->sendRequest()
#19 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Support/helpers.php(234): Illuminate\\Http\\Client\\PendingRequest->Illuminate\\Http\\Client\\{closure}()
#20 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Http/Client/PendingRequest.php(712): retry()
#21 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Http/Client/PendingRequest.php(595): Illuminate\\Http\\Client\\PendingRequest->send()
#22 /mnt/sdb1/releases/20220411151410/vendor/freshbitsweb/laravel-google-analytics-4-measurement-protocol/src/GA4MeasurementProtocol.php(50): Illuminate\\Http\\Client\\PendingRequest->post()
#23 /mnt/sdb1/releases/20220411151410/app/Http/Controllers/PaymentController.php(198): Freshbitsweb\\LaravelGoogleAnalytics4MeasurementProtocol\\GA4MeasurementProtocol->postEvent()
#24 /mnt/sdb1/releases/20220411151410/app/Http/Controllers/PaymentController.php(51): App\\Http\\Controllers\\PaymentController->handleResponse()
#25 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): App\\Http\\Controllers\\PaymentController->returnLhv()
#26 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction()
#27 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Routing/Route.php(262): Illuminate\\Routing\\ControllerDispatcher->dispatch()
#28 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Routing/Route.php(205): Illuminate\\Routing\\Route->runController()
#29 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Routing/Router.php(721): Illuminate\\Routing\\Route->run()
#30 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}()
#31 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(50): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#32 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle()
#33 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#34 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle()
#35 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(121): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#36 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(64): Illuminate\\Session\\Middleware\\StartSession->handleStatefulRequest()
#37 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Session\\Middleware\\StartSession->handle()
#38 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#39 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle()
#40 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(67): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#41 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle()
#42 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#43 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Routing/Router.php(723): Illuminate\\Pipeline\\Pipeline->then()
#44 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Routing/Router.php(698): Illuminate\\Routing\\Router->runRouteWithinStack()
#45 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Routing/Router.php(662): Illuminate\\Routing\\Router->runRoute()
#46 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Routing/Router.php(651): Illuminate\\Routing\\Router->dispatchToRoute()
#47 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(167): Illuminate\\Routing\\Router->dispatch()
#48 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}()
#49 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#50 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php(31): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#51 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle()
#52 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#53 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php(40): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#54 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TrimStrings->handle()
#55 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#56 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle()
#57 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(86): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#58 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle()
#59 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#60 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(142): Illuminate\\Pipeline\\Pipeline->then()
#61 /mnt/sdb1/releases/20220411151410/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(111): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter()
#62 /mnt/sdb1/releases/20220411151410/public/index.php(52): Illuminate\\Foundation\\Http\\Kernel->handle()
#63 {main}
"}

Seemingly, the request to GA4 is incomplete or the postEvent does not do this correctly? What would be the fix to this?

Need update

Can you update illuminate/http to 9.0 and illuminate/view to 9.0?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.