@codecademy/tracking 1.2.1 → 1.3.0-alpha.a12b70af2e.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.
- package/dist/events/track.d.ts +2 -1
- package/dist/events/track.js +33 -0
- package/dist/events/types.d.ts +31 -0
- package/dist/events/types.js +5 -1
- package/package.json +1 -1
package/dist/events/track.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BaseEventData, EventDataTypes, TrackingOptions, UserClickData, UserImpressionData, UserVisitData } from './types';
|
|
1
|
+
import type { BaseEventData, ContentContext, EventDataTypes, TrackingOptions, UserClickData, UserImpressionData, UserVisitData } from './types';
|
|
2
2
|
export type TrackerOptions = {
|
|
3
3
|
apiBaseUrl: string;
|
|
4
4
|
verbose?: boolean;
|
|
@@ -9,4 +9,5 @@ export declare const createTracker: ({ apiBaseUrl, verbose }: TrackerOptions) =>
|
|
|
9
9
|
impression: (data: UserImpressionData) => void;
|
|
10
10
|
visit: (data: UserVisitData) => void;
|
|
11
11
|
pushDataLayerEvent: (eventName: string) => void;
|
|
12
|
+
pushContentLoadedEvent: (contentIds: ContentContext) => Promise<void>;
|
|
12
13
|
};
|
package/dist/events/track.js
CHANGED
|
@@ -11,6 +11,28 @@ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" !=
|
|
|
11
11
|
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
12
12
|
import { getClientType } from '../integrations/device';
|
|
13
13
|
const browserSupportsKeepalive = () => 'keepalive' in window.Request.prototype;
|
|
14
|
+
|
|
15
|
+
/** Generate a SHA-256 hash of a string using the Web Crypto API. */
|
|
16
|
+
const hashStringSHA256 = async str => {
|
|
17
|
+
if (typeof window !== 'undefined' && window.crypto && window.crypto.subtle) {
|
|
18
|
+
const encoder = new TextEncoder();
|
|
19
|
+
const data = encoder.encode(str);
|
|
20
|
+
const hashBuffer = await window.crypto.subtle.digest('SHA-256', data);
|
|
21
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
22
|
+
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
|
23
|
+
}
|
|
24
|
+
throw new Error('Web Crypto API not available');
|
|
25
|
+
};
|
|
26
|
+
const validKeys = ['assessment_id', 'content_item_id', 'exercise_id', 'learning_standard_id', 'module_id', 'path_id', 'review_card_id', 'track_id'];
|
|
27
|
+
const getHashedContentId = async contentContext => {
|
|
28
|
+
if (!contentContext) return;
|
|
29
|
+
const contentIdsAsString = validKeys.reduce((sum, currentKey) => {
|
|
30
|
+
const id = contentContext[currentKey];
|
|
31
|
+
if (id) sum += id;
|
|
32
|
+
return sum;
|
|
33
|
+
}, '');
|
|
34
|
+
return hashStringSHA256(contentIdsAsString);
|
|
35
|
+
};
|
|
14
36
|
export const createTracker = _ref => {
|
|
15
37
|
let apiBaseUrl = _ref.apiBaseUrl,
|
|
16
38
|
verbose = _ref.verbose;
|
|
@@ -114,6 +136,17 @@ export const createTracker = _ref => {
|
|
|
114
136
|
((_ref6 = window).dataLayer || (_ref6.dataLayer = [])).push({
|
|
115
137
|
event: eventName
|
|
116
138
|
});
|
|
139
|
+
},
|
|
140
|
+
pushContentLoadedEvent: async contentIds => {
|
|
141
|
+
if (!contentIds) return;
|
|
142
|
+
const hashedId = await getHashedContentId(contentIds);
|
|
143
|
+
if (hashedId) {
|
|
144
|
+
var _ref7;
|
|
145
|
+
((_ref7 = window).dataLayer || (_ref7.dataLayer = [])).push({
|
|
146
|
+
event: 'content_id_loaded',
|
|
147
|
+
content_id: hashedId
|
|
148
|
+
});
|
|
149
|
+
}
|
|
117
150
|
}
|
|
118
151
|
};
|
|
119
152
|
};
|
package/dist/events/types.d.ts
CHANGED
|
@@ -262,3 +262,34 @@ export type ChatGPTPluginSearchEventData = {
|
|
|
262
262
|
resource: ChatGPTSearchResource;
|
|
263
263
|
query: string;
|
|
264
264
|
};
|
|
265
|
+
/**
|
|
266
|
+
* Content context for tracking events
|
|
267
|
+
*/
|
|
268
|
+
export type ContentContext = Partial<JourneyComponents & PathComponents & CourseComponents & ContentItem & Exercise & {
|
|
269
|
+
content_item_id: string;
|
|
270
|
+
exercise_id: string;
|
|
271
|
+
} & {
|
|
272
|
+
enterprise_module_id: string;
|
|
273
|
+
}>;
|
|
274
|
+
export interface JourneyComponents {
|
|
275
|
+
journey_id?: string;
|
|
276
|
+
module_id?: string;
|
|
277
|
+
path_id?: string;
|
|
278
|
+
track_id?: string;
|
|
279
|
+
}
|
|
280
|
+
export interface PathComponents {
|
|
281
|
+
module_id?: string;
|
|
282
|
+
path_id?: string;
|
|
283
|
+
track_id?: string;
|
|
284
|
+
}
|
|
285
|
+
export interface CourseComponents {
|
|
286
|
+
module_id?: string;
|
|
287
|
+
track_id?: string;
|
|
288
|
+
}
|
|
289
|
+
export interface ContentItem {
|
|
290
|
+
content_item_id?: string;
|
|
291
|
+
type?: string;
|
|
292
|
+
}
|
|
293
|
+
export interface Exercise {
|
|
294
|
+
exercise_id?: string;
|
|
295
|
+
}
|
package/dist/events/types.js
CHANGED
|
@@ -46,4 +46,8 @@ export let ChatGPTSearchResource = /*#__PURE__*/function (ChatGPTSearchResource)
|
|
|
46
46
|
return ChatGPTSearchResource;
|
|
47
47
|
}({});
|
|
48
48
|
|
|
49
|
-
// does not extend base event (all client side fields there)
|
|
49
|
+
// does not extend base event (all client side fields there)
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Content context for tracking events
|
|
53
|
+
*/
|
package/package.json
CHANGED