@croct/plug 0.18.1 → 0.19.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.
Files changed (48) hide show
  1. package/README.md +2 -1
  2. package/api/fetchContent.d.cts +1 -2
  3. package/api/fetchContent.d.ts +1 -2
  4. package/api/index.d.cts +1 -2
  5. package/api/index.d.ts +1 -2
  6. package/component.d.cts +1 -1
  7. package/component.d.ts +1 -1
  8. package/constants.cjs +1 -1
  9. package/constants.d.cts +1 -1
  10. package/constants.d.ts +1 -1
  11. package/constants.js +1 -1
  12. package/global.d.cjs +0 -0
  13. package/global.d.d.cts +30 -0
  14. package/global.d.d.ts +30 -0
  15. package/global.d.js +0 -0
  16. package/index.d.cts +2 -3
  17. package/index.d.ts +2 -3
  18. package/package.json +15 -2
  19. package/plug-Cf6SnDp4.d.cts +110 -0
  20. package/plug-DjUeWnq0.d.ts +110 -0
  21. package/plug.cjs +7 -4
  22. package/plug.d.cts +11 -77
  23. package/plug.d.ts +11 -77
  24. package/plug.js +7 -4
  25. package/plugin.d.cts +19 -39
  26. package/plugin.d.ts +19 -39
  27. package/plugins/autoTracking/index.cjs +148 -0
  28. package/plugins/autoTracking/index.d.cts +50 -0
  29. package/plugins/autoTracking/index.d.ts +50 -0
  30. package/plugins/autoTracking/index.js +124 -0
  31. package/plugins/autoTracking/structuredData.cjs +379 -0
  32. package/plugins/autoTracking/structuredData.d.cts +42 -0
  33. package/plugins/autoTracking/structuredData.d.ts +42 -0
  34. package/plugins/autoTracking/structuredData.js +353 -0
  35. package/plugins/globalVariable/index.cjs +64 -0
  36. package/plugins/globalVariable/index.d.cts +36 -0
  37. package/plugins/globalVariable/index.d.ts +36 -0
  38. package/plugins/globalVariable/index.js +40 -0
  39. package/{preview.cjs → plugins/preview/index.cjs} +2 -2
  40. package/{preview.d.ts → plugins/preview/index.d.cts} +14 -5
  41. package/{preview.d.cts → plugins/preview/index.d.ts} +14 -5
  42. package/{preview.js → plugins/preview/index.js} +2 -2
  43. package/sdk/index.d.cts +1 -1
  44. package/sdk/index.d.ts +1 -1
  45. package/playground.cjs +0 -166
  46. package/playground.d.cts +0 -64
  47. package/playground.d.ts +0 -64
  48. package/playground.js +0 -142
package/plug.d.ts CHANGED
@@ -1,14 +1,14 @@
1
- import { SessionFacade } from '@croct/sdk/facade/sessionFacade';
2
- import { UserFacade } from '@croct/sdk/facade/userFacade';
3
- import { TrackerFacade } from '@croct/sdk/facade/trackerFacade';
4
- import { EvaluationOptions, EvaluatorFacade } from '@croct/sdk/facade/evaluatorFacade';
5
- import { Configuration as Configuration$1 } from '@croct/sdk/facade/sdkFacade';
6
- import { Optional } from '@croct/sdk/utilityTypes';
7
- import { ExternalTrackingEventType, ExternalTrackingEventPayload, ExternalTrackingEvent } from '@croct/sdk/trackingEvents';
8
- import { FetchOptions as FetchOptions$1 } from '@croct/sdk/facade/contentFetcherFacade';
9
- import { PluginFactory } from './plugin.js';
10
- import { VersionedSlotId, SlotContent } from './slot.js';
11
- import { JsonValue, JsonObject } from '@croct/json/mutable';
1
+ import '@croct/sdk/facade/sessionFacade';
2
+ import '@croct/sdk/facade/userFacade';
3
+ import '@croct/sdk/facade/trackerFacade';
4
+ import '@croct/sdk/facade/evaluatorFacade';
5
+ import '@croct/sdk/facade/sdkFacade';
6
+ import '@croct/sdk/utilityTypes';
7
+ import '@croct/sdk/trackingEvents';
8
+ import '@croct/sdk/facade/contentFetcherFacade';
9
+ export { C as Configuration, f as FetchOptions, F as FetchResponse, G as GlobalPlug, P as Plug, e as PluginConfigurations } from './plug-DjUeWnq0.js';
10
+ import './slot.js';
11
+ import '@croct/json/mutable';
12
12
  import '@croct/sdk/token';
