Accessing Google Analytics API with Service Account and Coldfusion
For the Google Analytics Real Time API with Servie Account and Coldfusion
-
Create your project in the Google console: https://code.google.com/apis/console
-
Go to API's & Auth and turn off all but the Analytics API.
-
Go to Credentials (under API's) and click Create a New Client ID.
-
Select Service Account from the pop up.
-
Save the .p12 file it will prompt you to download to a non-browsable place on your webserver. Feel free to rename it but keep the .p12 extension.
-
The service account email address will be under the Service Account setting box as Email Address and be in the form of [email protected] (the really long email). Add this email to the Google analytics profile(s) as a user with Read & Analyze access.
- Add the Google Analytics API v3 client library .jar files to the CF server in the WEB-INF/lib folder. The files can be currently found at: https://developers.google.com/api-client-library/java/apis/analytics/v3 The readme.html will list the dependencies. As of April 2015 they are the following when using Jackson 2:
- google-api-services-analytics-v3-rev115-1.20.0.jar
- google-api-client-1.20.0.jar
- google-oauth-client-1.20.0.jar
- google-http-client-1.20.0.jar
- jsr305-1.3.9.jar
- google-http-client-jackson2-1.20.0.jar
- jackson-core-2.1.3.jar
- Restart the CF server (if you installed the .jar files directly on the server).
-
Save the cfanalytics folder to your web root or where you keep your com objects.
-
init() the cfanalytics object. This can be done as an application variable. The pathToKeyFile = expandPath("/your-path-to-key-file/your-key-name.p12"). Make sure this is non-browsable!
-
Call the getProfile() method to get the profile ID for the tableId parameter in the getData() and getRealTimeData() method.
-
Call the getData() method to access data or the getRealTimeData() method for real time data.
-
Use the following links for references on getting profile information and data from GA:
- https://developers.google.com/accounts/docs/OAuth2ServiceAccount
- https://code.google.com/p/google-api-java-client/wiki/OAuth2
For the Core Reporting API:
- https://developers.google.com/api-client-library/java/apis/analytics/v3
- https://developers.google.com/analytics/devguides/reporting/core/dimsmets
- http://ga-dev-tools.appspot.com/explorer/
For the Real Time API