UNPKG

@yext/analytics

Version:

An analytics library for Yext

69 lines 3.15 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.reportBrowserAnalytics = exports.analytics = void 0; const AnalyticsEventReporter_1 = require("./AnalyticsEventReporter"); const convertStringToValue_1 = require("./convertStringToValue"); /** * The Yext Analytics Events SDK. * Returns an AnalyticsEventService given an AnalyticsConfig. * @public */ function analytics(config) { return new AnalyticsEventReporter_1.AnalyticsEventReporter(config); } exports.analytics = analytics; /** * An alternative entry point for the Yext Analytics Events SDK, currently used by Google Tag Manager. * This method reads the config and payload from the variable analyticsEventPayload stored * in the global window object. The config and payload are then passed to the report function to be sent * to the Yext Analytics Events API. * @public */ function reportBrowserAnalytics() { const gtmPayload = window['analyticsEventPayload']; let response; if (gtmPayload) { const config = gtmPayload[0][1]; const data = gtmPayload[1][1]; if (config) { // User text input inside of Google Tag Manager defaults to a String type for all fields. // However, the Events API expects true boolean and number types for certain fields. // The below convertStringToValue method calls take care of converting the String types // to the correct one's for the config and payload objects. const correctedConfig = (0, convertStringToValue_1.convertStringToValue)(config); const correctedData = (0, convertStringToValue_1.convertStringToValue)(data); const reporter = new AnalyticsEventReporter_1.AnalyticsEventReporter(correctedConfig); response = reporter.report(correctedData); } else { response = Promise.reject('No config found in payload.'); } } else { response = Promise.reject('No payload found.'); } return response; } exports.reportBrowserAnalytics = reportBrowserAnalytics; __exportStar(require("./AnalyticsConfig"), exports); __exportStar(require("./AnalyticsEventService"), exports); __exportStar(require("./Environment"), exports); __exportStar(require("./Region"), exports); __exportStar(require("./EventPayload"), exports); __exportStar(require("./Action"), exports); __exportStar(require("./VersionLabel"), exports); //# sourceMappingURL=index.js.map