react-marketing-tools 0.4.3 → 1.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ import { TrackAnalyticsEventOptions } from '../types/index.js';
2
+ /**
3
+ *
4
+ * @paramType {object} options
5
+ * @property {object} data this is an object of any data you want to collect in analytics
6
+ * @property {object} eventNameInfo
7
+ * @property {string} analyticsType
8
+ * @property {array | undefined} userDataKeysToHashArray default for this value is null, when used it should include an array of strings you wish to hash.
9
+ * @property {boolean} dataLayerCheck
10
+ * @property {object} consoleLogData (optional) is an object with the following attributes, "showGlobalVars", "showJourneyPropsPayload", "showUserProps" if any of the values are true you will be able to see the respective payload in your console.
11
+ */
12
+ declare const trackAnalyticsEvent: (options: TrackAnalyticsEventOptions) => Promise<void>;
13
+ export { trackAnalyticsEvent };
@@ -0,0 +1,3 @@
1
+ import { Ga4GoogleAnalyticsEventTracking } from '../types';
2
+ declare const ga4GoogleAnalyticsEventTracking: (options: Ga4GoogleAnalyticsEventTracking) => Promise<void>;
3
+ export default ga4GoogleAnalyticsEventTracking;
@@ -0,0 +1,3 @@
1
+ import { HandleDataLayerPushOptions } from '../types';
2
+ declare const handleDataLayerPush: (options: HandleDataLayerPushOptions) => Promise<void>;
3
+ export default handleDataLayerPush;
@@ -0,0 +1,41 @@
1
+ import DeviceDetector from 'device-detector-js';
2
+ import { AllowedTypes, EventNameInfo } from '../types';
3
+ /**
4
+ *
5
+ * @param {object} user the structure of this data is dictated by you, aka your API or DB.
6
+ * @param {[string]} includeUserKeys is an array of strings that represent keys from your user data that you want to whitelist, the value for each key in the whitelist will be hashed.
7
+ * @returns a new user object with all values hashed including only the whitelisted key value pairs
8
+ */
9
+ export declare const hashUserData: (user: Record<AllowedTypes, AllowedTypes>, includeUserKeys: Array<string>) => Promise<Record<AllowedTypes, AllowedTypes>>;
10
+ /**
11
+ *
12
+ * @param {object} user the structure of this data is dictated by you, aka your API or DB.
13
+ * @param {[string]} includeUserKeys is an array of strings that represent keys from your user data that you want to map over, if no keys are supplied the original passed in user data is returned.
14
+ * @param {boolean} showMissingUserAttributesInConsole a boolean condition to show or hide "user" attributes that are not included in the "includeUserKeys" array, by console logging in dev tools.
15
+ * @returns If no "includeUserKeys" are supplied the original ser object is returned, otherwise, a new user object with only the key value pairs that you included in your includeUserKeys array is returned.
16
+ */
17
+ export declare const buildNewUserData: (user: Record<AllowedTypes, AllowedTypes>, includeUserKeys: Array<string>, showMissingUserAttributesInConsole: boolean | undefined) => Record<string, string>;
18
+ /**
19
+ *
20
+ * @paramType {Object} eventNameInfo
21
+ * @property {analyticsPrefixActionList} actionPrefix either a built in value of JOURNEY | INTERACTION or a custom value added to the analyticsPrefixActionList object.
22
+ * @property {string} description an OPTIONAL value passed in that is formatted like the following from 'Very descriptive description' to VERY_DESCRIPTIVE_DESCRIPTION.
23
+ * @property {string} globalAppEvent the current event name for this track event selected from the analyticsGlobalEventActionList
24
+ * @property {string} previousGlobalAppEvent an OPTIONAL value, the previous event name for this track event selected from the analyticsGlobalEventActionList.
25
+ * @returns string eventName for this specific tracked event
26
+ */
27
+ export declare const buildAnalyticsEventName: (eventNameInfo: EventNameInfo) => string;
28
+ /**
29
+ *
30
+ * @param {object} data
31
+ * @param {string} globalAppEvent
32
+ * @returns dataObject
33
+ */
34
+ export declare const buildEventDataObject: (data: Record<AllowedTypes, AllowedTypes>, globalAppEvent: string) => Promise<Record<AllowedTypes, AllowedTypes>>;
35
+ /**
36
+ *
37
+ * @param {string} eventName
38
+ * @returns a Boolean value, true if the event name is already in the dataLayer.
39
+ */
40
+ export declare const defaultDataLayerEventCheck: (eventName: string) => boolean;
41
+ export declare const deviceDetectorInfo: (userAgent: string) => DeviceDetector.DeviceDetectorResult;
@@ -0,0 +1,8 @@
1
+ import { BuildConfigOptions, Config, AnalyticsPlatform } from '../types/index.js';
2
+ declare const analyticsPlatform: AnalyticsPlatform;
3
+ declare const showMeBuildInAnalyticsPlatform: () => void;
4
+ declare const showMeBuildInEventActionPrefixList: () => void;
5
+ declare const showMeBuildInGlobalEventActionList: () => void;
6
+ declare let config: Config;
7
+ declare const buildConfig: (options: BuildConfigOptions) => void;
8
+ export { config, analyticsPlatform, buildConfig, showMeBuildInAnalyticsPlatform, showMeBuildInGlobalEventActionList, showMeBuildInEventActionPrefixList, };
@@ -0,0 +1,6 @@
1
+ import type { Analytics, AnalyticsConfig } from './types.js';
2
+ /**
3
+ * Creates an analytics instance. It has no side effects until `start()`, so it is safe to create at module scope and
4
+ * during server rendering.
5
+ */
6
+ export declare const createAnalytics: (config: AnalyticsConfig) => Analytics;
@@ -0,0 +1,39 @@
1
+ export type ConsentStatus = 'granted' | 'denied';
2
+ /** Event parameters, passed to every destination as-is. */
3
+ export type EventParams = Record<string, unknown>;
4
+ export type AnalyticsEvent = {
5
+ name: string;
6
+ params: EventParams;
7
+ /** Unique per `track()` call and shared by every destination, so vendors can deduplicate the same event. */
8
+ eventId: string;
9
+ /** Milliseconds since the Unix epoch at the moment `track()` was called. */
10
+ timestamp: number;
11
+ };
12
+ /** Somewhere events are sent. Built-in destinations are configured by key; custom ones go in `destinations`. */
13
+ export type Destination = {
14
+ name: string;
15
+ /** Called once, in the browser, by `analytics.start()`. */
16
+ start(): void;
17
+ track(event: AnalyticsEvent): void;
18
+ };
19
+ export type GtmConfig = {
20
+ /** Google Tag Manager container ID, e.g. `GTM-XXXXXXX`. */
21
+ containerId: string;
22
+ /** Set to `false` when the page already includes the GTM snippet. Defaults to `true`. */
23
+ loadScript?: boolean;
24
+ };
25
+ export type AnalyticsConfig = {
26
+ /** Initial consent state. Required, so every site makes an explicit choice. */
27
+ consent: ConsentStatus;
28
+ gtm?: GtmConfig;
29
+ /** Custom destinations, in addition to the built-in ones. */
30
+ destinations?: Destination[];
31
+ /** Content-Security-Policy nonce added to every script the library injects. */
32
+ nonce?: string;
33
+ };
34
+ export type Analytics = {
35
+ /** Loads vendor scripts and delivers queued events. Safe to call more than once; does nothing outside the browser. */
36
+ start(): void;
37
+ /** Sends an event to every destination, queued until `start()`. Does nothing outside the browser. */
38
+ track(name: string, params?: EventParams): void;
39
+ };
package/dist/core.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { createAnalytics } from './core/createAnalytics.js';
2
+ export type { Analytics, AnalyticsConfig, AnalyticsEvent, ConsentStatus, Destination, EventParams, GtmConfig, } from './core/types.js';
package/dist/core.js ADDED
@@ -0,0 +1,59 @@
1
+ //#region lib/destinations/gtm.ts
2
+ var e = /^GTM-[A-Z0-9]+$/, t = () => window.dataLayer ??= [], n = (e) => Array.from(document.scripts).some((t) => t.src === e), r = ({ src: e, nonce: t }) => {
3
+ let n = document.createElement("script");
4
+ n.async = !0, n.src = e, t && n.setAttribute("nonce", t), document.head.append(n);
5
+ }, i = ({ containerId: i, loadScript: a = !0, nonce: o }) => {
6
+ if (!e.test(i)) throw Error(`[react-marketing-tools] gtm.containerId must look like "GTM-XXXXXXX" (received ${JSON.stringify(i)}).`);
7
+ let s = `https://www.googletagmanager.com/gtm.js?id=${i}`;
8
+ return {
9
+ name: "gtm",
10
+ start() {
11
+ let e = t();
12
+ a && !n(s) && (e.push({
13
+ "gtm.start": Date.now(),
14
+ event: "gtm.js"
15
+ }), r({
16
+ src: s,
17
+ nonce: o
18
+ }));
19
+ },
20
+ track({ name: e, params: n, eventId: r }) {
21
+ t().push({
22
+ ...n,
23
+ event: e,
24
+ event_id: r
25
+ });
26
+ }
27
+ };
28
+ }, a = () => typeof window < "u", o = () => typeof crypto.randomUUID == "function" ? crypto.randomUUID() : "10000000-1000-4000-8000-100000000000".replace(/[018]/g, (e) => (Number(e) ^ crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(1))[0] & 15 >> Number(e) / 4).toString(16)), s = (e) => {
29
+ if (e.consent !== "granted" && e.consent !== "denied") throw Error(`[react-marketing-tools] createAnalytics: "consent" must be 'granted' or 'denied' (received ${JSON.stringify(e.consent)}).`);
30
+ }, c = (e) => {
31
+ s(e);
32
+ let t = [...e.gtm ? [i({
33
+ ...e.gtm,
34
+ nonce: e.nonce
35
+ })] : [], ...e.destinations ?? []], n = [], r = !1, c = (e) => {
36
+ for (let n of t) n.track(e);
37
+ };
38
+ return {
39
+ start() {
40
+ if (!r && a()) {
41
+ r = !0;
42
+ for (let e of t) e.start();
43
+ n.splice(0).forEach(c);
44
+ }
45
+ },
46
+ track(e, t = {}) {
47
+ if (!a()) return;
48
+ let i = {
49
+ name: e,
50
+ params: t,
51
+ eventId: o(),
52
+ timestamp: Date.now()
53
+ };
54
+ r ? c(i) : n.push(i);
55
+ }
56
+ };
57
+ };
58
+ //#endregion
59
+ export { c as createAnalytics };
@@ -0,0 +1,6 @@
1
+ import type { Destination, GtmConfig } from '../core/types.js';
2
+ type GtmDestinationOptions = GtmConfig & {
3
+ nonce?: string;
4
+ };
5
+ export declare const createGtmDestination: ({ containerId, loadScript, nonce, }: GtmDestinationOptions) => Destination;
6
+ export {};
package/dist/index.d.ts CHANGED
@@ -1,14 +1,14 @@
1
- import { ReactNode, Context } from 'react';
2
- import type { ProviderStateProps, ProviderApiProps } from './types';
3
- export type { Event, GooglePayload, EventNameInfo, AllowedTypes, TrackAnalyticsEventOptions, GlobalVars, DataLayer, ConsoleLogData, AnalyticsTrackerDataOptions, HandleDataLayerPushOptions, ServerLocationData, IpInfo, Ga4GoogleAnalyticsEventTracking, Platform, AnalyticsPlatform, Tokens, Config, BuildConfigOptions, ProviderStateProps, ProviderApiProps, AnalyticsEventActionPrefix, AnalyticsGlobalEventAction, } from './types';
4
- export { config, analyticsPlatform, buildConfig, showMeBuildInAnalyticsPlatform, showMeBuildInGlobalEventActionList, showMeBuildInEventActionPrefixList, } from './buildConfig';
5
- export { trackAnalyticsEvent } from './analytics/analyticsEventService';
1
+ import { ReactElement, ReactNode, Context } from 'react';
2
+ import type { ProviderStateProps, ProviderApiProps } from './types/index.js';
3
+ export type { Event, GooglePayload, EventNameInfo, AllowedTypes, TrackAnalyticsEventOptions, GlobalVars, DataLayer, ConsoleLogData, AnalyticsTrackerDataOptions, HandleDataLayerPushOptions, ServerLocationData, IpInfo, Ga4GoogleAnalyticsEventTracking, Platform, AnalyticsPlatform, Tokens, Config, BuildConfigOptions, ProviderStateProps, ProviderApiProps, AnalyticsEventActionPrefix, AnalyticsGlobalEventAction, } from './types/index.js';
4
+ export { config, analyticsPlatform, buildConfig, showMeBuildInAnalyticsPlatform, showMeBuildInGlobalEventActionList, showMeBuildInEventActionPrefixList, } from './buildConfig/index.js';
5
+ export { trackAnalyticsEvent } from './analytics/analyticsEventService.js';
6
6
  export declare const ContextState: Context<ProviderStateProps>;
7
7
  export declare const ContextApi: Context<ProviderApiProps>;
8
8
  type ReactMarketingProviderProps = {
9
9
  children: ReactNode;
10
10
  };
11
- export declare const ReactMarketingProvider: ({ children, }: ReactMarketingProviderProps) => import("react/jsx-runtime").JSX.Element;
11
+ export declare const ReactMarketingProvider: ({ children, }: ReactMarketingProviderProps) => ReactElement;
12
12
  /**
13
13
  * @property {string} appName
14
14
  * @property {string} appSessionCookieName