To be able to connect to Google API through a server (and without having to imput a password every time, we need to establish an OAuth Connection.
* 1. create project at https://console.developers.google.com/project | |
* 2. enable 'Analytics API' under 'APIs & auth' / APIs | |
* 3. create 'NEW CLIENT ID' (OAuth client) under 'APIs & auth' / Credentials | |
* i. select 'Service account' | |
* ii. save generated key file to 'key.p12' and upload it on your server web directory | |
* iii. remember CLIENT ID | |
* 4. Access your Google Analytics Account, then, under the tab "Admin", go to User Management, and add as a new user with basic read & analyse access the long email address generated when you created your API credenticals: xxxxxxxxx-yyyyyyyyyyyyyyyyyy@developer.gserviceaccount.com | |
* 5. get View ID, (see the last article that show how to find it) | |
* 5. download or clone the google php library https://github.com/google/google-api-php-client | |
* 6. Code the program in php thanks to these resources: | |
* doc here: https://developers.google.com/analytics/devguides/reporting/realtime/v3/reference/data/realtime/get | |
* real time metrics doc: https://developers.google.com/analytics/devguides/reporting/realtime/dimsmets/ | |
Check the following code :
require_once 'google-api/src/Google/autoload.php'; $PATH_TO_KEY_FILE = '/oauth/client_secrets.p12';$CLIENT_ID = 'lklkklklklklklk3rqrv5jkh0.apps.googleusercontent.com';
Note: link to realtime metrics:
https://developers.google.com/analytics/devguides/reporting/realtime/dimsmets/goalconversions?hl=pt-BR |
Commentaires
Enregistrer un commentaire
Tell me what you think