Most popular posts with Laravel and Google Analytics

popular posts with laravel and google analytics

In this post, I will explain how to retrieve data from Google Analytics. In this tutorial we will use laravel-analytics package by Spatie. So, let’s get started by installing the package.

Step 1: Install Package

The most easiest way to install package is using composer. To install this, simply run the below command:

In Laravel 5.5 and above, you don’t need to register the service provider but, below Laravel 5.5 version, you need to register service provider in config/app.php

Similarly, add Facade in alias section.

To configure your configuration for this package, you can publish this package. You can publish using below command:

This will publish a configuration file. Sample of the configuration file looks like below:

Add view_id in your .env file

Step 2: Obtain Google Analytics Credentials

Getting API Credentials

We will need Google API’s credentials to fetch our data. You can get clear documentation at it’s github repo, but I will explain step by step.

Go the Google’s API page and select your project. If you haven’t create any, create a new one.
google analytics create project

Now, click on “Enable APIs and Services”.
enable google api

Search for “Google Analytics Reporting API”
Google Analytics API

Create a “Service account key”. This will download a json file. Please save it safely.
generate service amount key

Now, create a folder named analytics inside app directory and put the recently downloaded json file inside this folder. Please note that, this file contain sensitive data.

Granting permissions to your Analytics

Go to this Google Suite and create a new account.
generate service amount key_002

The last thing you’ll have to do is fill in the view_id in the config file. You can get the right value on the Analytics site. Go to “View setting” in the Admin-section of the property.
google view id

Grab the view id and paste it inside your config/analytics.php in view_id section or define in .env file and access from here.

Great! we have successfully integrated the package in our application. It’s time to implement the package. Basic example of it’s usage is given below:

Note: Please note that, every method will return a Illuminate\Support\Collection instance.

Step 3: Usage

Let’s grab some popular post.

This will retrieve most popular blog post are return a collection.

Note: Please don’t forget to include use Analytics; and use Spatie\Analytics\Period;

Modifying Time limit

You can pass startDate and endDate to period object.

Available Methods

Visitors and pageviews

The function returns a Collection in which each item is an array that holds keys date, visitors, pageTitle and pageViews.

Total visitors and pageviews

The function returns a Collection in which each item is an array that holds keys date, visitors, and pageViews.

Most visited pages

The function returns a Collection in which each item is an array that holds keys url, pageTitle and pageViews.

Top referrers

The function returns a Collection in which each item is an array that holds keys url and pageViews.

User Types

The function returns a Collection in which each item is an array that holds keys type and sessions.

Top browsers

The function returns a Collection in which each item is an array that holds keys browser and sessions.

All other Google Analytics queries

To perform all other queries on the Google Analytics resource use performQuery. Google’s Core Reporting API provides more information on which metrics and dimensions might be used.

You can get access to the underlying Google_Service_Analytics object:

Hope you got ideas about installing this package and available methods. Enjoy!