Learn R Programming

GAR (version 1.1)

gaRequest: Request data from Google Analytics API

Description

This function compiles a Google Analytics API request, retrieves the results and outputs a dataframe. Input formats based on documented Google Analytic's Core Reporting API requirements. The function is designed to loop through the same request to multiple GA account ids. The result will still be one data frame. Additionally, the function attemps to output the error message if there is an issue. By default, the function will use the GAR_ACCESS_TOKEN variable set by the tokenRefresh() function. However, a supplied token value will take priority over stored variabled.

Usage

gaRequest( id, dimensions = NA, metrics, start, end, token = NA, sort = NA, max = 1000, segment = NA, filters = NA, allResults=FALSE )

Arguments

id
Required. Profile ID including the ga: prefix. Can be a more than one. See example below.
dimensions
Not Required. Parameter can be a single dimension or a string of multiple dimensions separated by commas.
metrics
Required. Parameter can be a single metric or a string of multiple metrics separated by commas.
start
Required. Start date formated as a string (YYYY-MM-DD).
end
Required. End date formated as a string (YYYY-MM-DD).
token
Required.
sort
Not Required. Can be a single parameter or can be a string of multiple parameters separated by commas.
max
Not Required. Google Analytics default is 100 and their max is 10,000.
segment
Not Required.
filters
Not Required. Can be a single parameter or can be a string of multiple parameters separated by commas.
allResults
Logical. TRUE will cause the function to paginate through all results. Max also needs to be set to 10000. Warning, pagination could increase run time.

Value

profileId
Profile/view ID in request.
accountId
Account ID.
webPropertyId
Property UA number.
internalWebPropertyId
Internal web property ID.
profileName
Name of profile in the request.
tableId
Table ID from Google Analytics.
start.date
Start date of the request.
end.date
End date of the request.
dimension(s)
Each requested dimension will be a separate field in Data Frame.
metric(s)
Each requested metric will be a separate field in Data Frame.

Details

The function returns a dataframe.

References

https://developers.google.com/analytics/devguides/reporting/core/v3/

See Also

GAR gaRequest getCode getRefresh tokenRefresh

Examples

Run this code

gaRequest(
        id=c('ga:123456789','ga:987654321'), 
        dimensions='ga:date,ga:month', 
        metrics='ga:sessions,ga:users,ga:pageviews', 
        start='YYYY-MM-DD',
        end='YYYY-MM-DDD',
        sort='-ga:sessions,ga:users',
        max='1000',
        filters='ga:browser==VALUE,ga:city==VALUE',
        segment='abcdef',
        allResults=FALSE,
        token='abcdef'
        )

Run the code above in your browser using DataLab