Send request to you Google Analytics has never been easier.
You can install the package via composer:
composer require marshmallow/google-analytics
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
{
//
}
}
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);
}
}
Add the GoogleEcommerceTrigger
listner to your EventServiceProvider.php
.
OrderCreated::class => [
GoogleEcommerceTrigger::class
]
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);
}
}
}
Below you find an overview of the different Google Types we currently support and how you can use them.
$pageview = (new Pageview)
->hostname('marshmallow.dev')
->page('/home')
->title('homepage');
$analytics->pageview($pageview);
$event = (new Event)
->category('video')
->action('play')
->label('holiday')
->value(300);
$analytics->event($event);
$ecommerce = (new EcommerceTracking)
->id(123456)
->affiliation('westernWear')
->revenue(50.00)
->shipping(32.00)
->tax(12.00)
->currency('EUR');
$analytics->ecommerceTracking($ecommerce);
$item1 = (new ItemHit)
->id(12345)
->name('sofa')
->price(300)
->quantity(2)
->code('u3eqds43')
->variation('furniture')
->currency('EUR');
$analytics->itemHit($item1);
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();
Please see CHANGELOG for more information what has changed recently.
If you discover any security related issues, please email stef@marshmallow.dev instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.