The latest stable version of googleAnalyticsR is available on CRAN.
You also need the version > 0.3.0
of googleAuthR
install.packages("googleAuthR") install.packages("googleAnalyticsR")
You may prefer to use the latest development version is on github which you can install via the below.
devtools::install_github("MarkEdmondson1234/googleAnalyticsR")
Check out the NEWS to see whats currently available in each version.
## setup library(googleAnalyticsR) ## This should send you to your browser to authenticate your email. ## Authenticate with an email that has access to the Google Analytics View you want to use. ga_auth() ## get your accounts account_list <- google_analytics_account_list() ## pick a profile with data to query ga_id <- account_list[23,'viewId']
Should you need to authenticate under a new user, use ga_auth(new_user=TRUE)
From version 0.4
(currently on Github) you can choose to auto-authenticate by moving your .httr-oauth
or by creating a Google OAuth service account JSON file.
Once you have set the environment variable GA_AUTH_FILE
to a valid file location, the function will look there for authentication details upon loading the library meaning you will not need to call ga_auth()
yourself as you would normally.
You can set environment variables using a .Renviron
file or via the function Sys.setenv()
An example .Renviron
file is below - this is placed in your root directory (~/
or C:\\
)
GA_AUTH_FILE = "/Users/bob/auth/googleAnalyticsR.httr-oauth"
GA_AUTH_FILE
can be either the file path to a .httr-oauth
token generated by gar_auth()
or your own Google Project's service account JSON ending with file extension .json
If you use the service account JSON, you will also need to add the service account email via your Google Analytics admin as a user to see data e.g. xxxx@yyyyyy.iam.gserviceaccount.com
To see what dimensions and metrics are available via the API, you can use this command:
## get a list of what metrics and dimensions you can use ga_auth() meta <- google_analytics_meta()
If offline, from v0.2.0
this data is also available offline by typing meta
, although it won't be quite as up to date.
The meta data does not include multi-channel as they are not available to download, for those see the online version.
When using the library, you can call metrics and dimensions with or without the ga:
prefix (mcf:
for multi-channel metrics/dimensions.)
If you are using more than one API for authentication (such as Search Console), then authenticate using googleAuthR::gar_auth()
instead, to ensure you are authenticate with the correct scopes.
With the amount of API calls possible with this library via batching and walking, its more likely the default shared Google API project will hit the 50,000 calls per day limit.
To mitigate that, use your own Google Developer Console Project key, so it is not shared across all users of this library.
Set the options before any call to ga_auth()
or other data fetching calls.
options(googleAuthR.client_id = "uxxxxxxx2fd4kesu6.apps.googleusercontent.com") options(googleAuthR.client_secret = "3JhLa_GxxxxxCQYLe31c64") options(googleAuthR.scopes.selected = "https://www.googleapis.com/auth/analytics")
In the next example, the scopes are also set so a user can authenticate with searchConsoleR
at the same time, which is possible as they both use googleAuthR as a backend.
options(googleAuthR.client_id = "tuxxxxxxx2fd4kesu6.apps.googleusercontent.com") options(googleAuthR.client_secret = "3JhLa_GxxxxxCQYLe31c64") options(googleAuthR.scopes.selected = c("https://www.googleapis.com/auth/analytics", "https://www.googleapis.com/auth/webmasters"))
If you want to use with Shiny, then set the webapp clientId/secrets.
options(googleAnalyticsR.webapp.client_id = "xxxxx9pcab1u005cv.apps.googleusercontent.com") options(googleAnalyticsR.webapp.client_secret = "zcofxxxxxxElemXN5sf")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.