Laravel package to easily send events to Google Analytics

Overview

Laravel Analytics Event Tracking

https://twitter.com/pascalbaljet/status/1257926601339277312

Latest Version on Packagist run-tests Quality Score Total Downloads Buy us a tree

Laravel package to easily send events to Google Analytics

Launcher 🚀

Hey! We've built a Docker-based deployment tool to launch apps and sites fully containerized. You can find all features and the roadmap on our website, and we are on Twitter as well!

Features

  • Use Laravel Events to track events with GA.
  • Blade Directive to easily store the Client ID.
  • Full access to the underlying library.
  • API calls to GA are queued.
  • Easy to configure.
  • Compatible with Laravel 8.0.
  • PHP 7.4 or higher required.

Support

We proudly support the community by developing Laravel packages and giving them away for free. Keeping track of issues and pull requests takes time, but we're happy to help! If this package saves you time or if you're relying on it professionally, please consider supporting the maintenance and development.

Installation

You can install the package via composer:

composer require protonemedia/laravel-analytics-event-tracking

Configuration

Publish the config and view files:

php artisan vendor:publish --provider="ProtoneMedia\AnalyticsEventTracking\ServiceProvider"

Set your Google Analytics Tracking ID in the .env file or in the config/analytics-event-tracking.php file.

GOOGLE_ANALYTICS_TRACKING_ID=UA-01234567-89

This package supports Google Analytics 4 as of version 1.2.1. Please republish the view file if you're upgrading to a new Google Analytics 4 property.

Blade Directive

This package comes with a @sendAnalyticsClientId directive that sends the Client ID from the GA front-end to your Laravel backend and stores it in the session.

It uses the Axios HTTP library the make an asynchronous POST request. Axios was choosen because it is provided by default in Laravel in the resources/js/bootstrap.js file.

Add the directive somewhere after initializing/configuring GA. The POST request will only be made if the Client ID isn't stored yet or when it's refreshed.

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-01234567-89"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'UA-01234567-89', { 'send_page_view': false });
  gtag('event', 'page_view', { 'event_callback': function() {
      @sendAnalyticsClientId
  }});
</script>

If you don't use Axios, you have to implement this call by yourself. By default the endpoint is /gaid but you can customize it in the configuration file. The request is handled by the ProtoneMedia\AnalyticsEventTracking\Http\StoreClientIdInSession class. Make sure to also send the CSRF token.

Broadcast events to Google Analytics

Add the ShouldBroadcastToAnalytics interface to your event and you're ready! You don't have to manually bind any listeners.

<?php

namespace App\Events;

use App\Order;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use ProtoneMedia\AnalyticsEventTracking\ShouldBroadcastToAnalytics;

class OrderWasCreated implements ShouldBroadcastToAnalytics
{
    use Dispatchable, SerializesModels;

    public $order;

    public function __construct(Order $order)
    {
        $this->order = $order;
    }
}

Handle framework and 3rd-party events

If you want to handle events where you can't add the ShouldBroadcastToAnalytics interface, you can manually register them in your EventServiceProvider using the DispatchAnalyticsJob listener.

<?php

namespace App\Providers;

use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use ProtoneMedia\AnalyticsEventTracking\Listeners\DispatchAnalyticsJob;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
            DispatchAnalyticsJob::class,
        ],
    ];
}

Customize the broadcast

There are two additional methods that lets you customize the call to Google Analytics.

With the withAnalytics method you can interact with the underlying package to set additional parameters. Take a look at the TheIconic\Tracking\GoogleAnalytics\Analytics class to see the available methods.

With the broadcastAnalyticsActionAs method you can customize the name of the Event Action. By default we use the class name with the class's namespace removed. This method gives you access to the underlying Analytics class as well.

<?php

namespace App\Events;

use App\Order;
use TheIconic\Tracking\GoogleAnalytics\Analytics;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use ProtoneMedia\AnalyticsEventTracking\ShouldBroadcastToAnalytics;

class OrderWasCreated implements ShouldBroadcastToAnalytics
{
    use Dispatchable, SerializesModels;

    public $order;

    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    public function withAnalytics(Analytics $analytics)
    {
        $analytics->setEventValue($this->order->sum_in_cents / 100);
    }

    public function broadcastAnalyticsActionAs(Analytics $analytics)
    {
        return 'CustomEventAction';
    }
}

Handling the Client ID outside a HTTP Request

You might want to track an event that occurs outside of a HTTP Request, for example in a queued job or while handling a 3rd-party callback/webhook. Let's continue with the Order example. When the Order is created, you could save the Client ID in the database.

<?php

namespace App\Http\Controllers;

