@splitsoftware/splitio-commons 1.17.1-rc.3 → 2.0.0-rc.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 (216) hide show
  1. package/CHANGES.txt +7 -3
  2. package/cjs/evaluator/index.js +2 -2
  3. package/cjs/evaluator/matchers/semver_inlist.js +1 -2
  4. package/cjs/evaluator/matchers/whitelist.js +1 -2
  5. package/cjs/listeners/browser.js +1 -2
  6. package/cjs/logger/browser/DebugLogger.js +1 -2
  7. package/cjs/logger/browser/ErrorLogger.js +1 -2
  8. package/cjs/logger/browser/InfoLogger.js +1 -2
  9. package/cjs/logger/browser/WarnLogger.js +1 -2
  10. package/cjs/logger/index.js +1 -2
  11. package/cjs/readiness/readinessManager.js +5 -7
  12. package/cjs/sdkClient/clientCS.js +5 -8
  13. package/cjs/sdkClient/sdkClientMethodCS.js +5 -8
  14. package/cjs/sdkFactory/index.js +10 -33
  15. package/cjs/services/decorateHeaders.js +1 -2
  16. package/cjs/storages/AbstractSplitsCacheAsync.js +7 -0
  17. package/cjs/storages/AbstractSplitsCacheSync.js +7 -0
  18. package/cjs/storages/KeyBuilderCS.js +0 -9
  19. package/cjs/storages/dataLoader.js +32 -64
  20. package/cjs/storages/inLocalStorage/MySegmentsCacheInLocal.js +1 -21
  21. package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +14 -7
  22. package/cjs/storages/inLocalStorage/index.js +1 -6
  23. package/cjs/storages/inMemory/InMemoryStorageCS.js +4 -16
  24. package/cjs/storages/inMemory/SegmentsCacheInMemory.js +3 -4
  25. package/cjs/storages/inMemory/SplitsCacheInMemory.js +2 -3
  26. package/cjs/storages/inMemory/UniqueKeysCacheInMemory.js +2 -3
  27. package/cjs/storages/inMemory/UniqueKeysCacheInMemoryCS.js +2 -3
  28. package/cjs/storages/inRedis/RedisAdapter.js +3 -4
  29. package/cjs/storages/inRedis/SplitsCacheInRedis.js +1 -1
  30. package/cjs/storages/inRedis/TelemetryCacheInRedis.js +3 -4
  31. package/cjs/storages/inRedis/UniqueKeysCacheInRedis.js +1 -2
  32. package/cjs/storages/pluggable/SplitsCachePluggable.js +1 -1
  33. package/cjs/storages/pluggable/TelemetryCachePluggable.js +6 -7
  34. package/cjs/storages/pluggable/UniqueKeysCachePluggable.js +1 -2
  35. package/cjs/storages/pluggable/inMemoryWrapper.js +7 -8
  36. package/cjs/storages/pluggable/index.js +32 -37
  37. package/cjs/sync/offline/syncTasks/fromObjectSyncTask.js +7 -2
  38. package/cjs/sync/polling/pollingManagerSS.js +3 -3
  39. package/cjs/sync/polling/updaters/splitChangesUpdater.js +13 -5
  40. package/cjs/sync/streaming/parseUtils.js +0 -1
  41. package/cjs/sync/streaming/pushManager.js +2 -3
  42. package/cjs/trackers/eventTracker.js +9 -11
  43. package/cjs/trackers/impressionsTracker.js +13 -15
  44. package/cjs/trackers/uniqueKeysTracker.js +3 -5
  45. package/cjs/utils/LRUCache/index.js +1 -2
  46. package/cjs/utils/constants/browser.js +1 -4
  47. package/cjs/utils/lang/index.js +6 -9
  48. package/cjs/utils/lang/objectAssign.js +12 -77
  49. package/cjs/utils/lang/sets.js +3 -110
  50. package/cjs/utils/settingsValidation/index.js +0 -9
  51. package/cjs/utils/settingsValidation/logger/builtinLogger.js +1 -2
  52. package/cjs/utils/settingsValidation/storage/storageCS.js +12 -1
  53. package/esm/evaluator/index.js +3 -3
  54. package/esm/evaluator/matchers/semver_inlist.js +1 -2
  55. package/esm/evaluator/matchers/whitelist.js +1 -2
  56. package/esm/listeners/browser.js +1 -2
  57. package/esm/logger/browser/DebugLogger.js +1 -2
  58. package/esm/logger/browser/ErrorLogger.js +1 -2
  59. package/esm/logger/browser/InfoLogger.js +1 -2
  60. package/esm/logger/browser/WarnLogger.js +1 -2
  61. package/esm/logger/index.js +1 -2
  62. package/esm/readiness/readinessManager.js +5 -7
  63. package/esm/sdkClient/clientCS.js +5 -8
  64. package/esm/sdkClient/sdkClientMethodCS.js +5 -8
  65. package/esm/sdkFactory/index.js +11 -34
  66. package/esm/services/decorateHeaders.js +1 -2
  67. package/esm/storages/AbstractSplitsCacheAsync.js +7 -0
  68. package/esm/storages/AbstractSplitsCacheSync.js +7 -0
  69. package/esm/storages/KeyBuilderCS.js +0 -9
  70. package/esm/storages/dataLoader.js +30 -61
  71. package/esm/storages/inLocalStorage/MySegmentsCacheInLocal.js +1 -21
  72. package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +14 -7
  73. package/esm/storages/inLocalStorage/index.js +2 -7
  74. package/esm/storages/inMemory/InMemoryStorageCS.js +4 -16
  75. package/esm/storages/inMemory/SegmentsCacheInMemory.js +3 -4
  76. package/esm/storages/inMemory/SplitsCacheInMemory.js +2 -3
  77. package/esm/storages/inMemory/UniqueKeysCacheInMemory.js +2 -3
  78. package/esm/storages/inMemory/UniqueKeysCacheInMemoryCS.js +2 -3
  79. package/esm/storages/inRedis/RedisAdapter.js +3 -4
  80. package/esm/storages/inRedis/SplitsCacheInRedis.js +2 -2
  81. package/esm/storages/inRedis/TelemetryCacheInRedis.js +3 -4
  82. package/esm/storages/inRedis/UniqueKeysCacheInRedis.js +1 -2
  83. package/esm/storages/pluggable/SplitsCachePluggable.js +2 -2
  84. package/esm/storages/pluggable/TelemetryCachePluggable.js +6 -7
  85. package/esm/storages/pluggable/UniqueKeysCachePluggable.js +1 -2
  86. package/esm/storages/pluggable/inMemoryWrapper.js +7 -8
  87. package/esm/storages/pluggable/index.js +32 -37
  88. package/esm/sync/offline/syncTasks/fromObjectSyncTask.js +8 -3
  89. package/esm/sync/polling/pollingManagerSS.js +3 -3
  90. package/esm/sync/polling/updaters/splitChangesUpdater.js +14 -6
  91. package/esm/sync/streaming/parseUtils.js +0 -1
  92. package/esm/sync/streaming/pushManager.js +2 -3
  93. package/esm/trackers/eventTracker.js +9 -11
  94. package/esm/trackers/impressionsTracker.js +13 -15
  95. package/esm/trackers/uniqueKeysTracker.js +3 -5
  96. package/esm/utils/LRUCache/index.js +1 -2
  97. package/esm/utils/constants/browser.js +0 -3
  98. package/esm/utils/lang/index.js +6 -9
  99. package/esm/utils/lang/objectAssign.js +12 -77
  100. package/esm/utils/lang/sets.js +2 -107
  101. package/esm/utils/settingsValidation/index.js +0 -9
  102. package/esm/utils/settingsValidation/logger/builtinLogger.js +1 -2
  103. package/esm/utils/settingsValidation/storage/storageCS.js +10 -0
  104. package/package.json +2 -2
  105. package/src/evaluator/index.ts +5 -5
  106. package/src/evaluator/matchers/semver_inlist.ts +1 -2
  107. package/src/evaluator/matchers/whitelist.ts +1 -3
  108. package/src/listeners/browser.ts +1 -2
  109. package/src/logger/browser/DebugLogger.ts +1 -2
  110. package/src/logger/browser/ErrorLogger.ts +1 -2
  111. package/src/logger/browser/InfoLogger.ts +1 -2
  112. package/src/logger/browser/WarnLogger.ts +1 -2
  113. package/src/logger/index.ts +3 -4
  114. package/src/readiness/readinessManager.ts +7 -9
  115. package/src/readiness/types.ts +0 -1
  116. package/src/sdkClient/clientCS.ts +5 -8
  117. package/src/sdkClient/sdkClientMethodCS.ts +3 -6
  118. package/src/sdkFactory/index.ts +12 -37
  119. package/src/sdkFactory/types.ts +1 -4
  120. package/src/services/decorateHeaders.ts +1 -2
  121. package/src/storages/AbstractSplitsCacheAsync.ts +9 -2
  122. package/src/storages/AbstractSplitsCacheSync.ts +9 -2
  123. package/src/storages/KeyBuilderCS.ts +0 -13
  124. package/src/storages/dataLoader.ts +32 -62
  125. package/src/storages/inLocalStorage/MySegmentsCacheInLocal.ts +1 -21
  126. package/src/storages/inLocalStorage/SplitsCacheInLocal.ts +16 -8
  127. package/src/storages/inLocalStorage/index.ts +2 -8
  128. package/src/storages/inMemory/InMemoryStorageCS.ts +4 -19
  129. package/src/storages/inMemory/SegmentsCacheInMemory.ts +4 -5
  130. package/src/storages/inMemory/SplitsCacheInMemory.ts +4 -5
  131. package/src/storages/inMemory/UniqueKeysCacheInMemory.ts +4 -5
  132. package/src/storages/inMemory/UniqueKeysCacheInMemoryCS.ts +4 -5
  133. package/src/storages/inRedis/RedisAdapter.ts +4 -5
  134. package/src/storages/inRedis/SplitsCacheInRedis.ts +3 -3
  135. package/src/storages/inRedis/TelemetryCacheInRedis.ts +3 -4
  136. package/src/storages/inRedis/UniqueKeysCacheInRedis.ts +1 -2
  137. package/src/storages/pluggable/SegmentsCachePluggable.ts +0 -1
  138. package/src/storages/pluggable/SplitsCachePluggable.ts +3 -3
  139. package/src/storages/pluggable/TelemetryCachePluggable.ts +6 -7
  140. package/src/storages/pluggable/UniqueKeysCachePluggable.ts +1 -2
  141. package/src/storages/pluggable/inMemoryWrapper.ts +8 -9
  142. package/src/storages/pluggable/index.ts +33 -38
  143. package/src/storages/types.ts +9 -6
  144. package/src/sync/offline/syncTasks/fromObjectSyncTask.ts +7 -3
  145. package/src/sync/polling/pollingManagerSS.ts +2 -3
  146. package/src/sync/polling/updaters/splitChangesUpdater.ts +15 -8
  147. package/src/sync/streaming/parseUtils.ts +0 -1
  148. package/src/sync/streaming/pushManager.ts +3 -4
  149. package/src/sync/submitters/types.ts +3 -4
  150. package/src/trackers/eventTracker.ts +7 -10
  151. package/src/trackers/impressionsTracker.ts +9 -12
  152. package/src/trackers/types.ts +0 -1
  153. package/src/trackers/uniqueKeysTracker.ts +4 -6
  154. package/src/types.ts +9 -18
  155. package/src/utils/LRUCache/index.ts +2 -3
  156. package/src/utils/constants/browser.ts +0 -4
  157. package/src/utils/lang/index.ts +6 -7
  158. package/src/utils/lang/objectAssign.ts +13 -92
  159. package/src/utils/lang/sets.ts +3 -125
  160. package/src/utils/settingsValidation/index.ts +0 -10
  161. package/src/utils/settingsValidation/logger/builtinLogger.ts +1 -2
  162. package/src/utils/settingsValidation/storage/storageCS.ts +13 -0
  163. package/src/utils/settingsValidation/types.ts +0 -2
  164. package/types/logger/index.d.ts +1 -2
  165. package/types/readiness/types.d.ts +0 -1
  166. package/types/sdkClient/clientCS.d.ts +2 -3
  167. package/types/sdkFactory/types.d.ts +1 -3
  168. package/types/storages/AbstractSplitsCacheAsync.d.ts +6 -2
  169. package/types/storages/AbstractSplitsCacheSync.d.ts +6 -2
  170. package/types/storages/KeyBuilderCS.d.ts +0 -2
  171. package/types/storages/dataLoader.d.ts +6 -17
  172. package/types/storages/inLocalStorage/SplitsCacheInLocal.d.ts +7 -2
  173. package/types/storages/inMemory/SplitsCacheInMemory.d.ts +1 -2
  174. package/types/storages/inMemory/UniqueKeysCacheInMemory.d.ts +2 -3
  175. package/types/storages/inRedis/SplitsCacheInRedis.d.ts +1 -2
  176. package/types/storages/pluggable/SplitsCachePluggable.d.ts +1 -2
  177. package/types/storages/pluggable/inMemoryWrapper.d.ts +1 -2
  178. package/types/storages/types.d.ts +7 -6
  179. package/types/sync/polling/updaters/splitChangesUpdater.d.ts +1 -2
  180. package/types/sync/submitters/types.d.ts +3 -4
  181. package/types/trackers/eventTracker.d.ts +1 -1
  182. package/types/trackers/impressionsTracker.d.ts +1 -1
  183. package/types/trackers/types.d.ts +0 -1
  184. package/types/types.d.ts +9 -17
  185. package/types/utils/LRUCache/index.d.ts +1 -2
  186. package/types/utils/constants/browser.d.ts +0 -2
  187. package/types/utils/lang/objectAssign.d.ts +3 -0
  188. package/types/utils/lang/sets.d.ts +1 -61
  189. package/types/utils/settingsValidation/index.d.ts +0 -1
  190. package/types/utils/settingsValidation/storage/storageCS.d.ts +5 -0
  191. package/types/utils/settingsValidation/types.d.ts +0 -2
  192. package/cjs/integrations/browser.js +0 -31
  193. package/cjs/integrations/ga/GaToSplit.js +0 -257
  194. package/cjs/integrations/ga/GoogleAnalyticsToSplit.js +0 -14
  195. package/cjs/integrations/ga/SplitToGa.js +0 -123
  196. package/cjs/integrations/ga/SplitToGoogleAnalytics.js +0 -14
  197. package/cjs/integrations/ga/types.js +0 -2
  198. package/cjs/sdkClient/sdkClientMethodCSWithTT.js +0 -79
  199. package/cjs/utils/lang/maps.js +0 -96
  200. package/esm/integrations/browser.js +0 -27
  201. package/esm/integrations/ga/GaToSplit.js +0 -250
  202. package/esm/integrations/ga/GoogleAnalyticsToSplit.js +0 -10
  203. package/esm/integrations/ga/SplitToGa.js +0 -120
  204. package/esm/integrations/ga/SplitToGoogleAnalytics.js +0 -10
  205. package/esm/integrations/ga/types.js +0 -1
  206. package/esm/sdkClient/sdkClientMethodCSWithTT.js +0 -75
  207. package/esm/utils/lang/maps.js +0 -92
  208. package/src/integrations/browser.ts +0 -35
  209. package/src/integrations/ga/GaToSplit.ts +0 -299
  210. package/src/integrations/ga/GoogleAnalyticsToSplit.ts +0 -14
  211. package/src/integrations/ga/SplitToGa.ts +0 -135
  212. package/src/integrations/ga/SplitToGoogleAnalytics.ts +0 -14
  213. package/src/integrations/ga/autoRequire.js +0 -33
  214. package/src/integrations/ga/types.ts +0 -153
  215. package/src/sdkClient/sdkClientMethodCSWithTT.ts +0 -101
  216. package/src/utils/lang/maps.ts +0 -108
