Google Apps Script – Google Analytics API service – part 050.
This is a simple example with Google Analytics API service settings. You need to add the Analytics service version 3.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function getAnalytics() { // get data with Google Analytics API service settings version 3 var viewId = "ga:YOUR_views_number"; var metrics = [{expression: "ga:sessions"}]; var dimensions = [{name: "ga:date"}]; var startDate = "2022-01-01"; var endDate = "2022-01-31"; var results = Analytics.Data.Ga.get( viewId, startDate, endDate, 'ga:visitors,ga:visits,ga:pageviews', {dimensions: "ga:date"}); Logger.log(results); } |
The result is shown in the Google Apps Script editor with Logger.log :
1 2 3 4 5 6 7 8 | {kind=analytics#gaData, id=https://www.googleapis.com/analytics/v3/data/ga?ids=ga: YOUR_views_number&dimensions=ga:date&metrics=ga:visitors,ga:visits,ga: pageviews&start-date=2022-01-01&end-date=2022-01-31, columnHeaders=[{name=ga:date, dataType=STRING, columnType=DIMENSION}, {columnType=METRIC, name=ga:visitors, dataType=INTEGER}, {name=ga:visits, dataType=INTEGER, columnType=METRIC}, {dataType=INTEGER, columnType=METRIC, name=ga:pageviews}], totalResults=31.0, rows= [[20220101, 33, 33, 41], ... |