13
13
  import './sdk/index.js';
14
14
  import '@croct/sdk/eventManager';
@@ -19,69 +19,3 @@ import '@croct/sdk/cid';
19
19
  import '@croct/json';
20
20
  import './versioning.js';
21
21
  import './component.js';
22
-
23
- interface PluginConfigurations {
24
- [key: string]: any;
25
- }
26
- type Configuration = Optional<Configuration$1, 'appId'> & {
27
- plugins?: PluginConfigurations;
28
- };
29
- type FetchOptions<T> = Omit<FetchOptions$1, 'version'> & {
30
- fallback?: T;
31
- };
32
- type FetchResponse<I extends VersionedSlotId, C extends JsonObject = JsonObject, F = never> = {
33
- content: SlotContent<I, C> | F;
34
- };
35
- interface Plug {
36
- readonly tracker: TrackerFacade;
37
- readonly user: UserFacade;
38
- readonly session: SessionFacade;
39
- readonly initialized: boolean;
40
- readonly flushed: Promise<this>;
41
- readonly plugged: Promise<this>;
42
- plug(configuration: Configuration): void;
43
- isAnonymous(): boolean;
44
- getUserId(): string | null;
45
- identify(userId: string): void;
46
- anonymize(): void;
47
- setToken(token: string): void;
48
- unsetToken(): void;
49
- track<T extends ExternalTrackingEventType>(type: T, payload: ExternalTrackingEventPayload<T>): Promise<ExternalTrackingEvent<T>>;
50
- evaluate<T extends JsonValue>(expression: string, options?: EvaluationOptions): Promise<T>;
51
- fetch<I extends VersionedSlotId>(slotId: I, options?: FetchOptions<SlotContent<I>>): Promise<FetchResponse<I>>;
52
- fetch<F, I extends VersionedSlotId>(slotId: I, options?: FetchOptions<SlotContent<I> | F>): Promise<FetchResponse<I, JsonObject, F>>;
53
- unplug(): Promise<void>;
54
- }
55
- declare class GlobalPlug implements Plug {
56
- static readonly GLOBAL: GlobalPlug;
57
- private pluginFactories;
58
- private instance?;
59
- private plugins;
60
- private initialize;
61
- private ready;
62
- constructor();
63
- extend(name: string, plugin: PluginFactory): void;
64
- plug(configuration?: Configuration): void;
65
- get initialized(): boolean;
66
- get plugged(): Promise<this>;
67
- get flushed(): Promise<this>;
68
- private get sdk();
69
- get tracker(): TrackerFacade;
70
- get evaluator(): EvaluatorFacade;
71
- get user(): UserFacade;
72
- get session(): SessionFacade;
73
- isAnonymous(): boolean;
74
- getUserId(): string | null;
75
- identify(userId: string): void;
76
- anonymize(): void;
77
- setToken(token: string): void;
78
- unsetToken(): void;
79
- track<T extends ExternalTrackingEventType>(type: T, payload: ExternalTrackingEventPayload<T>): Promise<ExternalTrackingEvent<T>>;
80
- evaluate<T extends JsonValue>(query: string, options?: EvaluationOptions): Promise<T>;
81
- test(expression: string, options?: EvaluationOptions): Promise<boolean>;
82
- fetch<I extends VersionedSlotId>(slotId: I, options?: FetchOptions<SlotContent<I>>): Promise<FetchResponse<I>>;
83
- fetch<F, I extends VersionedSlotId>(slotId: I, options?: FetchOptions<SlotContent<I> | F>): Promise<FetchResponse<I, JsonObject, F>>;
84
- unplug(): Promise<void>;
85
- }
86
-
87
- export { type Configuration, type FetchOptions, type FetchResponse, GlobalPlug, type Plug, type PluginConfigurations };
package/plug.js CHANGED
@@ -5,8 +5,9 @@ import { Token } from "@croct/sdk/token";
5
5
  import { VERSION } from "@croct/sdk";