use App\Order;
use App\Http\Requests\CreateOrderRequest;
use ProtoneMedia\AnalyticsEventTracking\Http\ClientIdRepository;

class CreateOrderController
{
    public function __invoke(CreateOrderRequest $request, ClientIdRepository $clientId)
    {
        $attributes = $request->validated();

        $attributes['google_analytics_client_id'] = $clientId->get();

        return Order::create($attributes);
    }
}

When you receive a webhook from your payment provider and you dispatch an OrderWasPaid event, you can use the withAnalytics method in your event to reuse the google_analytics_client_id:

<?php

namespace App\Events;

use App\Order;
use TheIconic\Tracking\GoogleAnalytics\Analytics;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use ProtoneMedia\AnalyticsEventTracking\ShouldBroadcastToAnalytics;

class OrderWasPaid implements ShouldBroadcastToAnalytics
{
    use Dispatchable, SerializesModels;

    public $order;

    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    public function withAnalytics(Analytics $analytics)
    {
        $analytics->setClientId($this->order->google_analytics_client_id);
    }
}

Additional configuration

You can configure some additional settings in the config/analytics-event-tracking.php file:

  • use_ssl: Use SSL to make calls 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
  • client_id_session_key: The session key to store the Client ID
  • http_uri: HTTP URI to post the Client ID to (from the Blade Directive)

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Other Laravel packages

  • Laravel Blade On Demand: Laravel package to compile Blade templates in memory.
  • Laravel Cross Eloquent Search: Laravel package to search through multiple Eloquent models.
  • Laravel Eloquent Scope as Select: Stop duplicating your Eloquent query scopes and constraints in PHP. This package lets you re-use your query scopes and constraints by adding them as a subquery.
  • Laravel Eloquent Where Not: This Laravel package allows you to flip/invert an Eloquent scope, or really any query constraint.
  • Laravel FFMpeg: This package provides an integration with FFmpeg for Laravel. The storage of the files is handled by Laravel's Filesystem.
  • Laravel Form Components: Blade components to rapidly build forms with Tailwind CSS Custom Forms and Bootstrap 4. Supports validation, model binding, default values, translations, includes default vendor styling and fully customizable!
  • Laravel Mixins: A collection of Laravel goodies.
  • Laravel Paddle: Paddle.com API integration for Laravel with support for webhooks/events.
  • Laravel Verify New Email: This package adds support for verifying new email addresses: when a user updates its email address, it won't replace the old one until the new one is verified.
  • Laravel WebDAV: WebDAV driver for Laravel's Filesystem.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Treeware

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

You might also like...
A simple PHP package for sending messages to Slack, with a focus on ease of use and elegant syntax.

Slack for PHP | A simple PHP package for sending messages to Slack with incoming webhooks, focused on ease-of-use and elegant syntax. supports: PHP 7.

Meta package tying together all the key packages of the Symfony CMF project.

This repository is no longer maintained Due to lack of interest, we had to decide to discontinue this repository. The CMF project focusses on the Rout

Package Repository Website - try https://packagist.com if you need your own -

Packagist Package Repository Website for Composer, see the about page on packagist.org for more. This project is not meant for re-use. It is open sour

Phalcon PHP REST API Package, still in beta, please submit issues or pull requests

PhREST API A Phalcon REST API package, based on Apigees guidelines as found in http://apigee.com/about/content/web-api-design Please see the skeleton

This package makes it easy for developers to access WhatsApp Cloud API service in their PHP code.
This package makes it easy for developers to access WhatsApp Cloud API service in their PHP code.

The first PHP API to send and receive messages using a cloud-hosted version of the WhatsApp Business Platform

PHP Package for Autentique API-v2

PHP Package for Autentique API-v2

Laravel API 文档生成器,可以将基于 Laravel 项目的项目代码,自动生成 json 或 md 格式的描述文件。

Thresh Laravel API 文档生成器,可以将基于 Laravel 项目的项目代码,自动生成 json 或 md 格式的描述文件。 安装 $ composer require telstatic/thresh -vvv 功能 生成 Markdown 文档 生成 Postman 配置文件 生

Laravel api tool kit is a set of tools that will help you to build a fast and well-organized API using laravel best practices.
Laravel api tool kit is a set of tools that will help you to build a fast and well-organized API using laravel best practices.

Laravel API tool kit and best API practices Laravel api tool kit is a set of tools that will help you to build a fast and well-organized API using lar

A simple way of authenticating your RESTful APIs with API keys using Laravel

ApiGuard This package is no longer maintained This package is no longer maintained as Laravel already has a similar feature built-in since Laravel 5.8

