How To Connect Multiple Google Analytics Accounts To R Studio [googleanalyticsR package]
A few days back, I posted a question on Twitter asking about what view level permission is required to pull data in R Studio.
The reason was because every time I tried pulling data from a particular view, I kept getting this error: Error : API returned: User does not have sufficient permissions for this profile
which was strange because I was able to get data from other views.
Tweet got picked by the creator of googleanalyticsR package, Mark Edmonson, who answered more than a few questions and was very helpful.
I'll be explaining the exchange we had on Twitter:
First step, use:
ga_accounts_list()
This will return all the accounts and that view Ids that you have access to. As it turns out, I had used an authentication token from my personal gmail and not my work gmail - which led to the error.
So, here are two options (that Mark provided) for connecting multiple accounts:
Remove previous token, request new connection
ga_auth(new_user=TRUE)
This will re-open the API connection page in Google and provide a token...paste it in console and you're good to go. Simple but doesn't allow you to switch between accounts.
Save authentication tokens as files so as to switch between accounts:
#Run first token, authenticate with work email account and save token in "work token" ga_auth(token = "work token") #Run second token, authenticate with personal email account and save token in "personal token" ga_auth(token = "personal token")
You now have two tokens created. Whenever you want to switch, run this command, go back to your query and execute it.
And that's it.
Here's the full Tweet exchange with Mark Edmonson on this topic [as I had a lot of questions]