Hi Chris,

When I run step 4, I set a date range of three days. The loop only creates a data set WITH data for the last day, for the other two days, the data sets are created but are empty. The message I get is:

MPRINT(GETGADATA): libname garesp json fileref=ga_resp;
NOTE: JSON data is only read once. To read the JSON again, reassign the JSON LIBNAME.
ERROR: Invalid JSON in input near line 123 column 15: Some code points did not transcode.
ERROR: Error in the LIBNAME statement.
MPRINT(GETGADATA): data ga.ga_daily20211121 (drop=element:);
MPRINT(GETGADATA): set garesp.rows;
ERROR: Libref GARESP is not assigned.

This is the code I am running:

%let startdate=%sysevalf('20Nov2021'd);
%let enddate = %sysfunc(today());

/* Metrics and dimensions are defined in the Google Analytics doc */
/* Experiment in the developer console for the right mix */
/* Your scenario might be different and would require a different */
/* type of query */
/* The GA API will "number" the return elements as */
/* element1, element2, element3 and so on */
/* In my example, path and title will be 1 and 2 */
%let dimensions= %sysfunc(urlencode(%str(ga:pagePath,ga:pageTitle)));
/* then pageviews, uniquepageviews, timeonpage will be 3, 4, 5, etc. */
%let metrics= %sysfunc(urlencode(%str(ga:pageviews,ga:uniquePageviews,ga:timeOnPage,ga:entrances,ga:exits)));
/* this ID is the "View ID" for the GA data you want to access */
%let id= %sysfunc(urlencode(%str(ga:xxxxxxxx)));

libname ga "\\xxx-xxxxx\xxxx\xxx\Analytics\Marketing\Marketing Performance Dashboard\GASASDATA";

%macro getGAdata;
%do workdate = &enddate %to &startdate %by -1;
%let urldate=%sysfunc(putn(&workdate.,yymmdd10.));
filename ga_resp temp;
proc http
url="https://www.googleapis.com/analytics/v3/data/ga?ids=&id.%str(&)start-date=&urldate.%str(&)end-date=&urldate.%str(&)metrics=&metrics.%str(&)dimensions=&dimensions.%str(&)max-results=20000"
method="GET" out=ga_resp;
headers
"Authorization"="Bearer &access_token."
"client-id:"="&client_id.";
run;

libname garesp json fileref=ga_resp;

data ga.ga_daily%sysfunc(compress(&urldate.,'-')) (drop=element:);
set garesp.rows;
drop ordinal_root ordinal_rows;
length date 8 url $ 300 title $ 250
views 8 unique_views 8 time_on_page 8 entrances 8 exits 8
;
format date yymmdd10.;
date=&workdate.;
/* Corerce the elements into data variables */
/* Basic on expected sequence */
url = element1;
title = element2;
views = input(element3, 5.);
unique_views = input(element4, 6.);
time_on_page=input(element5, 7.2);
entrances = input(element6, 6.);
exits = input(element7, 6.);
run;
%end;
%mend;

%getGAdata;