PHP code example of spatie / analytics-statistics

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

    

spatie / analytics-statistics example snippets


/**
 * @param  string $siteId
 * @param  string $clientId
 * @param  string $serviceEmail
 * @param  string $certificatePath
 */

/**
 * @param  \Spatie\Analytics\Cache $cache
 * @param  int $cacheLifetimeInMinutes
 * @param  int $realTimeCacheLifetime
 */

use Spatie\Analytics\Analytics;

$analytics = Analytics::create(
    'ga:xxxxxxxx',
    'xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com',
    'xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com',
    '/keys/analytics/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12',
    $myCache,
    60 * 24 * 2,
    5
);

/*
* $analyticsData now contains an array with 3 columns: "date", "visitors" and "pageViews"
*/
$analyticsData = $analytics->getVisitorsAndPageViews(7);

/*
* $analyticsData now contains an array with 2 columns: "url" and "pageViews"
*/
$analyticsData = $analytics->getMostVisitedPages(365, 20);

/**
 * Get the amount of visitors and pageviews
 *
 * @param  int $numberOfDays
 * @param  string $groupBy Possible values: date, yearMonth
 * @return array
 */
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 array
 */
public function getVisitorsAndPageViewsForPeriod($startDate, $endDate, $groupBy = 'date')

   /**
 * Get the top keywords
 *
 * @param  int $numberOfDays
 * @param  int $maxResults
 * @return array
 */
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 array
 */
public function getTopKeyWordsForPeriod($startDate, $endDate, $maxResults = 30)

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

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

/**
 * Get the top browsers
 *
 * @param  int $numberOfDays
 * @param  int $maxResults
 * @return array
 */
public function getTopBrowsers($numberOfDays = 365, $maxResults = 6)

/**
 * Get the top browsers for the given period
 *
 * @param  \DateTime $startDate
 * @param  \DateTime $endDate
 * @param  $maxResults
 * @return array
 */
public function getTopBrowsersForPeriod($startDate, $endDate, $maxResults) 

/**
 * Get the most visited pages
 *
 * @param  int $numberOfDays
 * @param  int $maxResults
 * @return array
 */
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 array
 */
public function getMostVisitedPagesForPeriod($startDate, $endDate, $maxResults = 20)

/**
 * Get the number of active users currently on the site
 * 
 * @param  array $others
 * @return array
 */
public function getActiveUsers($others = array())

/**
 * 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())