Google Analytics is an integral part of all websites right now. The main benefit of integrating it with our website is knowing our audiences better. We can know which location they are from, which device they are using, which pages they are visiting.etc... In normal process, we will get an analytics dashboard from google itself to browse through the analytics datas and graphs. But sometimes we will be having a requirement, where we have to show those data into our websites dashboards. In this scenario we have to integrate the API with our website.
STEPS FOR CREATING API ACCOUNT ( From Google Developer Docs)
To get started using Analytics Reporting API v4, you need to first use the setup tool, which guides you through creating a project in the Google API Console, enabling the API, and creating credentials.
STEPS FOR CREATING API ACCOUNT ( From Google Developer Docs)
To get started using Analytics Reporting API v4, you need to first use the setup tool, which guides you through creating a project in the Google API Console, enabling the API, and creating credentials.
- Open the Service accounts page. If prompted, select a project.
- Click Create Service Account, enter a name and description for the service account. You can use the default service account ID, or choose a different, unique one. When done click Create.
- The Service account permissions (optional) section that follows is not required. Click Continue.
- On the Grant users access to this service account screen, scroll down to the Create key section. Click Create key.
- In the side panel that appears, select the format for your key: JSON is recommended.
- Click Create. Your new public/private key pair is generated and downloaded to your machine; it serves as the only copy of this key. For information on how to store it securely, see Managing service account keys.
- Click Close on the Private key saved to your computer dialog, then click Done to return to the table of your service accounts.
Add service account to the Google Analytics account
The newly created service account will have an email address that looks similar to:
quickstart@PROJECT-ID.iam.gserviceaccount.com
Use this email address to add a user to the Google analytics view you want to access via the API. For this tutorial only Read & Analyze permissions are needed.
2. Install the client library
You can obtain the Google APIs Client Library for PHP using Composer:
composer require google/apiclient:^2.0
Refer:- https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php
3. Now all our libraries has been downloaded. But for fetching data, we should know how to call classes and functions. For attributes and responses you can refer to this library:-
https://ga-dev-tools.appspot.com/dimensions-metrics-explorer/
https://developers.google.com/analytics/devguides/reporting/core/v4/samples
https://developers.google.com/analytics/devguides/reporting/core/v4/samples
4. In this post, I want to make the developers job very easily. I have developed a package where you can just call functions and you will get the data you need. Please download my package from Github.
My Further Explanation will be based on this.
In my package there is a File called AnalyticsFunc.php, I have included all the functions in this file based on my research of Google analytics API.
One thing You have to do is replace two values in those files:-
1) Find $KEY_FILE_LOCATION = __DIR__ . '/credentials.json'; Line in the initializeAnalytics() function and change the json file name to the file name you downloaded from google developer console. You have to include that file in the root folder.
2)In Every Function, there is a line called $VIEW_ID = "YourID";. here you have
to provide your view id of that particular project from google analytics
dashboard.
You just need to call the functions like below:-
<?php
//Including Files and Libraries
require_once __DIR__ . '/vendor/autoload.php';
include("AnalyticsFunc.php");
//Initiliazing Analytics Object
$analytics = initializeAnalytics();
//Calling User Reports Sessions and page views
$userreport = getUserReport($analytics);
$records=fetchResults($userreport);
?>
Above is a sample code for calling user reports sessions. You will get result in an array format, which you can use for generating graphs, charts and tables.
For calling all functions until initializing Analytics objects will be common. The functions part you can call any functions as per your Requirement. I will list all the functions down here.
$sessionreport = getsessionReport($analytics); // Get all Session Data
$traffic = gettrafficsources($analytics); //Get all Traffic Sources
$platform = getplatformreport($analytics); //Get all Platforms Data
$geonetwork = getgeonetworkreport($analytics); // Get all Location Reports
$social = getsocialreport($analytics); //Report on Social activities
$pagetracking = getpagetrackingreport($analytics); // Report on Page Tracking
$internalsearch = getinternalsearchReport1($analytics); // Get Internal Search reports
$speed = getspeedReport($analytics);// Get Page Speed Report
$socialinter = getsocialinteractionReport($analytics); // Get Social Interaction reports