@widergy/web-utils
Version:
Utility GO! Web utils
179 lines (178 loc) • 8.78 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateEventId = exports.generateFlowId = exports.intercomUpdateUser = exports.intercomSingleEvent = exports.mixpanelUserReset = exports.mixpanelGroupProfileSet = exports.mixpanelSuperPropertiesRegister = exports.mixpanelPeopleSet = exports.mixpanelUserIdentify = exports.singleMixpanelEvent = exports.singleArgosEvent = exports.singleInhouseEvent = exports.singleGAEvent = exports.singleEventMultitracking = exports.multiTracking = exports.retrievePublicUserId = exports.createMiddleware = exports.setGAUserId = exports.sendGAPageView = exports.sendGAEvent = exports.initializeGA = void 0;
const react_ga4_1 = __importDefault(require("react-ga4"));
const lodash_1 = require("lodash");
const uuid_1 = require("uuid");
const ANALYTICS_TRACKING_ID_STARTING_CHARACTERS = 'G-';
const isValidAnalyticsTrackingId = (analyticsTrackingId) => {
return !!analyticsTrackingId && analyticsTrackingId.startsWith(ANALYTICS_TRACKING_ID_STARTING_CHARACTERS);
};
const initializeGA = (analyticsTrackingId, options = {
titleCase: false,
}) => {
if (!isValidAnalyticsTrackingId(analyticsTrackingId)) {
console.error('El ID de Google Analytics 4 es inválido');
return;
}
react_ga4_1.default.initialize(analyticsTrackingId, options);
react_ga4_1.default.send({ hitType: 'pageview', page: document.location.pathname });
};
exports.initializeGA = initializeGA;
const GAEvent = (eventData) => react_ga4_1.default.event(eventData);
const sendGAEvent = (category, action, label, value) => {
GAEvent({ category, action, label, value });
};
exports.sendGAEvent = sendGAEvent;
const sendGAPageView = (pathname) => {
react_ga4_1.default.send({ hitType: 'pageview', page: pathname });
};
exports.sendGAPageView = sendGAPageView;
const setGAUserId = (userId) => {
react_ga4_1.default.set({ userId });
};
exports.setGAUserId = setGAUserId;
const getEventData = (action, eventDataDefinition) => { var _a; return (_a = eventDataDefinition[action.type]) === null || _a === void 0 ? void 0 : _a.call(eventDataDefinition, action); };
const createMiddleware = (eventDataDefinition, customTracker, googleTracking = true) => () => (next) => (action) => {
const result = next(action);
const data = getEventData(action, eventDataDefinition);
if (data && Array.isArray(data)) {
if (googleTracking)
data.forEach((event) => GAEvent(event));
if (customTracker) {
data.forEach((event) => customTracker(event));
}
}
else if (data) {
if (googleTracking)
GAEvent(data);
if (customTracker) {
customTracker(data);
}
}
return result;
};
exports.createMiddleware = createMiddleware;
const retrievePublicUserId = () => {
const randomNumber = Math.round(Math.random() * 10 ** 10);
const currentTime = new Date(Date.now());
return `${randomNumber}.${currentTime.getDate()}-${currentTime.getMonth() + 1}-${currentTime.getFullYear()}.${currentTime.getHours()}:${currentTime.getMinutes()}:${currentTime.getSeconds()}`;
};
exports.retrievePublicUserId = retrievePublicUserId;
const multiTracking = (trackerArgumentsArray) => trackerArgumentsArray.map((trackerArguments) => (0, exports.createMiddleware)(trackerArguments.eventDataDefinition, trackerArguments.customTracker, trackerArguments.googleTracking));
exports.multiTracking = multiTracking;
const singleEventMultitracking = (trackers, eventDefinitions) => (name, eventData) => {
const mergedData = (0, lodash_1.merge)(eventDefinitions[name] || {}, eventData || {});
const { sharedProps } = mergedData, trackerProps = __rest(mergedData, ["sharedProps"]);
const trackersArray = Object.keys(mergedData);
if (!(0, lodash_1.isEmpty)(trackers))
trackersArray.forEach((trackerName) => {
const eventTracker = trackers[trackerName];
const fullEventData = (0, lodash_1.merge)(trackerProps[trackerName], sharedProps);
eventTracker === null || eventTracker === void 0 ? void 0 : eventTracker(fullEventData);
});
};
exports.singleEventMultitracking = singleEventMultitracking;
const singleGAEvent = () => (eventData) => {
const { action, category, label, value } = eventData;
(0, exports.sendGAEvent)(category, action, label, value);
};
exports.singleGAEvent = singleGAEvent;
const singleInhouseEvent = (sendInhouseAnalytics) => (eventData) => {
sendInhouseAnalytics(eventData);
};
exports.singleInhouseEvent = singleInhouseEvent;
const singleArgosEvent = (sendArgosAnalytics) => (eventData) => {
sendArgosAnalytics(eventData);
};
exports.singleArgosEvent = singleArgosEvent;
const singleMixpanelEvent = (mixpanel) => (eventData) => {
const { name } = eventData, yourProperties = __rest(eventData, ["name"]);
if (mixpanel.config && !(0, lodash_1.isEmpty)(name))
mixpanel.track(name, yourProperties);
};
exports.singleMixpanelEvent = singleMixpanelEvent;
const mixpanelUserIdentify = (mixpanel) => (userProfileProperties, utilityId) => {
const { id, externalId, email } = userProfileProperties;
const distinctId = `${utilityId}-${externalId !== null && externalId !== void 0 ? externalId : id}`;
if (mixpanel.config) {
mixpanel.identify(distinctId);
mixpanel.people.set(Object.assign({ $name: distinctId, $email: email !== null && email !== void 0 ? email : null }, userProfileProperties));
}
};
exports.mixpanelUserIdentify = mixpanelUserIdentify;
const mixpanelPeopleSet = (mixpanel) => (userProfileProperties) => {
if (mixpanel.config)
mixpanel.people.set(userProfileProperties);
};
exports.mixpanelPeopleSet = mixpanelPeopleSet;
const mixpanelSuperPropertiesRegister = (mixpanel) => (superProperties) => {
if (mixpanel.config)
mixpanel.register(superProperties);
};
exports.mixpanelSuperPropertiesRegister = mixpanelSuperPropertiesRegister;
const mixpanelGroupProfileSet = (mixpanel) => (groupKey, groupId, profileName) => {
if (mixpanel.config)
mixpanel.get_group(groupKey, String(groupId)).set({
$distinct_id: groupId,
$name: profileName,
});
};
exports.mixpanelGroupProfileSet = mixpanelGroupProfileSet;
const mixpanelUserReset = (mixpanel) => () => {
if (mixpanel.config)
mixpanel.reset();
};
exports.mixpanelUserReset = mixpanelUserReset;
const intercomSingleEvent = (trackEventFunction) => (eventData) => {
const { name } = eventData, eventMetadata = __rest(eventData, ["name"]);
if (trackEventFunction)
trackEventFunction(name, eventMetadata);
};
exports.intercomSingleEvent = intercomSingleEvent;
const intercomUpdateUser = (updaterFunction) => (userProperties) => {
updaterFunction(userProperties);
};
exports.intercomUpdateUser = intercomUpdateUser;
const generateFlowId = () => (0, uuid_1.v4)();
exports.generateFlowId = generateFlowId;
const generateEventId = () => (0, uuid_1.v4)();
exports.generateEventId = generateEventId;
const ANALYTICS_UTILS = {
createMiddleware: exports.createMiddleware,
generateEventId: exports.generateEventId,
generateFlowId: exports.generateFlowId,
initializeGA: exports.initializeGA,
intercomSingleEvent: exports.intercomSingleEvent,
intercomUpdateUser: exports.intercomUpdateUser,
mixpanelPeopleSet: exports.mixpanelPeopleSet,
mixpanelSuperPropertiesRegister: exports.mixpanelSuperPropertiesRegister,
mixpanelGroupProfileSet: exports.mixpanelGroupProfileSet,
mixpanelUserIdentify: exports.mixpanelUserIdentify,
mixpanelUserReset: exports.mixpanelUserReset,
multiTracking: exports.multiTracking,
retrievePublicUserId: exports.retrievePublicUserId,
sendGAEvent: exports.sendGAEvent,
sendGAPageView: exports.sendGAPageView,
setGAUserId: exports.setGAUserId,
singleEventMultitracking: exports.singleEventMultitracking,
singleGAEvent: exports.singleGAEvent,
singleInhouseEvent: exports.singleInhouseEvent,
singleArgosEvent: exports.singleArgosEvent,
singleMixpanelEvent: exports.singleMixpanelEvent,
};
exports.default = ANALYTICS_UTILS;