Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

Laravel Analytics v2 has been released

Original – by Freek Van der Herten – 2 minute read

One of our more popular packages is laravel-analytics. The package makes it easy to fetch information such as pageviews, top referrers, etc... from the Google Analytics API. In our Blender-based projects we use the fetched data to display a nice chart in the admin section: Screen Shot 2016-06-19 at 19.33.53

Laravel-analytics is one of the first packages we ever created. Since the first release the preferred way of authenticating with Google has changed. We also learned a lot on how to create readable code. That's why we did a cleanup of the code and published a new major version of the package.

Installation

Learning how to authenticate with a Google API can be quite a pain. A previous post already touched on this:

If you’ve ever worked with some API’s by Google then you know their documentation can be very confusing. It’s not that they don’t have documentation, but code examples of common use cases are simply not present. You must wade through a lot of pages to learn basic things such as how to make an authorized request.

To prevent potential tableflips on your part there's some documentation included to guide you through Google's authentication process.

Usage

Once the package is installed this is how to retrieve visitor and pageview data for the past week.

$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));

$analyticsData is a collection in which each item is an array that holds keys date, visitors and pageViews.

These are the other functions the package provides:

public function fetchMostVisitedPages(Period $period, int $maxResults = 20): Collection;

public function fetchTopReferrers(Period $period, int $maxResults = 20): Collection;

public function fetchTopBrowsers(Period $period, int $maxResults = 10): Collection;

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

public function performQuery(Period $period, string $metrics, array $others = [])


Check out the documentation on GitHub to learn more. If you like laravel-analtyics, be sure to check out our other Laravel packages as well.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

What are your thoughts on "Laravel Analytics v2 has been released"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.