PHP code example of marshmallow / google-analytics

1. Go to this page and download the library: Download marshmallow/google-analytics 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/ */

    

marshmallow / google-analytics example snippets


namespace App\Events;

use Marshmallow\GoogleAnalytics\GoogleAnalytics;
use Marshmallow\GoogleAnalytics\Contracts\GoogleAnalyticsEvent;

class OrderCreated implements GoogleAnalyticsEvent
{
    //...
    
    public function withAnalytics(GoogleAnalytics $analytics): GoogleAnalytics
    {
        //
    }
}

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);
    }
}

OrderCreated::class => [
    GoogleEcommerceTrigger::class
]

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);
        }
    }
}

$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);


$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();