Skip to main content
deleted 4454 characters in body
Source Link
Stojchesky
  • 171
  • 1
  • 11

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;
        }
    }

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;
        }
    }
deleted 1000 characters in body
Source Link
Stojchesky
  • 171
  • 1
  • 11
 @AuraEnabled(cacheable=true)
    public static List<Object> fetchSessions (String webSite, String selectedSiteName, List<String> stringColumns, String date_from, String date_to, String token){
        try{
            // GET API NAMES OF THE SELECTED DIMEONSIONS
            Schema.DescribeFieldResult objFieldDescribe = Piwik__c.Choose_dimensions__c.getDescribe();
            List<Schema.PicklistEntry> lstPickListValues = objFieldDescribe.getPickListValues();
            String elementValue;
            String elementLabel;
            List<String> apiNamesList = new List<String>();
            for (Schema.PicklistEntry objPickList : lstPickListValues) {
                elementLabel = objPickList.getLabel();
                if (stringColumns.contains(elementLabel)){
                    elementValue = objPickList.getValue();
                    apiNamesList.add(elementValue);
                }
            }
            // 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;
        }
    }
 @AuraEnabled(cacheable=true)
    public static List<Object> fetchSessions (String webSite, String selectedSiteName, List<String> stringColumns, String date_from, String date_to, String token){
        try{
            // GET API NAMES OF THE SELECTED DIMEONSIONS
            Schema.DescribeFieldResult objFieldDescribe = Piwik__c.Choose_dimensions__c.getDescribe();
            List<Schema.PicklistEntry> lstPickListValues = objFieldDescribe.getPickListValues();
            String elementValue;
            String elementLabel;
            List<String> apiNamesList = new List<String>();
            for (Schema.PicklistEntry objPickList : lstPickListValues) {
                elementLabel = objPickList.getLabel();
                if (stringColumns.contains(elementLabel)){
                    elementValue = objPickList.getValue();
                    apiNamesList.add(elementValue);
                }
            }
            // 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;
        }
    }
            // 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;
        }
    }
deleted 12 characters in body
Source Link
Stojchesky
  • 171
  • 1
  • 11
 @AuraEnabled(cacheable=true)
    public static List<Object> fetchSessions (String webSite, String selectedSiteName, List<String> stringColumns, String date_from, String date_to, String token){
        try{
            // GET API NAMES OF THE SELECTED DIMEONSIONS
            Schema.DescribeFieldResult objFieldDescribe = Piwik__c.Choose_dimensions__c.getDescribe();
            List<Schema.PicklistEntry> lstPickListValues = objFieldDescribe.getPickListValues();
            String elementValue;
            String elementLabel;
            List<String> apiNamesList = new List<String>();
            for (Schema.PicklistEntry objPickList : lstPickListValues) {
                elementLabel = objPickList.getLabel();
                if (stringColumns.contains(elementLabel)){
                    elementValue = objPickList.getValue();
                    apiNamesList.add(elementValue);
                }
            }
            // CUSTOM ENDPOINT
            String customEndpoint = webSite + '/api/analytics/v1/sessions/';
            // CUSTOM BODY 
            **DataData 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'); // piwik's 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;
        }
    }
 @AuraEnabled(cacheable=true)
    public static List<Object> fetchSessions (String webSite, String selectedSiteName, List<String> stringColumns, String date_from, String date_to, String token){
        try{
            // GET API NAMES OF THE SELECTED DIMEONSIONS
            Schema.DescribeFieldResult objFieldDescribe = Piwik__c.Choose_dimensions__c.getDescribe();
            List<Schema.PicklistEntry> lstPickListValues = objFieldDescribe.getPickListValues();
            String elementValue;
            String elementLabel;
            List<String> apiNamesList = new List<String>();
            for (Schema.PicklistEntry objPickList : lstPickListValues) {
                elementLabel = objPickList.getLabel();
                if (stringColumns.contains(elementLabel)){
                    elementValue = objPickList.getValue();
                    apiNamesList.add(elementValue);
                }
            }
            // 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'); // piwik's 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;
        }
    }
 @AuraEnabled(cacheable=true)
    public static List<Object> fetchSessions (String webSite, String selectedSiteName, List<String> stringColumns, String date_from, String date_to, String token){
        try{
            // GET API NAMES OF THE SELECTED DIMEONSIONS
            Schema.DescribeFieldResult objFieldDescribe = Piwik__c.Choose_dimensions__c.getDescribe();
            List<Schema.PicklistEntry> lstPickListValues = objFieldDescribe.getPickListValues();
            String elementValue;
            String elementLabel;
            List<String> apiNamesList = new List<String>();
            for (Schema.PicklistEntry objPickList : lstPickListValues) {
                elementLabel = objPickList.getLabel();
                if (stringColumns.contains(elementLabel)){
                    elementValue = objPickList.getValue();
                    apiNamesList.add(elementValue);
                }
            }
            // 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;
        }
    }
added 5425 characters in body
Source Link
Stojchesky
  • 171
  • 1
  • 11
Loading
deleted 132 characters in body
Source Link
sfdcfox
  • 501.6k
  • 21
  • 473
  • 828
Loading
Source Link
Stojchesky
  • 171
  • 1
  • 11
Loading