@@ -1,299 +0,0 @@
1
- import { objectAssign } from '../../utils/lang/objectAssign';
2
- import { isString, isFiniteNumber, uniqAsStrings } from '../../utils/lang';
3
- import {
4
- validateEvent,
5
- validateEventValue,
6
- validateEventProperties,
7
- validateKey,
8
- validateTrafficType
9
- } from '../../utils/inputValidation';
10
- import { SplitIO } from '../../types';
11
- import { Identity, GoogleAnalyticsToSplitOptions } from './types';
12
- import { ILogger } from '../../logger/types';
13
- import { IIntegrationFactoryParams } from '../types';
14
- import { ITelemetryTracker } from '../../trackers/types';
15
-
16
- const logPrefix = 'ga-to-split: ';
17
- const logNameMapper = 'ga-to-split:mapper';
18
-
19
- /**
20
- * Provides a plugin to use with analytics.js, accounting for the possibility
21
- * that the global command queue has been renamed or not yet defined.
22
- * @param window Reference to global object.
23
- * @param pluginName The plugin name identifier.
24
- * @param pluginConstructor The plugin constructor function.
25
- * @param log Logger instance.
26
- * @param autoRequire If true, log error when auto-require script is not detected
27
- */
28
- function providePlugin(window: any, pluginName: string, pluginConstructor: Function, log: ILogger, autoRequire: boolean, telemetryTracker?: ITelemetryTracker) {
29
- // get reference to global command queue. Init it if not defined yet.
30
- const gaAlias = window.GoogleAnalyticsObject || 'ga';
31
- window[gaAlias] = window[gaAlias] || function () {
32
- (window[gaAlias].q = window[gaAlias].q || []).push(arguments);
33
- };
34
-
35
- // provides the plugin for use with analytics.js.
36
- window[gaAlias]('provide', pluginName, pluginConstructor);
37
-
38
- const hasAutoRequire = window[gaAlias].q && window[gaAlias].q.push !== [].push;
39
- if (autoRequire && !hasAutoRequire) { // Expecting spy on ga.q push method but not found
40
- log.error(logPrefix + 'integration is configured to autorequire the splitTracker plugin, but the necessary script does not seem to have run. Please check the docs.');
41
- }
42
- if (telemetryTracker && hasAutoRequire) {
43
- telemetryTracker.addTag('integration:ga-autorequire');
44
- }
45
- }
46
-
47
- // Default mapping: object used for building the default mapper from hits to Split events
48
- const defaultMapping = {
49
- eventTypeId: {
50
- event: 'eventAction',
51
- social: 'socialAction',
52
- },
53
- eventValue: {
54
- event: 'eventValue',
55
- timing: 'timingValue',
56
- },
57
- eventProperties: {
58
- pageview: ['page'],
59
- screenview: ['screenName'],
60
- event: ['eventCategory', 'eventLabel'],
61
- social: ['socialNetwork', 'socialTarget'],
62
- timing: ['timingCategory', 'timingVar', 'timingLabel'],
63
- exception: ['exDescription', 'exFatal'],
64
- }
65
- };
66
-
67
- /**
68
- * Build a mapper function based on a mapping object
69
- *
70
- * @param {object} mapping
71
- */
72
- function mapperBuilder(mapping: typeof defaultMapping) {
73
- return function (model: UniversalAnalytics.Model): SplitIO.EventData {
74
- const hitType: string = model.get('hitType');
75
- // @ts-expect-error
76
- const eventTypeId = model.get(mapping.eventTypeId[hitType] || 'hitType');
77
- // @ts-expect-error
78
- const value = model.get(mapping.eventValue[hitType]);
79
-
80
- const properties: Record<string, any> = {}; // @ts-expect-error
81
- const fields: string[] = mapping.eventProperties[hitType];
82
- if (fields) {
83
- for (let i = 0; i < fields.length; i++) {
84
- const fieldValue = model.get(fields[i]);
85
- if (fieldValue !== undefined) properties[fields[i]] = fieldValue;
86
- }
87
- }
88
-
89
- return {
90
- eventTypeId,
91
- value,
92
- properties,
93
- timestamp: Date.now(),
94
- };
95
- };
96
- }
97
-
98
- // exposed for unit testing purposses.
99
- export const defaultMapper = mapperBuilder(defaultMapping);
100
-
101
- export const defaultPrefix = 'ga';
102
-
103
- /**
104
- * Return a new list of identities removing invalid and duplicated ones.
105
- *
106
- * @param {Array} identities list of identities
107
- * @returns list of valid and unique identities. The list might be empty if `identities` is not an array or all its elements are invalid.
108
- */
109
- export function validateIdentities(identities?: Identity[]) {
110
- if (!Array.isArray(identities))
111
- return [];
112
-
113
- // Remove duplicated identities
114
- const uniqueIdentities = uniqAsStrings(identities);
115
-
116
- // Filter based on rum-agent identities validator
117
- return uniqueIdentities.filter(identity => {
118
- if (!identity) return false;
119
-
120
- const maybeKey = identity.key;
121
- const maybeTT = identity.trafficType;
122
-
123
- if (!isString(maybeKey) && !isFiniteNumber(maybeKey))
124
- return false;
125
- if (!isString(maybeTT))
126
- return false;
127
-
128
- return true;
129
- });
130
- }
131
-
132
- /**
133
- * Checks if EventData fields (except EventTypeId) are valid, and logs corresponding warnings.
134
- * EventTypeId is validated separately.
135
- *
136
- * @param {EventData} data event data instance to validate. Precondition: data != undefined
137
- * @returns {boolean} Whether the data instance is a valid EventData or not.
138
- */
139
- export function validateEventData(log: ILogger, eventData: any): eventData is SplitIO.EventData {
140
- if (!validateEvent(log, eventData.eventTypeId, logNameMapper))
141
- return false;
142
-
143
- if (validateEventValue(log, eventData.value, logNameMapper) === false)
144
- return false;
145
-
146
- const { properties } = validateEventProperties(log, eventData.properties, logNameMapper);
147
- if (properties === false)
148
- return false;
149
-
150
- if (eventData.timestamp && !isFiniteNumber(eventData.timestamp))
151
- return false;
152
-
153
- if (eventData.key && validateKey(log, eventData.key, logNameMapper) === false)
154
- return false;
155
-
156
- if (eventData.trafficTypeName && validateTrafficType(log, eventData.trafficTypeName, logNameMapper) === false)
157
- return false;
158
-
159
- return true;
160
- }
161
-
162
- const INVALID_PREFIX_REGEX = /^[^a-zA-Z0-9]+/;
163
- const INVALID_SUBSTRING_REGEX = /[^-_.:a-zA-Z0-9]+/g;
164
- /**
165
- * Fixes the passed string value to comply with EventTypeId format, by removing invalid characters and truncating if necessary.
166
- *
167
- * @param {object} log factory logger
168
- * @param {string} eventTypeId string value to fix.
169
- * @returns {string} Fixed version of `eventTypeId`.
170
- */
171
- export function fixEventTypeId(log: ILogger, eventTypeId: any) {
172
- // return the input eventTypeId if it cannot be fixed
173
- if (!isString(eventTypeId) || eventTypeId.length === 0) {
174
- return eventTypeId;
175
- }
176
-
177
- // replace invalid substrings and truncate
178
- const fixed = eventTypeId
179
- .replace(INVALID_PREFIX_REGEX, '')
180
- .replace(INVALID_SUBSTRING_REGEX, '_');
181
- const truncated = fixed.slice(0, 80);
182
- if (truncated.length < fixed.length) log.warn(logPrefix + 'EventTypeId was truncated because it cannot be more than 80 characters long.');
183
- return truncated;
184
- }
185
-
186
- /**
187
- * GaToSplit integration.
188
- * This function provides the SplitTracker plugin to ga command queue.
189
- *
190
- * @param {object} sdkOptions options passed at the SDK integrations settings (isomorphic SDK) or the GoogleAnalyticsToSplit plugin (pluggable browser SDK)
191
- * @param {object} storage SDK storage passed to track events
192
- * @param {object} coreSettings core settings used to define an identity if no one provided as SDK or plugin options
193
- * @param {object} log factory logger
194
- */
195
- export function GaToSplit(sdkOptions: GoogleAnalyticsToSplitOptions, params: IIntegrationFactoryParams) {
196
-
197
- const { storage, settings: { core: coreSettings, log }, telemetryTracker } = params;
198
-
199
- const defaultOptions = {
200
- prefix: defaultPrefix,
201
- // We set default identities if key and TT are present in settings.core
202
- identities: (coreSettings.key && coreSettings.trafficType) ?
203
- [{ key: coreSettings.key, trafficType: coreSettings.trafficType }] :
204
- undefined
205
- };
206
-
207
- class SplitTracker {
208
-
209
- private tracker: UniversalAnalytics.Tracker;
210
-
211
- // Constructor for the SplitTracker plugin.
212
- constructor(tracker: UniversalAnalytics.Tracker, pluginOptions: GoogleAnalyticsToSplitOptions) {
213
-
214
- // precedence of options: SDK options (config.integrations) overwrite pluginOptions (`ga('require', 'splitTracker', pluginOptions)`)
215
- const opts = objectAssign({}, defaultOptions, sdkOptions, pluginOptions) as GoogleAnalyticsToSplitOptions & { identities: Identity[] };
216
-
217
- this.tracker = tracker;
218
-
219
- // Validate identities
220
- const validIdentities = validateIdentities(opts.identities);
221
-
222
- if (validIdentities.length === 0) {
223
- log.warn(logPrefix + 'No valid identities were provided. Please check that you are passing a valid list of identities or providing a traffic type at the SDK configuration.');
224
- return;
225
- }
226
-
227
- const invalids = validIdentities.length - opts.identities.length;
228
- if (invalids) {
229
- log.warn(logPrefix + `${invalids} identities were discarded because they are invalid or duplicated. Identities must be an array of objects with key and trafficType.`);
230
- }
231
- opts.identities = validIdentities;
232
-
233
- // Validate prefix
234
- if (!isString(opts.prefix)) {
235
- log.warn(logPrefix + 'The provided `prefix` was ignored since it is invalid. Please check that you are passing a string object as `prefix`.');
236
- opts.prefix = undefined;
237
- }
238
-
239
- // Overwrite sendHitTask to perform plugin tasks:
240
- // 1) filter hits
241
- // 2) map hits to Split events
242
- // 3) handle events, i.e., validate and send them to Split BE
243
- const originalSendHitTask = tracker.get('sendHitTask');
244
- tracker.set('sendHitTask', function (model: UniversalAnalytics.Model) {
245
- originalSendHitTask(model);
246
-
247
- // filter hit if `hits` flag is false or if it comes from Split-to-GA integration
248
- if (opts.hits === false || model.get('splitHit')) return;
249
- try {
250
- if (opts.filter && !opts.filter(model)) return;
251
- } catch (err) {
252
- log.warn(logPrefix + `custom filter threw: ${err}`);
253
- return;
254
- }
255
-
256
- // map hit into an EventData instance
257
- let eventData: SplitIO.EventData = defaultMapper(model);
258
- if (opts.mapper) {
259
- try {
260
- eventData = opts.mapper(model, eventData as SplitIO.EventData);
261
- } catch (err) {
262
- log.warn(logPrefix + `custom mapper threw: ${err}`);
263
- return;
264
- }
265
- if (!eventData)
266
- return;
267
- }
268
-
269
- // Add prefix. Nothing is appended if the prefix is falsy, e.g. undefined or ''.
270
- if (opts.prefix) eventData.eventTypeId = `${opts.prefix}.${eventData.eventTypeId}`;
271
-
272
- eventData.eventTypeId = fixEventTypeId(log, eventData.eventTypeId);
273
-
274
- if (!validateEventData(log, eventData))
275
- return;
276
-
277
- // Store the event
278
- if (eventData.key && eventData.trafficTypeName) {
279
- storage.events.track(eventData);
280
- } else { // Store the event for each Key-TT pair (identities), if key and TT is not present in eventData
281
- opts.identities.forEach(identity => {
282
- const event = objectAssign({
283
- key: identity.key,
284
- trafficTypeName: identity.trafficType,
285
- }, eventData);
286
- storage.events.track(event);
287
- });
288
- }
289
- });
290
-
291
- log.info(logPrefix + 'integration started');
292
- }
293
-
294
- }
295
-
296
- // Register the plugin, even if config is invalid, since, if not provided, it will block `ga` command queue.
297
- // eslint-disable-next-line no-undef
298
- providePlugin(window, 'splitTracker', SplitTracker, log, sdkOptions.autoRequire === true, telemetryTracker);
299
- }
@@ -1,14 +0,0 @@
1
- import { IIntegrationFactoryParams, IntegrationFactory } from '../types';
2
- import { GaToSplit } from './GaToSplit';
3
- import { GoogleAnalyticsToSplitOptions } from './types';
4
-
5
- export function GoogleAnalyticsToSplit(options: GoogleAnalyticsToSplitOptions = {}): IntegrationFactory {
6
-
7
- // GaToSplit integration factory
8
- function GoogleAnalyticsToSplitFactory(params: IIntegrationFactoryParams) {
9
- return GaToSplit(options, params);
10
- }
11
-
12
- GoogleAnalyticsToSplitFactory.type = 'GOOGLE_ANALYTICS_TO_SPLIT';
13
- return GoogleAnalyticsToSplitFactory;
14
- }
@@ -1,135 +0,0 @@
1
- /* eslint-disable no-undef */
2
- import { uniq } from '../../utils/lang';
3
- import { SPLIT_IMPRESSION, SPLIT_EVENT } from '../../utils/constants';
4
- import { SplitIO } from '../../types';
5
- import { IIntegration } from '../types';
6
- import { SplitToGoogleAnalyticsOptions } from './types';
7
- import { ILogger } from '../../logger/types';
8
-
9
- const logPrefix = 'split-to-ga: ';
10
- const noGaWarning = '`ga` command queue not found.';
11
- const noHit = 'No hit was sent.';
12
-
13
- export class SplitToGa implements IIntegration {
14
-
15
- // A falsy object represents the default tracker
16
- static defaultTrackerNames = [''];
17
-
18
- private trackerNames: string[];
19
- private filter?: (data: SplitIO.IntegrationData) => boolean;
20
- private mapper?: (data: SplitIO.IntegrationData, defaultMapping: UniversalAnalytics.FieldsObject) => UniversalAnalytics.FieldsObject;
21
- private impressions: boolean | undefined;
22
- private events: boolean | undefined;
23
- private log: ILogger;
24
-
25
- // Default mapper function.
26
- static defaultMapper({ type, payload }: SplitIO.IntegrationData): UniversalAnalytics.FieldsObject {
27
- switch (type) {
28
- case SPLIT_IMPRESSION:
29
- return {
30
- hitType: 'event',
31
- eventCategory: 'split-impression',
32
- eventAction: 'Evaluate ' + (payload as SplitIO.ImpressionData).impression.feature,
33
- eventLabel: 'Treatment: ' + (payload as SplitIO.ImpressionData).impression.treatment + '. Targeting rule: ' + (payload as SplitIO.ImpressionData).impression.label + '.',
34
- nonInteraction: true,
35
- };
36
- case SPLIT_EVENT:
37
- return {
38
- hitType: 'event',
39
- eventCategory: 'split-event',
40
- eventAction: (payload as SplitIO.EventData).eventTypeId,
41
- eventValue: (payload as SplitIO.EventData).value,
42
- nonInteraction: true,
43
- };
44
- }
45
- }
46
-
47
- // Util to access ga command queue, accounting for the possibility that it has been renamed.
48
- static getGa(): UniversalAnalytics.ga | undefined { // @ts-expect-error
49
- return typeof window !== 'undefined' ? window[window['GoogleAnalyticsObject'] || 'ga'] : undefined;
50
- }
51
-
52
- /**
53
- * Validates if a given object is a UniversalAnalytics.FieldsObject instance, and logs a warning if not.
54
- * It checks that the object contains a `hitType`, since it is the minimal field required to send the hit
55
- * and avoid the GA error `No hit type specified. Aborting hit.`.
56
- * Other validations (e.g., an `event` hitType must have a `eventCategory` and `eventAction`) are handled
57
- * and logged (as warnings or errors depending the case) by GA debugger, but the hit is sent anyway.
58
- *
59
- * @param {object} log factory logger
60
- * @param {UniversalAnalytics.FieldsObject} fieldsObject object to validate.
61
- * @returns {boolean} Whether the data instance is a valid FieldsObject or not.
62
- */
63
- static validateFieldsObject(log: ILogger, fieldsObject: any): fieldsObject is UniversalAnalytics.FieldsObject {
64
- if (fieldsObject && fieldsObject.hitType) return true;
65
-
66
- log.warn(logPrefix + 'your custom mapper returned an invalid FieldsObject instance. It must be an object with at least a `hitType` field.');
67
- return false;
68
- }
69
-
70
- /**
71
- * constructor description
72
- * @param {object} options options passed at the SDK integrations settings (isomorphic SDK) or the SplitToGoogleAnalytics plugin (pluggable browser SDK)
73
- */
74
- constructor(log: ILogger, options: SplitToGoogleAnalyticsOptions) {
75
-
76
- this.trackerNames = SplitToGa.defaultTrackerNames;
77
- this.log = log;
78
-
79
- if (options) {
80
- if (typeof options.filter === 'function') this.filter = options.filter;
81
- if (typeof options.mapper === 'function') this.mapper = options.mapper;
82
- // We strip off duplicated values if we received a `trackerNames` param.
83
- // We don't warn if a tracker does not exist, since the user might create it after the SDK is initialized.
84
- // Note: GA allows to create and get trackers using a string or number as tracker name, and does nothing if other types are used.
85
- if (Array.isArray(options.trackerNames)) this.trackerNames = uniq(options.trackerNames);
86
-
87
- // No need to validate `impressions` and `events` flags. Any other value than `false` is ignored (considered true by default).
88
- this.impressions = options.impressions;
89
- this.events = options.events;
90
- }
91
-
92
- log.info(logPrefix + 'integration started');
93
- if (typeof SplitToGa.getGa() !== 'function') log.warn(logPrefix + `${noGaWarning} No hits will be sent until it is available.`);
94
- }
95
-
96
- queue(data: SplitIO.IntegrationData) {
97
- // access ga command queue via `getGa` method, accounting for the possibility that
98
- // the global `ga` reference was not yet mutated by analytics.js.
99
- const ga = SplitToGa.getGa();
100
- if (ga) {
101
-
102
- if (this.impressions === false && data.type === SPLIT_IMPRESSION) return;
103
- if (this.events === false && data.type === SPLIT_EVENT) return;
104
-
105
- let fieldsObject: UniversalAnalytics.FieldsObject & { splitHit?: boolean };
106
- try { // only try/catch filter and mapper, which might be defined by the user
107
- // filter
108
- if (this.filter && !this.filter(data)) return;
109
-
110
- // map data into a FieldsObject instance
111
- fieldsObject = SplitToGa.defaultMapper(data);
112
- if (this.mapper) {
113
- fieldsObject = this.mapper(data, fieldsObject);
114
- // don't send the hit if it is falsy or invalid
115
- if (!fieldsObject || !SplitToGa.validateFieldsObject(this.log, fieldsObject)) return;
116
- }
117
- } catch (err) {
118
- this.log.warn(logPrefix + `queue method threw: ${err}. ${noHit}`);
119
- return;
120
- }
121
-
122
- // send the hit
123
- this.trackerNames.forEach(trackerName => {
124
- const sendCommand = trackerName ? `${trackerName}.send` : 'send';
125
- // mark the hit as a Split one to avoid the loop.
126
- fieldsObject.splitHit = true;
127
- // Send to GA using our reference to the GA object.
128
- ga(sendCommand, fieldsObject);
129
- });
130
- } else {
131
- this.log.warn(logPrefix + `${noGaWarning} ${noHit}`);
132
- }
133
- }
134
-
135
- }
@@ -1,14 +0,0 @@
1
- import { IIntegrationFactoryParams, IntegrationFactory } from '../types';
2
- import { SplitToGa } from './SplitToGa';
3
- import { SplitToGoogleAnalyticsOptions } from './types';
4
-
5
- export function SplitToGoogleAnalytics(options: SplitToGoogleAnalyticsOptions = {}): IntegrationFactory {
6
-
7
- // SplitToGa integration factory
8
- function SplitToGoogleAnalyticsFactory(params: IIntegrationFactoryParams) {
9
- return new SplitToGa(params.settings.log, options);
10
- }
11
-
12
- SplitToGoogleAnalyticsFactory.type = 'SPLIT_TO_GOOGLE_ANALYTICS';
13
- return SplitToGoogleAnalyticsFactory;
14
- }
@@ -1,33 +0,0 @@
1
- /* eslint-disable no-undef */
2
- /**
3
- * Auto-require script to use with GoogleAnalyticsToSplit integration
4
- */
5
- (function (w, g, o) {
6
- w[o] = w[o] || g;
7
- w[g] = w[g] || function () { w[g].q.push(arguments); };
8
- w[g].q = w[g].q || [];
9
-
10
- var trackerNames = {};
11
- function name(arg) { return typeof arg === 'object' && typeof arg.name === 'string' && arg.name; }
12
-
13
- function processCommand(command) { // Queue a `require` command if v is a `create` command
14
- if (command && command[0] === 'create') {
15
- var trackerName = name(command[1]) || name(command[2]) || name(command[3]) || (typeof command[3] === 'string' ? command[3] : undefined); // Get tracker name
16
-
17
- if (!trackerNames[trackerName]) {
18
- trackerNames[trackerName] = true;
19
- w[g]((trackerName ? trackerName + '.' : '') + 'require', 'splitTracker'); // Auto-require
20
- }
21
- }
22
- }
23
-
24
- w[g].q.forEach(processCommand); // Process already queued commands
25
-
26
- var originalPush = w[g].q.push;
27
- w[g].q.push = function (command) { // Spy new queued commands
28
- var result = originalPush.apply(this, arguments);
29
- processCommand(command);
30
- return result;
31
- };
32
-
33
- })(window, 'ga', 'GoogleAnalyticsObject');
@@ -1,153 +0,0 @@
1
- import { SplitIO } from '../../types';
2
-
3
- /**
4
- * A pair of user key and it's trafficType, required for tracking valid Split events.
5
- * @typedef {Object} Identity
6
- * @property {string} key The user key.
7
- * @property {string} trafficType The key traffic type.
8
- */
9
- export type Identity = {
10
- key: string;
11
- trafficType: string;
12
- };
13
-
14
- /**
15
- * Options for GoogleAnalyticsToSplit integration plugin
16
- */
17
- export interface GoogleAnalyticsToSplitOptions {
18
- /**
19
- * Optional flag to filter GA hits from being tracked as Split events.
20
- * @property {boolean} hits
21
- * @default true
22
- */
23
- hits?: boolean,
24
- /**
25
- * Optional predicate used to define a custom filter for tracking GA hits as Split events.
26
- * For example, the following filter allows to track only 'event' hits:
27
- * `(model) => model.get('hitType') === 'event'`
28
- * By default, all hits are tracked as Split events.
29
- */
30
- filter?: (model: UniversalAnalytics.Model) => boolean,
31
- /**
32
- * Optional function useful when you need to modify the Split event before tracking it.
33
- * This function is invoked with two arguments:
34
- * 1. the GA model object representing the hit.
35
- * 2. the default format of the mapped Split event instance.
36
- * The return value must be a Split event, that can be the second argument or a new object.
37
- *
38
- * For example, the following mapper adds a custom property to events:
39
- * `(model, defaultMapping) => {
40
- * defaultMapping.properties.someProperty = SOME_VALUE;
41
- * return defaultMapping;
42
- * }`
43
- */
44
- mapper?: (model: UniversalAnalytics.Model, defaultMapping: SplitIO.EventData) => SplitIO.EventData,
45
- /**
46
- * Optional prefix for EventTypeId, to prevent any kind of data collision between events.
47
- * @property {string} prefix
48
- * @default 'ga'
49
- */
50
- prefix?: string,
51
- /**
52
- * List of Split identities (key & traffic type pairs) used to track events.
53
- * If not provided, events are sent using the key and traffic type provided at SDK config
54
- */
55
- identities?: Identity[],
56
- /**
57
- * Optional flag to log an error if the `auto-require` script is not detected.
58
- * The auto-require script automatically requires the `splitTracker` plugin for created trackers,
59
- * and should be placed right after your Google Analytics, Google Tag Manager or gtag.js script tag.
60
- *
61
- * @see {@link https://help.split.io/hc/en-us/articles/360040838752#set-up-with-gtm-and-gtag.js}
62
- *
63
- * @property {boolean} autoRequire
64
- * @default false
65
- */
66
- autoRequire?: boolean,
67
- }
68
-
69
- /**
70
- * Enable 'Google Analytics to Split' integration, to track Google Analytics hits as Split events.
71
- * Used by the browser variant of the isomorphic JS SDK.
72
- *
73
- * @see {@link https://help.split.io/hc/en-us/articles/360040838752#google-analytics-to-split}
74
- */
75
- export interface IGoogleAnalyticsToSplitConfig extends GoogleAnalyticsToSplitOptions {
76
- type: 'GOOGLE_ANALYTICS_TO_SPLIT'
77
- }
78
-
79
- /**
80
- * Options for SplitToGoogleAnalytics integration plugin
81
- */
82
- export interface SplitToGoogleAnalyticsOptions {
83
- /**
84
- * Optional flag to filter Split impressions from being tracked as GA hits.
85
- * @property {boolean} impressions
86
- * @default true
87
- */
88
- impressions?: boolean,
89
- /**
90
- * Optional flag to filter Split events from being tracked as GA hits.
91
- * @property {boolean} events
92
- * @default true
93
- */
94
- events?: boolean,
95
- /**
96
- * Optional predicate used to define a custom filter for tracking Split data (events and impressions) as GA hits.
97
- * For example, the following filter allows to track only impressions, equivalent to setting events to false:
98
- * `(data) => data.type === 'IMPRESSION'`
99
- */
100
- filter?: (data: SplitIO.IntegrationData) => boolean,
101
- /**
102
- * Optional function useful when you need to modify the GA hit before sending it.
103
- * This function is invoked with two arguments:
104
- * 1. the input data (Split event or impression).
105
- * 2. the default format of the mapped FieldsObject instance (GA hit).
106
- * The return value must be a FieldsObject, that can be the second argument or a new object.
107
- *
108
- * For example, the following mapper adds a custom dimension to hits:
109
- * `(data, defaultMapping) => {
110
- * defaultMapping.dimension1 = SOME_VALUE;
111
- * return defaultMapping;
112
- * }`
113
- *
114
- * Default FieldsObject instance for data.type === 'IMPRESSION':
115
- * `{
116
- * hitType: 'event',
117
- * eventCategory: 'split-impression',
118
- * eventAction: 'Evaluate ' + data.payload.impression.feature,
119
- * eventLabel: 'Treatment: ' + data.payload.impression.treatment + '. Targeting rule: ' + data.payload.impression.label + '.',
120
- * nonInteraction: true,
121
- * }`
122
- * Default FieldsObject instance for data.type === 'EVENT':
123
- * `{
124
- * hitType: 'event',
125
- * eventCategory: 'split-event',
126
- * eventAction: data.payload.eventTypeId,
127
- * eventValue: data.payload.value,
128
- * nonInteraction: true,
129
- * }`
130
- */
131
- mapper?: (data: SplitIO.IntegrationData, defaultMapping: UniversalAnalytics.FieldsObject) => UniversalAnalytics.FieldsObject,
132
- /**
133
- * List of tracker names to send the hit. An empty string represents the default tracker.
134
- * If not provided, hits are only sent to default tracker.
135
- */
136
- trackerNames?: string[],
137
- }
138
-
139
- /**
140
- * Enable 'Split to Google Analytics' integration, to track Split impressions and events as Google Analytics hits.
141
- * Used by the browser variant of the isomorphic JS SDK.
142
- *
143
- * @see {@link https://help.split.io/hc/en-us/articles/360040838752#split-to-google-analytics}
144
- */
145
- export interface ISplitToGoogleAnalyticsConfig extends SplitToGoogleAnalyticsOptions {
146
- type: 'SPLIT_TO_GOOGLE_ANALYTICS'
147
- }
148
-
149
- /**
150
- * Available integration options for the browser
151
- * Used by the browser variant of the isomorphic JS SDK.
152
- */
153
- export type BrowserIntegration = ISplitToGoogleAnalyticsConfig | IGoogleAnalyticsToSplitConfig;