Log Events to LogApp

This commit is contained in:
FrankRua 2022-04-29 14:07:11 -04:00
parent a638f63cdd
commit 121a7dbcb2
3 changed files with 41 additions and 57 deletions

View file

@ -1,49 +1,34 @@
import axios from "axios"; import axios from "axios";
import analyticsMixIn from "@/mixins/analytics-mixin.js";
import { applicationConfig } from "@/constants/application-config.js"; import { applicationConfig } from "@/constants/application-config.js";
import { GaCategories, GaActions, GaLabels } from "@/constants/analytics"; import { GaCategories, GaActions, GaLabels } from "@/constants/analytics";
import analyticsMixIn from "@/mixins/analytics-mixin.js";
import httpStatusCodes from "http-status-codes";
export default { export default {
callHttpClient({ method, endpoint, payload }) { callHttpClient({ method, endpoint, payload, logApiCall = true }) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const apiGatewayUrl = applicationConfig.CONSUMER_APIGATEWAY_URL; const apiGatewayUrl = applicationConfig.CONSUMER_APIGATEWAY_URL;
const payloadAndAnalyticsData = Object.assign({}, payload, { const payloadAndAnalyticsData = Object.assign({}, payload, { AppName: "FixMyGlass" });
AppName: "FixMyGlass",
});
axios({ axios({ method: method, url: apiGatewayUrl + endpoint, data: payloadAndAnalyticsData, crossDomain: true, responseType: {} })
method: method, .then((response) => {
url: apiGatewayUrl + endpoint,
data: payloadAndAnalyticsData,
crossDomain: true,
responseType: {},
}).then(
(response) => {
if (response.status == httpStatusCodes.OK) {
if (response.data == undefined) {
analyticsMixIn.methods.pushEventToGA(GaCategories.API_RESPONSE, GaActions.RESULT, if (logApiCall) {
`${GaLabels.ERROR}_${endpoint}`, true); analyticsMixIn.methods.pushEventToGA(GaCategories.API_RESPONSE, GaActions.RESULT, `${GaLabels.SUCCESS}_${endpoint}`, true);
reject(response);
} else {
analyticsMixIn.methods.pushEventToGA(GaCategories.API_RESPONSE, GaActions.RESULT,
`${GaLabels.SUCCESS}_${endpoint}`, true);
resolve(response);
}
} }
},
(error) => {
console.error(error);
analyticsMixIn.methods.pushEventToGA(GaCategories.API_RESPONSE, GaActions.RESULT,
`${GaLabels.ERROR}_${endpoint}`, true);
return reject(error.response); return resolve(response);
} },
); error => {
console.error(error);
if (logApiCall) {
analyticsMixIn.methods.pushEventToGA(GaCategories.API_RESPONSE, GaActions.RESULT, `${GaLabels.ERROR}_${endpoint}`, true);
}
return reject(error.response);
}
);
}); });
}, },
@ -58,11 +43,7 @@ export default {
responseType: {}, responseType: {},
}).then( }).then(
(response) => { (response) => {
if (response.status == httpStatusCodes.OK) { resolve(response);
resolve(response);
} else {
reject(response);
}
}, },
(error) => { (error) => {
return reject(error.response); return reject(error.response);

View file

@ -5,29 +5,31 @@ import { experimentSettings } from "@/constants/experiments";
import { analyticsPageEvents, GaCategories, GaActions, GaLabels, GaEvents } from "@/constants/analytics"; import { analyticsPageEvents, GaCategories, GaActions, GaLabels, GaEvents } from "@/constants/analytics";
import baseMixin from "@/mixins/base-mixin"; import baseMixin from "@/mixins/base-mixin";
import { applicationConfig } from "@/constants/application-config.js";
import axios from "axios";
// We will need current page name for multiple methods, define it once to re-use. // We will need current page name for multiple methods, define it once to re-use.
const currentPageName = getPageNameByQueryString(); const currentPageName = getPageNameByQueryString();
export default { export default {
methods: { methods: {
logEvent(destinationFmgPageValue, pageEvent, category, action, label, value){ logEvent(pageEvent, category, action, label, value) {
var payload = { var payload = {
userId: getDeviceIdValue(), userId: getDeviceIdValue(),
sessionKey: getSessionKeyValue(), sessionKey: getSessionKeyValue(),
pageName: destinationFmgPageValue, pageName: currentPageName,
sessionId: getSessionIdValue(), sessionId: getSessionIdValue(),
shouldUseSessionId: true, shouldUseSessionId: true,
}; };
if (pageEvent) { if (pageEvent) {
payload.pageEvent = {action: '', event: pageEvent}; payload.pageEvent = { action: '', event: pageEvent };
} }
if (category) { if (category) {
payload.customEvent = {category: category, action: action, label: label, value: value}; payload.customEvent = { category: category, action: action, label: label, value: value };
} }
baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.LOG_ACTIVITY, payload, false); baseMixin.methods.dispatchNonBlockingStoreAction(storeActions.LOG_ACTIVITY, payload, false);
}, },
@ -42,9 +44,9 @@ export default {
} }
pushToDataLayerIfDefined(eventToBePushed); pushToDataLayerIfDefined(eventToBePushed);
if (pushToLogApp) { if (pushToLogApp) {
this.logEvent(currentPageName, undefined, category, action, label, undefined); this.logEvent(undefined, category, action, label, undefined);
} }
}, },
@ -57,27 +59,27 @@ export default {
}; };
pushToDataLayerIfDefined(pageViewEvent); pushToDataLayerIfDefined(pageViewEvent);
//this.logEvent(currentPageName, analyticsPageEvents.ENTRY); //this.logEvent(currentPageName, analyticsPageEvents.ENTRY);
}, },
pushExperimentsToDataLayer(experiments){ pushExperimentsToDataLayer(experiments) {
experiments?.data?.forEach(exp => { experiments?.data?.forEach(exp => {
// Set Google Dimension Index based on experiment settings. // Set Google Dimension Index based on experiment settings.
let googleDimensionIndex = 99; let googleDimensionIndex = 99;
if (exp.settings[experimentSettings.GOOGLE_CUSTOM_DIMENSION_INDEX] !== undefined) { if (exp.settings[experimentSettings.GOOGLE_CUSTOM_DIMENSION_INDEX] !== undefined) {
googleDimensionIndex = exp.settings[experimentSettings.GOOGLE_CUSTOM_DIMENSION_INDEX]; googleDimensionIndex = exp.settings[experimentSettings.GOOGLE_CUSTOM_DIMENSION_INDEX];
} }
// Create object with dimension index and value. // Create object with dimension index and value.
const experimentWithDimension = {}; const experimentWithDimension = {};
Object.keys(exp).forEach(key => { Object.keys(exp).forEach(key => {
experimentWithDimension[`${key}_${googleDimensionIndex}`] = exp[key]; experimentWithDimension[`${key}_${googleDimensionIndex}`] = exp[key];
}); });
// Push to the data layer with the Google Custom Dimension Index. // Push to the data layer with the Google Custom Dimension Index.
pushToDataLayerIfDefined(experimentWithDimension); pushToDataLayerIfDefined(experimentWithDimension);
}); });
@ -108,9 +110,9 @@ function pushToDataLayerIfDefined(data) {
function getPageNameByQueryString() { function getPageNameByQueryString() {
const params = new URLSearchParams(location.search); const params = new URLSearchParams(location.search);
if(params.has(queryStrings.FMG_PAGE)) { if (params.has(queryStrings.FMG_PAGE)) {
return params.get(queryStrings.FMG_PAGE); return params.get(queryStrings.FMG_PAGE);
}else{ } else {
return ''; return '';
} }
} }

View file

@ -442,7 +442,8 @@ export const actions = {
return globalMethods.callHttpClient({ return globalMethods.callHttpClient({
method: endpoints.LogActivity.method, method: endpoints.LogActivity.method,
endpoint: endpoints.LogActivity.url, endpoint: endpoints.LogActivity.url,
payload: payload payload: payload,
logApiCall: false
}); });
}, },