Skip to content

Instantly share code, notes, and snippets.

@field2
Created January 24, 2024 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save field2/269305f6a9988c6144e804afdb6a78df to your computer and use it in GitHub Desktop.
Save field2/269305f6a9988c6144e804afdb6a78df to your computer and use it in GitHub Desktop.
monthly users
To retrieve the number of visits over the last month using Google Analytics, you can modify the PHP code to make use of the Google Analytics Reporting API. Here's an example of how you can achieve this:
### 1. Set up Google Analytics:
1. Make sure your website is configured with Google Analytics, and you have the tracking code.
2. Enable the Google Analytics Reporting API for your project and obtain the API credentials (client ID, client secret, etc.).
### 2. Install the Google API PHP Client Library:
You can use Composer to install the library. Run the following command in your project directory:
```bash
composer require google/apiclient:^2.0
```
### 3. Use PHP to retrieve the number of visits over the last month:
Update your `functions.php` file with the following code:
```php
require __DIR__ . '/vendor/autoload.php'; // Adjust the path if necessary
use Google\Client;
use Google\Service\AnalyticsReporting;
function get_visits_last_month() {
$client = new Client();
$client->setApplicationName('Your Application Name');
$client->setAuthConfig('path/to/your/credentials.json'); // Replace with the path to your API credentials
$client->setScopes([AnalyticsReporting::ANALYTICS_READONLY]);
$analytics = new AnalyticsReporting($client);
// Replace 'ga:XXXXXXXX' with your Google Analytics View ID
$viewId = 'ga:XXXXXXXX';
// Set the date range for the last month
$startDate = date('Y-m-d', strtotime('first day of last month'));
$endDate = date('Y-m-d', strtotime('last day of last month'));
$dateRange = new Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate($startDate);
$dateRange->setEndDate($endDate);
$sessions = new Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression('ga:sessions');
$sessions->setAlias('sessions');
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($viewId);
$request->setDateRanges([$dateRange]);
$request->setMetrics([$sessions]);
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests([$request]);
$reports = $analytics->reports->batchGet($body);
if (!empty($reports->getReports())) {
$data = $reports->getReports()[0]->getData();
$totals = $data->getTotals();
$sessionsValue = $totals[0]->getValues()[0];
return intval($sessionsValue);
}
return 0;
}
$visits_last_month = get_visits_last_month();
echo "Number of visits last month: $visits_last_month";
```
Make sure to replace `'Your Application Name'`, `'path/to/your/credentials.json'`, and `'ga:XXXXXXXX'` with your actual values.
This code sets up a connection to the Google Analytics Reporting API and retrieves the number of sessions (visits) for the last month. Adjust it based on your specific requirements and analytics configuration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment