PHP code example of darvis / livewire-google-analytics

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

    

darvis / livewire-google-analytics example snippets


// Before: Complex and error-prone ❌
$this->js("gtag('event', 'generate_lead', {...})");

// After: Simple and safe ✅
$this->trackLead(['form_name' => 'contact_form']);

use Darvis\LivewireGoogleAnalytics\Traits\TracksAnalytics;

class ContactForm extends Component
{
    use TracksAnalytics;

    public function submit()
    {
        $this->validate();
        Contact::create($this->all());
        
        // Track the conversion
        $this->trackLead([
            'form_name' => 'contact_form',
            'lead_type' => 'contact',
        ]);
        
        $this->success = true;
    }
}

$this->trackLead([
    'form_name' => 'contact_form',
    'lead_type' => 'contact',
]);

$this->trackEvent('purchase', [
    'transaction_id' => 'T12345',
    'value' => 25.99,
    'currency' => 'EUR',
]);

$this->trackNewsletterSignup([
    'source' => 'footer_widget',
]);

$this->trackCustomEvent('download_brochure', [
    'brochure_name' => 'Product Catalog 2024',
]);

class ContactForm extends Component
{
    use TracksAnalytics;

    public function submit()
    {
        $validated = $this->validate();
        Contact::create($validated);
        
        $this->trackLead([
            'form_name' => 'contact_form',
            'lead_type' => 'contact',
        ]);
        
        $this->success = true;
    }
}

class CheckoutForm extends Component
{
    use TracksAnalytics;

    public function completePurchase()
    {
        $order = Order::create([...]);
        
        $this->trackEvent('purchase', [
            'transaction_id' => $order->id,
            'value' => $order->total,
            'currency' => 'EUR',
        ]);
        
        return redirect()->route('order.success');
    }
}