Giter VIP home page Giter VIP logo

analytics-reports's Introduction

Retrieve data from Google Analytics

Latest Stable Version Total Downloads License

This is an opinionated Laravel 4 package to retrieve Google Analytics data.

###Laravel 5 If you're using Laravel 5, take a look at this package.

Installation

This package can be installed through Composer.

composer require spatie/analytics-reports

You must install this service provider.

// app/config/app.php

'providers' => [
    '...',
    'Spatie\AnalyticsReports\AnalyticsReportsServiceProvider'
];

This package also comes with a facade, which provides an easy way to call the the class.

// app/config/app.php

'aliases' => array(
	...
	'AnalyticsReports' => 'Spatie\AnalyticsReports\AnalyticsReportsFacade',
)

Although the composer.json of this package specifies that "google/apiclient" AND "thujohn/analytics" must be pulled in as well, it sometimes fails to do so. If you encouter this problem as well include these lines in your composer.json as well:

{
    "require": {
    ...
        "google/apiclient" : "1.1.*",
        "thujohn/analytics": "dev-master",
	...
	}
}

You can publish the config file of the package using artisan

php artisan config:publish spatie/analytics-reports

After the config file has been published you'll manually have to move it to your app's config-folder. (Hopefully this step won't be necessary in a next version)

To move the config file copy the config.php file from

/app/config/packages/spatie/analytics-reports/

and paste it to your /app/config folder and rename it to analyticsReports.php

In the config file you can specify two values:

  • siteId: the Google site id, something in the form of ga:xxxxxxxx
  • cacheLifeTime: the amount of minutes the Google API responses will be cached. If you set this value to zero, the responses won't be cached at all.

Internally this package uses thujohn/analytics-l4 to authenticate with Google. So in order to use this package you must also follow their installation instructions.

Usage

When the installation is done you can easily retrieve Analytics data. Mostly all methods will return an Illuminate\Support\Collection-instance.

Here is an example to retrieve visitors and pageview data for the last seven days.

/*
* $analyticsData now contains a Collection with 3 columns: "date", "visitors" and "pageViews"
*/
$analyticsData = AnalyticsReports::getVisitorsAndPageViews(7)

Here's another example to get the 20 most visited pages of the last 365 days

/*
* $analyticsData now contains a Collection with 2 columns: "url" and "pageViews"
*/
$analyticsData = AnalyticsReports::getMostVisitedPages(365, 20)

Provided methods

###Visitors and Pageviews These methods return a Collection with columns "date", "vistors" and "pageViews". When grouping by yearMonth, the first column will be called "yearMonth".

    /**
     * Get the amount of visitors and pageviews
     *
     * @param int $numberOfDays
     * @param string $groupBy Possible values: date, yearMonth
     * @return Collection
     */
    public function getVisitorsAndPageViews($numberOfDays = 365, $groupBy = 'date')

    /**
     * Get the amount of visitors and pageviews for the given period
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     * @param string $groupBy Possible values: date, yearMonth
     * @return Collection
     */
    public function getVisitorsAndPageViewsForPeriod($startDate, $endDate, $groupBy = 'date')

###Keywords These methods return a Collection with columns "keyword" and "sessions".

   /**
     * Get the top keywords
     *
     * @param int $numberOfDays
     * @param int $maxResults
     * @return Collection
     */
    public function getTopKeywords($numberOfDays = 365, $maxResults = 30)

    /**
     * Get the top keywords for the given period
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     * @param int $maxResults
     * @return Collection
     */
    public function getTopKeyWordsForPeriod($startDate, $endDate, $maxResults = 30)

###Referrers These methods return a Collection with columns "url" and "pageViews".

    /**
     * Get the top referrers
     *
     * @param int $numberOfDays
     * @param int $maxResults
     * @return Collection
     */
    public function getTopReferrers($numberOfDays = 365, $maxResults = 20)

    /**
     * Get the top referrers for the given period
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     * @param $maxResults
     * @return Collection
     */
    public function getTopReferrersForPeriod($startDate, $endDate, $maxResults)

###Browsers These methods return a Collection with columns "browser" and "sessions".

If there are more used browsers than the number specified in maxResults, then a new resultrow with browser-name "other" will be appended with a sum of all the remaining browsers.

    /**
     * Get the top browsers
     *
     * @param int $numberOfDays
     * @param int $maxResults
     * @return Collection
     */
    public function getTopBrowsers($numberOfDays = 365, $maxResults = 6)
    
    /**
     * Get the top browsers for the given period
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     * @param $maxResults
     * @return Collection
     */
    public function getTopBrowsersForPeriod($startDate, $endDate, $maxResults) 

###Most visited pages These methods return a Collection with columns "url" and "pageViews".

    /**
     * Get the most visited pages
     *
     * @param int $numberOfDays
     * @param int $maxResults
     * @return Collection
     */
    public function getMostVisitedPages($numberOfDays = 365, $maxResults = 20)
    
    /**
     * Get the most visited pages for the given period
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     * @param int $maxResults
     * @return Collection
     */
    public function getMostVisitedPagesForPeriod($startDate, $endDate, $maxResults = 20)

###All other Google Analytics Queries To perform all other GA queries use performQuery. Google's Core Reporting API provides more information on on which metrics and dimensions might be used.

    /**
     * Call the query method on the autenthicated client
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     * @param $metrics
     * @param array $others
     * @return mixed
     */
    public function performQuery($startDate, $endDate, $metrics, $others = array())

##Convenience methods getSiteIdByUrl can be used to get the site id for the given url

    /**
     * Returns the site id (ga:xxxxxxx) for the given url
     *
     * @param $url
     * @throws \Exception
     * @return string
     */
    public function getSiteIdByUrl($url)

analytics-reports's People

Contributors

freekmurze avatar innesm4 avatar jbrooksuk avatar jonasva avatar wazirsoft avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.