marshmallow / google-analytics
Sending request to you Google Analytics has never been easier. With support for e-commerce tracking.
Installs: 4 571
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 1
Requires
- php: ^7.0|^8.0
- marshmallow/helpers: ^2.0
This package is auto-updated.
Last update: 2024-12-05 11:06:51 UTC
README
Laravel Google Analytics Package
Send request to you Google Analytics has never been easier.
Installation
You can install the package via composer:
composer require marshmallow/google-analytics
Usage
Use with an event listener
The easiest way to use this package is to use Events and Listners. For instance. If you want to use the ecommerce tracking your should create an event like OrderCreated
. This event should implement GoogleAnalyticsEvent
. This will require you to implement the withAnalytics
method.
namespace App\Events; use Marshmallow\GoogleAnalytics\GoogleAnalytics; use Marshmallow\GoogleAnalytics\Contracts\GoogleAnalyticsEvent; class OrderCreated implements GoogleAnalyticsEvent { //... public function withAnalytics(GoogleAnalytics $analytics): GoogleAnalytics { // } }
The withAnalytics method
withAnalytics
will provide you with an instance of GoogleAnalytics
where you can add the Google Types
you wish to send to Google Analytics. Below is an example of how this could look.
class OrderCreated implements GoogleAnalyticsEvent { protected $order; public function __construct(Order $order) { $this->order = $order; } public function withAnalytics(GoogleAnalytics $analytics): GoogleAnalytics { $ecommerce = (new EcommerceTracking) ->id($this->order->id) ->affiliation(env('APP_NAME')) ->revenue($this->order->revenue()) ->shipping($this->order->shippingCost()) ->tax($this->order->tax()) ->currency('EUR'); return $analytics->ecommerceTracking($ecommerce); } }
Register the Listener
Add the GoogleEcommerceTrigger
listner to your EventServiceProvider.php
.
OrderCreated::class => [ GoogleEcommerceTrigger::class ]
Concatunate types
You can add as many types as you want. To stay with the example above, lets say you want to let Google Analytics now which products where sold with this order.
class OrderCreated implements GoogleAnalyticsEvent { protected $order; public function __construct(Order $order) { $this->order = $order; } public function withAnalytics(GoogleAnalytics $analytics): GoogleAnalytics { $analytics->ecommerceTracking($ecommerce); foreach ($this->order->products as $product) { $hitItem = (new ItemHit) ->id($this->order->id) ->name($product->name()) ->price($product->price()) ->quantity($product->quantity()) ->code($product->sku()) ->variation($product->category->name) ->currency('EUR'); $analytics->itemHit($hitItem); } } }
Google Event Types
Below you find an overview of the different Google Types we currently support and how you can use them.
Event
$pageview = (new Pageview) ->hostname('marshmallow.dev') ->page('/home') ->title('homepage'); $analytics->pageview($pageview);
Event
$event = (new Event) ->category('video') ->action('play') ->label('holiday') ->value(300); $analytics->event($event);
Ecommerce Tracking
$ecommerce = (new EcommerceTracking) ->id(123456) ->affiliation('westernWear') ->revenue(50.00) ->shipping(32.00) ->tax(12.00) ->currency('EUR'); $analytics->ecommerceTracking($ecommerce);
Ecommerce item (Item Hit)
$item1 = (new ItemHit) ->id(12345) ->name('sofa') ->price(300) ->quantity(2) ->code('u3eqds43') ->variation('furniture') ->currency('EUR'); $analytics->itemHit($item1);
Stand alone example
If you don't want to use this with an event and listner, you can use it stand alone as well.
$pageview = (new Pageview) ->hostname('marshmallow.dev') ->page('/home') ->title('homepage'); (new GoogleAnalytics()) ->version(1) ->trackingId(env('SEO_GA')) ->anonymousClientId() ->pageview($pageview) /** * Call the sendToGoogle method at the end */ sendToGoogle();
Changelog
Please see CHANGELOG for more information what has changed recently.
Security
If you discover any security related issues, please email stef@marshmallow.dev instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.