Comments
  • Can't find ga

    Can't find ga

    Hi,

    I'm using the packages as described in the docs, and added a CDN to axios since we don't use axois.

    <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js"
                    integrity="sha512-bZS47S7sPOxkjU/4Bt0zrhEtWx0y0CRkhEp8IckzK+ltifIIE9EMIMTuT/mEzoIMewUINruDBIR/jJnbguonqQ=="
                    crossorigin="anonymous"></script>
            <!-- Global site tag (gtag.js) - Google Analytics -->
            <script async
                    src="https://www.googletagmanager.com/gtag/js?id={{ config('analytics-event-tracking.tracking_id') }}"></script>
            <script>
                window.dataLayer = window.dataLayer || [];
    
                function gtag() {
                    dataLayer.push(arguments);
                }
    
                gtag('js', new Date());
                gtag('config', '{{ config('analytics-event-tracking.tracking_id') }}', { 'send_page_view': false });
                gtag('event', 'page_view', {
                    'event_callback': function () {
                    @sendAnalyticsClientId
                    }
                });
            </script>
    

    When I load the page, I get the message ReferenceError: Can't find variable: ga in the console and the event is not sent to analytics. Do need to include another library?

    Thank you in advance!

    opened by stefro 8
  • Events Not Broadcasting

    Events Not Broadcasting

    We are utilizing this package in order to send events to GA from our app. We have the events set up in a few queued events. However, no events are reaching GA.

    Everything is set up according to the readme. We can watch the queue processing, and the events firing. We also have no errors in the logs.

    Event

    <?php
    
    namespace App\Events;
    
    use App\User;
    use Illuminate\Broadcasting\Channel;
    use Illuminate\Broadcasting\InteractsWithSockets;
    use Illuminate\Broadcasting\PresenceChannel;
    use Illuminate\Broadcasting\PrivateChannel;
    use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
    use Illuminate\Foundation\Events\Dispatchable;
    use Illuminate\Queue\SerializesModels;
    use ProtoneMedia\AnalyticsEventTracking\ShouldBroadcastToAnalytics;
    use TheIconic\Tracking\GoogleAnalytics\Analytics;
    
    class NewSignup implements ShouldBroadcastToAnalytics
    {
        use Dispatchable, InteractsWithSockets, SerializesModels;
    
        public $user;
    
        /**
         * Create a new event instance.
         *
         * @return void
         */
        public function __construct(User $user)
        {
            $this->user = $user;
        }
    
        /**
         * adds event values to analytics call
         * @param  Analytics $analytics
         * @return void
         */
        public function withAnalytics(Analytics $analytics)
        {
            $analytics->setEventValue($this->user->email);
        }
    
        /**
         * sends GA tracking event
         * @param  Analytics $analytics
         * @return string
         */
        public function broadcastAnalyticsActionAs(Analytics $analytics)
        {
            return 'NewSignup';
        }
    }
    

    Config

    <?php
    
    return [
        /**
         * Your GA Tracking ID.
         * https://support.google.com/analytics/answer/1008080
         */
        'tracking_id' => env('GOOGLE_ANALYTICS_TRACKING_ID'),
    
        /**
         * Use SSL to make calls to GA.
         */
        'use_ssl' => true,
    
        /**
         * Anonymizes the last digits of the user's IP.
         */
        'anonymize_ip' => true,
    
        /**
         * Send the ID of the authenticated user to GA.
         */
        'send_user_id' => false,
    
        /*
         * This queue will be used to perform the API calls to GA.
         * Leave empty to use the default queue.
         */
        'queue_name' => '',
    
        /**
         * The session key to store the Client ID.
         */
        'client_id_session_key' => 'analytics-event-tracker-client-id',
    
        /**
         * HTTP URI to post the Client ID to (from the Blade Directive).
         */
        'http_uri' => '/gaid',
    
    ];
    

    Can you tell us the best way to debug this?

    opened by dougblackjr 2
  • Help with an event triggered by a webhook

    Help with an event triggered by a webhook

    First of all thank you for the package. I am trying to trigger a GA event after a webhook is called.

    The webhook is creating a job that is put in a queue and when the que is processed, the call to GA is made. I've made a custom GA4 property and I can test page views events from the frontend just fine but when the Laravel event is broadcasted, the event is not visible in GA dashboard.

    Here is what I get back from GA:

      #httpStatusCode: 200
      #requestUrl: "https://www.google-analytics.com/debug/collect?v=1&tid=UA-XXXXXX-1&aip=1&cid=339915999.1664272553&ea=webhook_accessed&ec=By%20webhook&el=Webhook&ev=100&ti=1252&t=event"
      #responseBody: """
        {\n
          "hitParsingResult": [ {\n
            "valid": true,\n
            "parserMessage": [ {\n
              "messageType": "INFO",\n
              "description": "IP Address from this hit was anonymized to 83.167.220.0.",\n
              "messageCode": "VALUE_MODIFIED"\n
            } ],\n
            "hit": "/debug/collect?v=1\u0026tid=UA-XXXXXX-1\u0026aip=1\u0026cid=339915999.1664272553\u0026ea=webhook_accessed\u0026ec=By%20webhook\u0026el=Webhook\u0026ev=100\u0026ti=1252\u0026t=event"\n
          } ],\n
          "parserMessage": [ {\n
            "messageType": "INFO",\n
            "description": "Found 1 hit in the request."\n
          } ]\n
        }
    

    The above code is the result of $this->analytics->sendEvent()

    Now, I understand that doing a POST to /debug/collect will not make the event visible in the dashboard but even if I POST to the url without /debug is still not recording the event. Any help is appreciated.

    opened by maxi032 0
  • DispatchAnalyticsJob events not appearing in GA

    DispatchAnalyticsJob events not appearing in GA

    Hello,

    I've been trying to implement DispatchAnalyticsJob in the Registered event, but am having trouble getting the event to show up in analytics.

        protected $listen = array(
            Registered::class => array(
                SendEmailVerificationNotification::class,
                DispatchAnalyticsJob::class,
            ),
        );
    
    

    Here's a little background:

    • This is a GA4 property.
    • The project is using Laravel Jetstream with Inertia + Vue.
    • Pageviews and other events sent from gtag() are working as expected.
    • While debugging, I determined the Registered event is being sent to GA and the response code is 200.
    • In Laravel Horizon, I can see the "SendEventToAnalytics" job is completed successfully.

    I reviewed Issue #2, which isn't entirely applicable in this case, but I was wondering if DispatchAnalyticsJob would handle setting the eventValue correctly on its own?

    The other possibility I was considering is that Google is filtering the event as a bot. With GA4 properties, there's no way to disable bot filtering as of now. Has anyone experienced this being an issue?

    Thanks for the work on this package and any advice you can provide!

    opened by timstl 0
