musora-content-services 1.5.0 → 1.6.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,64 @@
1
+ /**
2
+ * @module Search-Engine-Services
3
+ */
4
+
5
+ import { globalConfig } from './config'
6
+ import { algoliasearch } from 'algoliasearch'
7
+
8
+ /**
9
+ * Sends an event through the search engine API (as of now, algolia)
10
+ *
11
+ * @param {string} index - The index to which the event will be sent.
12
+ * @param {string} queryId - The unique identifier for the search query.
13
+ * @param {string[]} objectIDs - An array of object IDs that were clicked.
14
+ * @param {number[]} positions - An array of positions corresponding to the object IDs clicked.
15
+ *
16
+ * @returns {Promise<void>} - A promise that resolves when the event is sent.
17
+ *
18
+ * @example
19
+ * ```
20
+ * sendAlgoliaClickEvent(
21
+ * 'production_sanity_all',
22
+ * '43b15df305339e827f0ac0bdc5ebcaa7',
23
+ * ['9780545139700', '9780439784542'],
24
+ * [7, 6]
25
+ * );
26
+ * ```
27
+ */
28
+ export async function sendAlgoliaClickEvent(index, queryId, objectIDs, positions) {
29
+ if (!queryId || !objectIDs || !positions) {
30
+ throw new Error('queryId, objectIDs, and positions are required parameters.')
31
+ }
32
+
33
+ if (objectIDs.length !== positions.length) {
34
+ throw new Error('objectIDs and positions must have the same length.')
35
+ }
36
+
37
+ const searchEngine = algoliasearch(
38
+ globalConfig.searchEngineConfig.applicationId,
39
+ globalConfig.searchEngineConfig.apiKey
40
+ ).initInsights({
41
+ region: 'us',
42
+ })
43
+
44
+ try {
45
+ await searchEngine.pushEvents({
46
+ events: [
47
+ {
48
+ eventType: 'click',
49
+ eventName: 'Content Clicked',
50
+ index: index,
51
+ userToken: globalConfig.railcontentConfig.userId.toString(),
52
+ authenticatedUserToken: globalConfig.railcontentConfig.authToken,
53
+ timestamp: new Date().getTime(),
54
+ objectIDs: objectIDs,
55
+ queryID: queryId,
56
+ positions: positions,
57
+ },
58
+ ],
59
+ })
60
+ } catch (error) {
61
+ // Don't throw as it is not a blocker
62
+ console.error('Error sending search event:', error)
63
+ }
64
+ }
@@ -8,6 +8,7 @@ export let globalConfig = {
8
8
  sanityConfig: {},
9
9
  railcontentConfig: {},
10
10
  recommendationsConfig: {},
11
+ searchEngineConfig: {},
11
12
  localStorage: null,
12
13
  isMA: false,
13
14
  localTimezoneString: null, // In format: America/Vancouver
@@ -40,6 +41,8 @@ const excludeFromGeneratedIndex = []
40
41
  * @param {string} config.railcontentConfig.authToken - The bearer authorization token.
41
42
  * @param {string} config.recommendationsConfig.token - The token for authenticating recommendation requests.
42
43
  * @param {string} config.recommendationsConfig.baseUrl - The url for the recommendation server.
44
+ * @param {string} config.searchEngineConfig.applicationId - The application ID for the search engine (e.g., Algolia).
45
+ * @param {string} config.searchEngineConfig.apiKey - The API key for the search engine.
43
46
  * @param {Object} config.localStorage - Cache to use for localStorage
44
47
  * @param {boolean} config.isMA - Variable that tells if the library is used by MA or FEW
45
48
  * @param {string} config.localTimezoneString - The local timezone string in format: America/Vancouver
@@ -60,7 +63,7 @@ const excludeFromGeneratedIndex = []
60
63
  * token: 'your-user-api-token',
61
64
  * userId: 'current-user-id',
62
65
  * baseUrl: 'https://web-staging-one.musora.com',
63
- * authToken 'your-auth-token',
66
+ * authToken: 'your-auth-token',
64
67
  * },
65
68
  * recommendationsConfig: {
66
69
  * token: 'your-user-api-token',
@@ -77,6 +80,7 @@ export function initializeService(config) {
77
80
  globalConfig.isMA = config.isMA || false
78
81
  globalConfig.localTimezoneString = config.localTimezoneString || null
79
82
  globalConfig.recommendationsConfig = config.recommendationsConfig
83
+ globalConfig.searchEngineConfig = config.searchEngineConfig
80
84
  }
81
85
 
82
86
  export function setUserMetadata(userMetaData) {