Laravel Analytics is a package that we can use to integrate Google Analytics to our Laravel applications.

google analytics

Quickstart

composer require ipunkt/laravel-analytics

Add to providers in config/app.php:

Ipunkt\LaravelAnalytics\AnalyticsServiceProvider::class,

Add to aliases in config/app.php:

'Analytics' => Ipunkt\LaravelAnalytics\AnalyticsFacade::class,

To your .env add these variables and set them to your liking:

ANALYTICS_PROVIDER=GoogleAnalytics
ANALYTICS_TRACKING_ID=your-tracking-id

Finally, just above your closing tag place, this code:

{!! Analytics::render() !!}

You now have Google Analytics working. Enjoy!

Usage

In controller action (or anywhere else) use following statement to track an event or page view:

//  tracking the current page view
Analytics::trackPage(); // only necessary if `auto_track` is false or Analytics::disableAutoTracking() was called before

//  tracking an event
Analytics::trackEvent('category', 'action');

//  tracking a custom line
Analytics::trackCustom("ga('send', ......"); // this line will be added within the tracking script

In your view or layout template (e.g. a blade template) use the following statement:

{!! Analytics::render() !!}

For Google Analytics you should place the statement right behind the body tag

<body>{!! Analytics::render() !!}