Yesterday, I was wanting to pull specific Google Analytics data with their API. I started following their Hello Analytics API: PHP QuickStart guide and quickly found out that their quick start does not use the latest Client PHP Library. After struggling a while to get the new stuff working, I finally got it all set up correctly. If you have never used their Analytics API you ought to.
After getting everything worked out I decided to take their current docs and update them with the latest API info. Most of what’s below is their documentation with some critical changes made by me. Enjoy!
Step 1: Enable the Analytics API
To get started using Google Analytics API, you need to first create or select a project in the Google Developers Console and enable the API. Using this link guides you through the process and activates the Google Analytics API automatically.
Alternatively, you can activate the Google Analytics API yourself in the Developers Console by doing the following:
In either case, you end up on the Credentials page and can create your project’s credentials from here.
Create a client ID
- Open the Service accounts section of the Developers Console’s Permissions page.
- Click Create service account.
- In the Create service account window, type a name for the service account and select Furnish a new private key. If you want to grant Google Apps domain-wide authority to the service account, also select Enable Google Apps Domain-wide Delegation. Then, click Create.
When prompted for the Key type select JSON, select your Service Account and then click Create. Once you create the service account a JSON file will be downloaded. Your new public/private key pair is generated and downloaded to your machine; it serves as the only copy of this key. You are responsible for storing it securely.
Add service account to Google Analytics account
The newly created service account will have an email address,
<projectId>-<uniqueId>@developer.gserviceaccount.com. Use this email address to add a user to the Google analytics account you want to access via the API. For this tutorial only Read & Analyze permissions are needed.
Step 2: Install the Google Client Library
Once Composer is installed, execute the following command in your project root to install the PHP API library:
composer require google/apiclient:^2.0.0@RC
Finally, be sure to include the autoloader:
Step 3: Setup the sample
You’ll need to create a single file named
HelloAnalytics.php, which will contain the sample code below. The only difference in this code and the original is seen in the
Move the previously downloaded
.json within the same directory as the sample code and make sure you update the filename for this line
Step 4: Run the sample
After you have enabled the Analytics API, installed the Google APIs client library for PHP, and set up the sample source code the sample is ready to run.
Run the sample using:
When you finish these steps, the sample outputs the name of the authorized user’s first Google Analytics view (profile) and the number of sessions for the last seven days.
With the authorized Analytics service object you can now run any of code samples found in the Management API reference docs. For example you could try changing the code to use the accountSummaries.list method.
If you have any questions feel free to comment on the Gist