Reputation: 188
I am writing a script in which it queries Google Analytics and with the information it pulls back it creates a report.
I have went through the PHP Quickstart guide on Google Analytics website and got the developer service account set up and working.
However the problem I have run in to is that I can only access the accounts that have this email on and many other accounts I cannot add this to as I don't have permission. We cannot get the developer service account on to these accounts but we do have a company email with read permissions on their accounts.
Is there any way of querying Google Analytics through the company email instead of the developer service account? Or is there some other way of querying Google Analytics to get the information I need from their accounts?
Upvotes: 0
Views: 295
Reputation: 116878
Service accounts work when you have access to the account in question, or if the owner of the account is willing to grant your service account access to the account in question by adding it as a user.
If you cant do that then you use Oauth2 and this will require that the owner or a user of the account. Basically anyone with access goes in and authenticates your application. Hello Analytics API: PHP quickstart for web applications is the official documentation for using Oauth2. Since your company email has access to their accounts you should just be able to authenticate the application using that email and then access it.
If you are trying to automate this. You are going to need to request offline access in order to get a refresh token then save the refresh token you will then be able to access that accounts at anytime to create your reports. without needing to reauthenticate the application.
Upvotes: 3
Reputation: 476
i think the best way to get this information is that you have all the sub accounts under 1 Master Account and than get the Token exchange being done using either OAuth or you can have the Service Account for the Master Account that will be able to retrive all the information and post that you only have to Query the listManagementAccounts() method over the management_accounts class
let Say you $GAClient is your Google Analytics client in that case the perfect call can be like
$accounts = $GAClient->management_accounts->listManagementAccounts();
In case if you don't have access or you are creating an independent service then you may use the oAuth login to save the token and then you may query it when ever its needed.
Here is the HELP url for the same.
https://developers.google.com/api-client-library/php/auth/web-app
Upvotes: 0