getSiteIds method:
@AuraEnabled(cacheable=true)
public static List<String> getSiteIds(String webSiteId, String token)
{
try{
// CUSTOMIZE THE ENDPOINT FOR THE GIVEN METHOD
String customEndpoint = webSiteId + '/api/apps/v2';
//String token = AccessTokenGetSet.getSetToken;
// MAKE THE API CALL
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setHeader('Content-Type','application/vnd.api+json');
req.setHeader('Authorization','Bearer ' + token);
req.setEndpoint(customEndpoint);
Http h = new Http();
HTTPResponse res = h.send(req);
if (res.getStatusCode() == 200){
Map<String,Object> results = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
List<Object> resultsData = (List<Object>)results.get('data');
List<String> finalList = new List<String>();
List<Object> data = (List<Object>) results.get('data');
for (Object dataIte : data) {
Map<String, Object> dataMapIte = (Map<String, Object>) dataIte;
String id = (String) dataMapIte.get('id');
Map<String, Object> attributes = (Map<String, Object>) dataMapIte.get('attributes');
String name = (String) attributes.get('name');
siteMap.put(name, id);
finalList.add(name);
}
return finalList;
}
else {
System.debug('ELSE');
return null;
}
}
catch (exception ex){
System.debug(ex.getMessage());
return null;
}
}
fetchSession method:
// CUSTOM ENDPOINT
String customEndpoint = webSite + '/api/analytics/v1/sessions/';
// CUSTOM BODY
Data data = new Data();
data.website_id = siteMap.get(selectedSiteName);
System.debug(data.website_id);
// BODY column
List<Data.Column> columnWrapper = new List<Data.Column>();
Data.Column c = new Data.Column();
for (String column_id : apiNamesList) {
c.column_id = column_id;
columnWrapper.add(c);
}
data.columns = columnWrapper;
data.date_from = date_From;
data.date_to = date_to;
data.offset = 0; // hardcoded for now, if wished - can be dynamically sent to the request - TESTED
data.format = 'json'; // hardcoded for now, if wished - can be dynamically sent to the request - TESTED
// MAKE THE API CALL
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setHeader('Content-Type','json');
req.setHeader('Authorization','Bearer ' + token);
req.setHeader('Accept-Encoding', 'gzip'); // advice - enable encoding/compression when pulling large amounts of data https://prnt.sc/LzsAv6K4_NTk
req.setEndpoint(customEndpoint);
req.setBody(JSON.serialize(data));
Http h = new Http();
HTTPResponse res = h.send(req);
if (res.getStatusCode() == 200){
Map<String,Object> results = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
List<Object> responseData = (List<Object>) results.get('data');
// Return data List<Object>
return responseData;
}
else {
System.debug(res.getBody());
System.debug(res.getStatusCode());
System.debug('ELSE');
return null;
}
}
catch (exception ex){
System.debug(ex.getMessage());
return null;
}
}