6
6
  import { loadSlotContent } from "@croct/content";
7
7
  import { CDN_URL } from "./constants.js";
8
- import { factory as playgroundPluginFactory } from "./playground.js";
9
- import { factory as previewPluginFactory } from "./preview.js";
8
+ import { factory as previewPluginFactory } from "./plugins/preview/index.js";
9
+ import { factory as autoTrackingPluginFactory } from "./plugins/autoTracking/index.js";
10
+ import { factory as globalVariablePluginFactory } from "./plugins/globalVariable/index.js";
10
11
  const PLUGIN_NAMESPACE = "Plugin";
11
12
  function detectAppId() {
12
13
  const script = window.document.querySelector(`script[src^='${CDN_URL}']`);
@@ -18,8 +19,9 @@ function detectAppId() {
18
19
  const _GlobalPlug = class _GlobalPlug {
19
20
  constructor() {
20
21
  this.pluginFactories = {
21
- playground: playgroundPluginFactory,
22
- preview: previewPluginFactory
22
+ preview: previewPluginFactory,
23
+ globalVariable: globalVariablePluginFactory,
24
+ autoTracking: autoTrackingPluginFactory
23
25
  };
24
26
  this.plugins = {};
25
27
  this.ready = new Promise((resolve) => {
@@ -93,6 +95,7 @@ const _GlobalPlug = class _GlobalPlug {
93
95
  sdk: {
94
96
  version: VERSION,
95
97
  appId,
98
+ plug: this,
96
99
  tracker: sdk.tracker,
97
100
  evaluator: sdk.evaluator,
98
101
  user: sdk.user,
package/plugin.d.cts CHANGED
@@ -1,41 +1,21 @@
1
- import { TokenStore } from '@croct/sdk/token';
2
- import { EvaluatorFacade } from '@croct/sdk/facade/evaluatorFacade';
3
- import { TrackerFacade } from '@croct/sdk/facade/trackerFacade';
4
- import { SdkEventManager } from './sdk/index.cjs';
5
- import { UserFacade } from '@croct/sdk/facade/userFacade';
6
- import { SessionFacade } from '@croct/sdk/facade/sessionFacade';
7
- import { Tab } from '@croct/sdk/tab';
8
- import { CidAssigner } from '@croct/sdk/cid';
9
- import { Logger } from '@croct/sdk/logging';
1
+ import '@croct/sdk/token';
2
+ import '@croct/sdk/facade/evaluatorFacade';
3
+ import '@croct/sdk/facade/trackerFacade';
4
+ import './sdk/index.cjs';
5
+ export { a as Plugin, b as PluginArguments, c as PluginFactory, d as PluginSdk } from './plug-Cf6SnDp4.cjs';
6
+ import '@croct/sdk/facade/userFacade';
7
+ import '@croct/sdk/facade/sessionFacade';
8
+ import '@croct/sdk/tab';
9
+ import '@croct/sdk/cid';
10
+ import '@croct/sdk/logging';
10
11
  import '@croct/sdk/eventManager';
11
12
  import '@croct/sdk/sdkEvents';
12
-
13
- interface PluginSdk {
14
- readonly version: string;
15
- readonly appId: string;
16
- readonly tracker: TrackerFacade;
17
- readonly evaluator: EvaluatorFacade;
18
- readonly user: UserFacade;
19
- readonly session: SessionFacade;
20
- readonly tab: Tab;
21
- readonly userTokenStore: TokenStore;
22
- readonly previewTokenStore: TokenStore;
23
- readonly cidAssigner: CidAssigner;
24
- readonly eventManager: SdkEventManager;
25
- getLogger(...namespace: string[]): Logger;
26
- getTabStorage(...namespace: string[]): Storage;
27
- getBrowserStorage(...namespace: string[]): Storage;
28
- }
29
- interface PluginArguments<T = any> {
30
- options: T;
31
- sdk: PluginSdk;
32
- }
33
- interface PluginFactory<T = any> {
34
- (args: PluginArguments<T>): Plugin;
35
- }
36
- interface Plugin {
37
- enable(): Promise<void> | void;
38
- disable?(): Promise<void> | void;
39
- }
40
-
41
- export type { Plugin, PluginArguments, PluginFactory, PluginSdk };
13
+ import '@croct/sdk/facade/sdkFacade';
14
+ import '@croct/sdk/utilityTypes';
15
+ import '@croct/sdk/trackingEvents';
16
+ import '@croct/sdk/facade/contentFetcherFacade';
17
+ import './slot.cjs';
18
+ import '@croct/json';
19
+ import './versioning.cjs';
20
+ import './component.cjs';
21
+ import '@croct/json/mutable';
package/plugin.d.ts CHANGED
@@ -1,41 +1,21 @@
1
- import { TokenStore } from '@croct/sdk/token';
2
- import { EvaluatorFacade } from '@croct/sdk/facade/evaluatorFacade';
3
- import { TrackerFacade } from '@croct/sdk/facade/trackerFacade';
4
- import { SdkEventManager } from './sdk/index.js';
5
- import { UserFacade } from '@croct/sdk/facade/userFacade';
6
- import { SessionFacade } from '@croct/sdk/facade/sessionFacade';
7
- import { Tab } from '@croct/sdk/tab';
8
- import { CidAssigner } from '@croct/sdk/cid';
9
- import { Logger } from '@croct/sdk/logging';
1
+ import '@croct/sdk/token';
2
+ import '@croct/sdk/facade/evaluatorFacade';
3
+ import '@croct/sdk/facade/trackerFacade';
4
+ import './sdk/index.js';
5
+ export { a as Plugin, b as PluginArguments, c as PluginFactory, d as PluginSdk } from './plug-DjUeWnq0.js';
6
+ import '@croct/sdk/facade/userFacade';
7
+ import '@croct/sdk/facade/sessionFacade';
8
+ import '@croct/sdk/tab';
9
+ import '@croct/sdk/cid';
10
+ import '@croct/sdk/logging';
10
11
  import '@croct/sdk/eventManager';
11
12
  import '@croct/sdk/sdkEvents';
12
-
13
- interface PluginSdk {
14
- readonly version: string;
15
- readonly appId: string;
16
- readonly tracker: TrackerFacade;
17
- readonly evaluator: EvaluatorFacade;
18
- readonly user: UserFacade;
19
- readonly session: SessionFacade;
20
- readonly tab: Tab;
21
- readonly userTokenStore: TokenStore;
22
- readonly previewTokenStore: TokenStore;
23
- readonly cidAssigner: CidAssigner;
24
- readonly eventManager: SdkEventManager;
25
- getLogger(...namespace: string[]): Logger;
26
- getTabStorage(...namespace: string[]): Storage;
27
- getBrowserStorage(...namespace: string[]): Storage;
28
- }
29
- interface PluginArguments<T = any> {
30
- options: T;
31
- sdk: PluginSdk;
32
- }
33
- interface PluginFactory<T = any> {
34
- (args: PluginArguments<T>): Plugin;
35
- }
36
- interface Plugin {
37
- enable(): Promise<void> | void;
38
- disable?(): Promise<void> | void;
39
- }
40
-
41
- export type { Plugin, PluginArguments, PluginFactory, PluginSdk };
13
+ import '@croct/sdk/facade/sdkFacade';
14
+ import '@croct/sdk/utilityTypes';
15
+ import '@croct/sdk/trackingEvents';
16
+ import '@croct/sdk/facade/contentFetcherFacade';
17
+ import './slot.js';
18
+ import '@croct/json';
19
+ import './versioning.js';
20
+ import './component.js';
21
+ import '@croct/json/mutable';
@@ -0,0 +1,148 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var autoTracking_exports = {};
19
+ __export(autoTracking_exports, {
20
+ AutoTrackingPlugin: () => AutoTrackingPlugin,
21
+ factory: () => factory
22
+ });
23
+ module.exports = __toCommonJS(autoTracking_exports);
24
+ var import_structuredData = require('./structuredData.cjs');
25
+ class AutoTrackingPlugin {
26
+ constructor(configuration) {
27
+ this.tab = configuration.tab;
28
+ this.tracker = configuration.tracker;
29
+ this.options = configuration.options;
30
+ this.trackStructuredData = this.trackStructuredData.bind(this);
31
+ this.trackLinkOpened = this.trackLinkOpened.bind(this);
32
+ }
33
+ isDisabled() {
34
+ return this.options?.disablePostViewed === true && this.options?.disableProductViewed === true && this.options?.disableLinkOpened === true;
35
+ }
36
+ enable() {
37
+ if (this.isDisabled()) {
38
+ return;
39
+ }
40
+ this.trackStructuredData();
41
+ this.tab.addListener("urlChange", this.trackStructuredData);
42
+ if (this.options?.disableLinkOpened !== true) {
43
+ document.addEventListener("click", this.trackLinkOpened, true);
44
+ }
45
+ }
46
+ disable() {
47
+ this.tab.removeListener("urlChange", this.trackStructuredData);
48
+ document.removeEventListener("click", this.trackLinkOpened, true);
49
+ }
50
+ trackStructuredData() {
51
+ const structuredDataElements = document.querySelectorAll('script[type="application/ld+json"]');
52
+ for (const element of structuredDataElements) {
53
+ const entity = (0, import_structuredData.parseEntity)(element.textContent ?? "");
54
+ switch (entity?.type) {
55
+ case "post":
56
+ if (this.options?.disablePostViewed !== true) {
57
+ this.trackPostViewed(entity);
58
+ }
59
+ break;
60
+ case "product":
61
+ case "service":
62
+ if (this.options?.disableProductViewed !== true) {
63
+ this.trackProductViewed(entity);
64
+ }
65
+ break;
66
+ }
67
+ }
68
+ }
69
+ trackPostViewed(info) {
70
+ let postId = info.id;
71
+ if (postId === void 0 && info.url !== void 0) {
72
+ const parsedUrl = new URL(info.url);
73
+ const pathSegments = parsedUrl.pathname.split("/").filter((segment) => segment.length > 0);
74
+ if (pathSegments.length > 0) {
75
+ postId = pathSegments[pathSegments.length - 1];
76
+ }
77
+ }
78
+ if (postId === void 0 || info.title === void 0) {
79
+ return;
80
+ }
81
+ this.tracker.track("postViewed", {
82
+ post: AutoTrackingPlugin.clean({
83
+ postId: AutoTrackingPlugin.truncate(postId, 200),
84
+ title: AutoTrackingPlugin.truncate(info.title, 200),
85
+ url: info.url,
86
+ tags: info.tags?.map((tag) => AutoTrackingPlugin.truncate(tag, 50)),
87
+ categories: info.categories?.map((category) => AutoTrackingPlugin.truncate(category, 50)),
88
+ authors: info.authors?.map((author) => AutoTrackingPlugin.truncate(author, 100)),
89
+ publishTime: info.publishTime ?? Date.now(),
90
+ updateTime: info.updateTime
91
+ })
92
+ });
93
+ }
94
+ trackProductViewed(info) {
95
+ if (info.id === void 0 || info.name === void 0 || info.displayPrice === void 0) {
96
+ return;
97
+ }
98
+ this.tracker.track("productViewed", {
99
+ product: AutoTrackingPlugin.clean({
100
+ productId: AutoTrackingPlugin.truncate(info.id, 50),
101
+ name: AutoTrackingPlugin.truncate(info.name, 200),
102
+ displayPrice: info.displayPrice,
103
+ url: info.url,
104
+ sku: info.sku !== void 0 ? AutoTrackingPlugin.truncate(info.sku, 50) : void 0,
105
+ brand: info.brand !== void 0 ? AutoTrackingPlugin.truncate(info.brand, 100) : void 0,
106
+ variant: info.variant !== void 0 ? AutoTrackingPlugin.truncate(info.variant, 50) : void 0,
107
+ category: info.category !== void 0 ? AutoTrackingPlugin.truncate(info.category, 100) : void 0,
108
+ originalPrice: info.originalPrice,
109
+ imageUrl: info.imageUrl
110
+ })
111
+ });
112
+ }
113
+ trackLinkOpened(event) {
114
+ if (event.target instanceof HTMLElement) {
115
+ const link = event.target.closest("a");
116
+ if (link?.href !== void 0 && URL.canParse(link.href, document.baseURI)) {
117
+ this.tracker.track("linkOpened", {
118
+ link: new URL(link.href, document.baseURI).toString()
119
+ });
120
+ }
121
+ }
122
+ }
123
+ static truncate(value, maxLength) {
124
+ if (value.length <= maxLength) {
125
+ return value;
126
+ }
127
+ return value.slice(0, maxLength);
128
+ }
129
+ static clean(obj) {
130
+ const result = { ...obj };
131
+ for (const key of Object.keys(result)) {
132
+ if (result[key] === void 0) {
133
+ delete result[key];
134
+ }
135
+ }
136
+ return result;
137
+ }
138
+ }
139
+ const factory = (props) => new AutoTrackingPlugin({
140
+ tab: props.sdk.tab,
141
+ tracker: props.sdk.tracker,
142
+ options: props.options
143
+ });
144
+ // Annotate the CommonJS export names for ESM import in node:
145
+ 0 && (module.exports = {
146
+ AutoTrackingPlugin,
147
+ factory
148
+ });
@@ -0,0 +1,50 @@
1
+ import { a as Plugin, b as PluginArguments } from '../../plug-Cf6SnDp4.cjs';
2
+ import { TrackerFacade } from '@croct/sdk/facade/trackerFacade';
3
+ import { Tab } from '@croct/sdk/tab';
4
+ import '@croct/sdk/facade/sessionFacade';
5
+ import '@croct/sdk/facade/userFacade';
6
+ import '@croct/sdk/facade/evaluatorFacade';
7
+ import '@croct/sdk/facade/sdkFacade';
8
+ import '@croct/sdk/utilityTypes';
9
+ import '@croct/sdk/trackingEvents';
10
+ import '@croct/sdk/facade/contentFetcherFacade';
11
+ import '@croct/sdk/token';
12
+ import '../../sdk/index.cjs';
13
+ import '@croct/sdk/eventManager';
14
+ import '@croct/sdk/sdkEvents';
15
+ import '@croct/sdk/logging';
16
+ import '@croct/sdk/cid';
17
+ import '../../slot.cjs';
18
+ import '@croct/json';
19
+ import '../../versioning.cjs';
20
+ import '../../component.cjs';
21
+ import '@croct/json/mutable';
22
+
23
+ type Configuration = {
24
+ tab: Tab;
25
+ tracker: TrackerFacade;
26
+ options?: Options;
27
+ };
28
+ type Options = {
29
+ disablePostViewed?: boolean;
30
+ disableProductViewed?: boolean;
31
+ disableLinkOpened?: boolean;
32
+ };
33
+ declare class AutoTrackingPlugin implements Plugin {
34
+ private readonly tab;
35
+ private readonly tracker;
36
+ private readonly options?;
37
+ constructor(configuration: Configuration);
38
+ private isDisabled;
39
+ enable(): void;
40
+ disable(): void;
41
+ private trackStructuredData;
42
+ private trackPostViewed;
43
+ private trackProductViewed;
44
+ private trackLinkOpened;
45
+ private static truncate;
46
+ private static clean;
47
+ }
48
+ declare const factory: (props: PluginArguments<Options>) => AutoTrackingPlugin;
49
+
50
+ export { AutoTrackingPlugin, type Configuration, type Options, factory };
@@ -0,0 +1,50 @@
1
+ import { a as Plugin, b as PluginArguments } from '../../plug-DjUeWnq0.js';
2
+ import { TrackerFacade } from '@croct/sdk/facade/trackerFacade';
3
+ import { Tab } from '@croct/sdk/tab';
4
+ import '@croct/sdk/facade/sessionFacade';
5
+ import '@croct/sdk/facade/userFacade';
6
+ import '@croct/sdk/facade/evaluatorFacade';
7
+ import '@croct/sdk/facade/sdkFacade';
8
+ import '@croct/sdk/utilityTypes';
9
+ import '@croct/sdk/trackingEvents';
10
+ import '@croct/sdk/facade/contentFetcherFacade';
11
+ import '@croct/sdk/token';
12
+ import '../../sdk/index.js';
13
+ import '@croct/sdk/eventManager';
14
+ import '@croct/sdk/sdkEvents';
15
+ import '@croct/sdk/logging';
16
+ import '@croct/sdk/cid';
17
+ import '../../slot.js';
18
+ import '@croct/json';
19
+ import '../../versioning.js';
20
+ import '../../component.js';
21
+ import '@croct/json/mutable';
22
+
23
+ type Configuration = {
24
+ tab: Tab;
25
+ tracker: TrackerFacade;
26
+ options?: Options;
27
+ };
28
+ type Options = {
29
+ disablePostViewed?: boolean;
30
+ disableProductViewed?: boolean;
31
+ disableLinkOpened?: boolean;
32
+ };
33
+ declare class AutoTrackingPlugin implements Plugin {
34
+ private readonly tab;
35
+ private readonly tracker;
36
+ private readonly options?;
37
+ constructor(configuration: Configuration);
38
+ private isDisabled;
39
+ enable(): void;
40
+ disable(): void;
41
+ private trackStructuredData;
42
+ private trackPostViewed;
43
+ private trackProductViewed;
44
+ private trackLinkOpened;
45
+ private static truncate;
46
+ private static clean;
47
+ }
48
+ declare const factory: (props: PluginArguments<Options>) => AutoTrackingPlugin;
49
+
50
+ export { AutoTrackingPlugin, type Configuration, type Options, factory };
@@ -0,0 +1,124 @@
1
+ import { parseEntity } from "./structuredData.js";
2
+ class AutoTrackingPlugin {
3
+ constructor(configuration) {
4
+ this.tab = configuration.tab;
5
+ this.tracker = configuration.tracker;
6
+ this.options = configuration.options;
7
+ this.trackStructuredData = this.trackStructuredData.bind(this);
8
+ this.trackLinkOpened = this.trackLinkOpened.bind(this);
9
+ }
10
+ isDisabled() {
11
+ return this.options?.disablePostViewed === true && this.options?.disableProductViewed === true && this.options?.disableLinkOpened === true;
12
+ }
13
+ enable() {
14
+ if (this.isDisabled()) {
15
+ return;
16
+ }
17
+ this.trackStructuredData();
18
+ this.tab.addListener("urlChange", this.trackStructuredData);
19
+ if (this.options?.disableLinkOpened !== true) {
20
+ document.addEventListener("click", this.trackLinkOpened, true);
21
+ }
22
+ }
23
+ disable() {
24
+ this.tab.removeListener("urlChange", this.trackStructuredData);
25
+ document.removeEventListener("click", this.trackLinkOpened, true);
26
+ }
27
+ trackStructuredData() {
28
+ const structuredDataElements = document.querySelectorAll('script[type="application/ld+json"]');
29
+ for (const element of structuredDataElements) {
30
+ const entity = parseEntity(element.textContent ?? "");
31
+ switch (entity?.type) {
32
+ case "post":
33
+ if (this.options?.disablePostViewed !== true) {
34
+ this.trackPostViewed(entity);
35
+ }
36
+ break;
37
+ case "product":
38
+ case "service":
39
+ if (this.options?.disableProductViewed !== true) {
40
+ this.trackProductViewed(entity);
41
+ }
42
+ break;
43
+ }
44
+ }
45
+ }
46
+ trackPostViewed(info) {
47
+ let postId = info.id;
48
+ if (postId === void 0 && info.url !== void 0) {
49
+ const parsedUrl = new URL(info.url);
50
+ const pathSegments = parsedUrl.pathname.split("/").filter((segment) => segment.length > 0);
51
+ if (pathSegments.length > 0) {
52
+ postId = pathSegments[pathSegments.length - 1];
53
+ }
54
+ }
55
+ if (postId === void 0 || info.title === void 0) {
56
+ return;
57
+ }
58
+ this.tracker.track("postViewed", {
59
+ post: AutoTrackingPlugin.clean({
60
+ postId: AutoTrackingPlugin.truncate(postId, 200),
61
+ title: AutoTrackingPlugin.truncate(info.title, 200),
62
+ url: info.url,
63
+ tags: info.tags?.map((tag) => AutoTrackingPlugin.truncate(tag, 50)),
64
+ categories: info.categories?.map((category) => AutoTrackingPlugin.truncate(category, 50)),
65
+ authors: info.authors?.map((author) => AutoTrackingPlugin.truncate(author, 100)),
66
+ publishTime: info.publishTime ?? Date.now(),
67
+ updateTime: info.updateTime
68
+ })
69
+ });
70
+ }
71
+ trackProductViewed(info) {
72
+ if (info.id === void 0 || info.name === void 0 || info.displayPrice === void 0) {
73
+ return;
74
+ }
75
+ this.tracker.track("productViewed", {
76
+ product: AutoTrackingPlugin.clean({
77
+ productId: AutoTrackingPlugin.truncate(info.id, 50),
78
+ name: AutoTrackingPlugin.truncate(info.name, 200),
79
+ displayPrice: info.displayPrice,
80
+ url: info.url,
81
+ sku: info.sku !== void 0 ? AutoTrackingPlugin.truncate(info.sku, 50) : void 0,
82
+ brand: info.brand !== void 0 ? AutoTrackingPlugin.truncate(info.brand, 100) : void 0,
83
+ variant: info.variant !== void 0 ? AutoTrackingPlugin.truncate(info.variant, 50) : void 0,
84
+ category: info.category !== void 0 ? AutoTrackingPlugin.truncate(info.category, 100) : void 0,
85
+ originalPrice: info.originalPrice,
86
+ imageUrl: info.imageUrl
87
+ })
88
+ });
89
+ }
90
+ trackLinkOpened(event) {
91
+ if (event.target instanceof HTMLElement) {
92
+ const link = event.target.closest("a");
93
+ if (link?.href !== void 0 && URL.canParse(link.href, document.baseURI)) {
94
+ this.tracker.track("linkOpened", {
95
+ link: new URL(link.href, document.baseURI).toString()
96
+ });
97
+ }
98
+ }
99
+ }
100
+ static truncate(value, maxLength) {
101
+ if (value.length <= maxLength) {
102
+ return value;
103
+ }
104
+ return value.slice(0, maxLength);
105
+ }
106
+ static clean(obj) {
107
+ const result = { ...obj };
108
+ for (const key of Object.keys(result)) {
109
+ if (result[key] === void 0) {
110
+ delete result[key];
111
+ }
112
+ }
113
+ return result;
114
+ }
115
+ }
116
+ const factory = (props) => new AutoTrackingPlugin({
117
+ tab: props.sdk.tab,
118
+ tracker: props.sdk.tracker,
119
+ options: props.options
120
+ });
121
+ export {
122
+ AutoTrackingPlugin,
123
+ factory
124
+ };