Releases(1.5.2)
Owner
Protone Media
We are a Dutch software company that develops apps, websites, and cloud platforms. As we're building projects, we gladly contribute to OSS by sharing our work.
Protone Media
Laravel package to easily update business KPI monitors for your software or service

Laravel package to easily update business KPI monitors for your software or service. Monitors can be exposed to your user community via mobile app or embedded into your own application.

Heinz Seldte 2 Dec 12, 2022
Transporter is a futuristic way to send API requests in PHP

Transporter Transporter is a futuristic way to send API requests in PHP. This is an OOP approach to handle API requests.

Steve McDougall 369 Dec 22, 2022
Disable Google's FLoC with help of PSR-15 middleware

Disable Google's FLoC with PSR-15 middleware This package will help you disable Google's FLoC. Installation You can install the package via composer:

P7V 9 Dec 14, 2022
Quickly and easily expose Doctrine entities as REST resource endpoints with the use of simple configuration with annotations, yaml, json or a PHP array.

Drest Dress up doctrine entities and expose them as REST resources This library allows you to quickly annotate your doctrine entities into restful res

Lee Davis 88 Nov 5, 2022
Easily integrate custom-made NPS (Net Promoter Score) into your application

Laravel NPS Easily integrate custom-made NPS (Net Promoter Score) to your application. Installation You can install the package via composer: composer

H-FARM Innovation 48 Oct 27, 2022
A Laravel Fractal package for building API responses, giving you the power of Fractal with Laravel's elegancy.

Laravel Responder is a package for building API responses, integrating Fractal into Laravel and Lumen. It can transform your data using transformers,

Alexander Tømmerås 776 Dec 25, 2022
A RESTful API package for the Laravel and Lumen frameworks.

The Dingo API package is meant to provide you, the developer, with a set of tools to help you easily and quickly build your own API. While the goal of

null 9.3k Jan 7, 2023
Official Laravel package for thepeer

Thepeer Laravel SDK Installation composer install thepeer/sdk Usage Initiate <?php $thepeer = new \Thepeer\Sdk\Thepeer("your-secret-key"); Available

The Peer 26 Oct 7, 2022
AWS Cognito package using the AWS SDK for PHP/Laravel

Laravel Package to manage Web and API authentication with AWS Cognito AWS Cognito package using the AWS SDK for PHP This package provides a simple way

EllaiSys 74 Nov 15, 2022
JSON API (jsonapi.org) package for Laravel applications.

cloudcreativity/laravel-json-api Status This package has now been rewritten, substantially improved and released as the laravel-json-api/laravel packa

Cloud Creativity 753 Dec 28, 2022