3
votes

So my task is to create a web app in PHP which will do this:

  • output total number of visitors on the site
  • output the name of the country from where the most visitors came from

So far I have done this:

  • Created a project in API console
  • Enabled API
  • Created credentials
  • Successfully setup sample

When I run the sample, it outputs the name of the authorized user's first Google Analytics view and the number of sessions for the last seven days. I followed these instructions: https://developers.google.com/analytics/devguides/config/mgmt/v3/quickstart/web-php

I analyzed the code and figured out that in order to call Core Reporting API, it's required to get Profile ID of the user.

function getResults($analytics, $profileId) {
   return $analytics->data_ga->get(
      'ga:' . $profileId,
      '7daysAgo',
      'today',
      'ga:sessions');
}

To get the Profile ID, the function getFirstProfileId is used (you can see it on the link I provided).

So my question is - how do I make it work so it outputs total number of visitors on my page. Do I have to skip part where I get profileID, and if that's true, how do I call Core Reporting API?

2
This is the id of your analytics view, not an id for a user (i.e.. this is were you get your data from, so you cannot skip this). Also there is no such thing as an absolute number of users, it is always users with a given timeframe. And you probably should use the v4 API to future proof your application. - Eike Pierstorff

2 Answers

1
votes

I think you are misunderstand standing what a profile id is. When you authenticate to Google you are getting access to the current authenticated users Google analytics accounts.

Inorder to request data from the Google Analytics api it needs to know what view you wish to request data for view is the same as profile.

So my question is - how do I make it work so it outputs total number of visitors on my page. Do I have to skip part where I get profileID, and if that's true, how do I call Core Reporting API?

You cant you need to tell Google analytics which view to request data from. It links back to your other comment

output total number of visitors on the site

Profile id is the id of the site you want to request data from.

0
votes

You can use Google Analytics API client in PHP. Google analytic api client library

You can use the Query Explorer to create the queries to check.

Code Example:

$analytics = new analytics('username', 'password');
$analytics->setProfileByName('user.name');
//set the date range for which you want stats for 
$analytics->setMonth(date('n'), date('Y'));
// it could also be $analytics->setDateRange('YYYY-MM-DD', 'YYYY-MM-DD'))
print_r($analytics->getVisitors());
print_r($analytics->getPageviews());

The above example used the Google Analytics API client in PHP. It was the first library released in PHP. Six years later, this software is outdated. Google changed the API. As an alternative you can use GAPI library. Above is the example how it would work, you can include gapi class to make it functional.

GAPI Analytic Library

Another way is that you can use the Google Analytics Reporting API v4 for PHP. You can obtain this using composer:

composer require google/apiclient:^2.0

Guide to usage of this library is at github