UNPKG

@honeypot-run/core

Version:

Patented threat analytics and fingerprinting library. Requires a honeypot.run account

96 lines (95 loc) 4.99 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; export const createAmplitudePlugin = (track, get, identifyFn, pluginConfig) => { let pageViews = 0; const debug = (message) => { try { let forceDebug = new URLSearchParams(window.location.search).has('hpdebug'); if ((pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.debug) === true || forceDebug) { console.log(`[hp] ${message}`); } } catch (error) { } }; return { name: 'honeypot-enrichment', type: 'before', setup: (config) => __awaiter(void 0, void 0, void 0, function* () { try { if (config.userId) { if (!(pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.trackIdentities)) { debug(`ignoring user_id since trackIdentities is set to false: ${config.userId}`); } else { debug(`setting user_id: ${config.userId}`); yield identifyFn(config.userId); } } } catch (error) { } let honey = yield get(); debug(`initialized plugin. honey=${JSON.stringify(honey)}`); }), execute: (event) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b; try { const { user_id, event_type } = event; try { if (user_id) { if (!(pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.trackIdentities)) { debug(`ignoring identify event since trackIdentities is set to false: ${user_id}`); } else { debug(`processing identify event: ${user_id}`); yield identifyFn(user_id); } } } catch (error) { } const isInternal = event_type.startsWith('[Amplitude] '); const cleanedEventType = isInternal ? event_type.replace('[Amplitude] ', '') : event_type; pageViews = pageViews + 1; let skipTrack = false; const shouldIgnore = ((_a = pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.excludeEvents) === null || _a === void 0 ? void 0 : _a.includes(cleanedEventType)) || (((_b = pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.includeEvents) === null || _b === void 0 ? void 0 : _b.length) && !(pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.includeEvents.includes(cleanedEventType))); if (shouldIgnore) { if (pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.enrichEventProperties) { const honey = { ignored: true }; event.event_properties = Object.assign(Object.assign({}, event.event_properties), { honey }); } debug(`ignoring event: ${event_type}`); return event; } if (!skipTrack) { debug(`tracking event: ${event_type}`); yield track(event_type, event.event_properties); } const honey = yield get(); if ((honey === null || honey === void 0 ? void 0 : honey.geofenced) && (honey === null || honey === void 0 ? void 0 : honey.redirect_auto)) { debug(`dropping event: ${event_type}`); return null; } if (pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.enrichEventProperties) { event.event_properties = Object.assign(Object.assign({}, event.event_properties), { honey }); debug(`enriching event: ${event_type}. ${JSON.stringify(event.event_properties)}`); } return event; } catch (error) { return event; } }), }; };