I'm trying to build a custom formula for a Google Sheets spreadsheet. Using Google Apps Script, I'm trying to access the Unique Event Count of an event, with a specific Event Label in a Google Analytics view. Here is my code below:
function USE_CLICK_COUNT(startDate, endDate){
var tableId = 'ga:78467590';
var reportStart = startDate;
var reportEnd = endDate;
var optArgs = {
'metrics': 'ga:uniqueEvents',
'dimensions':'ga:eventLabel',
'filters':'ga:eventLabel=~^UseClick'
};
var results = Analytics.Data.Ga.get(tableId, '2020-08-31', '2020-09-06', optArgs);
Logger.log(results);
}
Although this is not the final version of the function, I tested it to see if it works so far. However I get the following error:
GoogleJsonResponseException: API call to analytics.data.ga.get failed with error: Invalid value '{metrics=ga:uniqueEvents, dimensions=ga:eventLabel, filters=ga:eventLabel=~^UseClick}'. Values must match the following regular expression: 'ga:.+' (line 16, file "Code")
I tried changing the capitalization of Analytics query args, but nothing changed. What should I do?
Analytics.Data.Ga.get
are ids, start-date, end-date, metrics, optionalArgs
. So in your script, metrics
is not set. I think that this might be the reason of your issue. So please set metrics
. Ref Although I'm not sure what you want, for example, as a test, how about testing it by setting ga:pageviews
as metrics
.var results = Analytics.Data.Ga.get(tableId, '2020-08-31', '2020-09-06', optArgs);
To:
var results = Analytics.Data.Ga.get(tableId, '2020-08-31', '2020-09-06', "ga:pageviews", optArgs);
var results = Analytics.Data.Ga.get(tableId, '2020-08-31', '2020-09-06', "", optArgs);
occurs no error.