Log in Register
Home Search Categories About
Bilel T
read article

How to Fetch Reports from Google Analytics API Using PHP

google analytics reports

Google Analytics provides a powerful API that allows developers to programmatically retrieve analytics data. In this tutorial, we'll go through the steps to make an API request to Google Analytics using PHP to fetch the number of active users in the last 7 days.

Step 1: Set Up a Project in Google Cloud Console

  1. Go to the Google Cloud Console.
  2. Create a new project and select it from the project drop-down.

Step 2: Enable the Google Analytics API

  1. In the Cloud Console, navigate to "API & Services" > "Library."
  2. Search for "Google Analytics API" and enable it for your project.

Step 3: Create API Credentials

  1. In the Cloud Console, go to "API & Services" > "Credentials."
  2. Click on "Create Credentials" and choose "Service account key."
  3. Fill out the form, select your service account, choose the role "Project" > "Editor," and set Key type to JSON.
  4. Click "Create" to download the JSON file containing your credentials.

Step 4: Share Google Analytics Account Access

In your Google Analytics account, go to "Admin" > "Account User Management" and add the service account email address from the JSON file as a user with "Read & Analyze" permissions.

Step 5: Install Required Libraries

Install the necessary PHP libraries using Composer:

composer require google/apiclient:^2.0

Step 6: Create PHP Script

Create a PHP script (e.g., fetch-analytics.php) to fetch active users in the last 7 days:

<?php

require 'vendor/autoload.php';

use Google\Client as Google_Client;

// Load the credentials from the JSON file
$client = new Google_Client();
$client->setAuthConfig('path/to/your/credentials.json');
$client->setApplicationName('Your Application Name');
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);

// Set your Google Analytics view ID
$analyticsViewId = 'ga:YOUR_VIEW_ID';

// Create Analytics service
$analytics = new Google_Service_AnalyticsReporting($client);

// Date range
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate('7daysAgo');
$dateRange->setEndDate('today');

// Metric for active users
$metric = new Google_Service_AnalyticsReporting_Metric();
$metric->setExpression('ga:activeUsers');

// Report request
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($analyticsViewId);
$request->setDateRanges([$dateRange]);
$request->setMetrics([$metric]);

// Get the response
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests([$request]);
$response = $analytics->reports->batchGet($body);

// Print the active users
if (!empty($response->getReports())) {
    $report = $response->getReports()[0];
    $activeUsers = $report->getData()->getRows()[0]->getMetrics()[0]->getValues()[0];
    echo 'Active Users in the Last 7 Days: ' . $activeUsers;
} else {
    echo 'No data found.';
}

Step 7: Run the Script

Run the PHP script:

php fetch-analytics.php

This script uses the Google Analytics API to fetch the number of active users in the last 7 days and prints the result.

Remember to replace 'path/to/your/credentials.json' with the actual path to your JSON credentials file and 'YOUR_VIEW_ID' with your Google Analytics view ID.

That's it! You've successfully made an API request to Google Analytics using PHP. Feel free to customize the script to suit your specific needs or integrate it into your web application.