@splitsoftware/splitio-commons 1.17.1-rc.2 → 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 (195) 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/sdkClient/clientCS.js +5 -8
  12. package/cjs/sdkClient/sdkClientMethodCS.js +1 -1
  13. package/cjs/sdkFactory/index.js +1 -3
  14. package/cjs/services/decorateHeaders.js +1 -2
  15. package/cjs/storages/AbstractSplitsCacheAsync.js +7 -0
  16. package/cjs/storages/AbstractSplitsCacheSync.js +7 -0
  17. package/cjs/storages/KeyBuilderCS.js +0 -9
  18. package/cjs/storages/dataLoader.js +32 -65
  19. package/cjs/storages/inLocalStorage/MySegmentsCacheInLocal.js +1 -21
  20. package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +14 -7
  21. package/cjs/storages/inLocalStorage/index.js +1 -4
  22. package/cjs/storages/inMemory/InMemoryStorageCS.js +4 -16
  23. package/cjs/storages/inMemory/SegmentsCacheInMemory.js +3 -4
  24. package/cjs/storages/inMemory/SplitsCacheInMemory.js +2 -3
  25. package/cjs/storages/inMemory/UniqueKeysCacheInMemory.js +2 -3
  26. package/cjs/storages/inMemory/UniqueKeysCacheInMemoryCS.js +2 -3
  27. package/cjs/storages/inRedis/RedisAdapter.js +2 -3
  28. package/cjs/storages/inRedis/SplitsCacheInRedis.js +1 -1
  29. package/cjs/storages/inRedis/TelemetryCacheInRedis.js +3 -4
  30. package/cjs/storages/inRedis/UniqueKeysCacheInRedis.js +1 -2
  31. package/cjs/storages/pluggable/SplitsCachePluggable.js +1 -1
  32. package/cjs/storages/pluggable/TelemetryCachePluggable.js +6 -7
  33. package/cjs/storages/pluggable/UniqueKeysCachePluggable.js +1 -2
  34. package/cjs/storages/pluggable/inMemoryWrapper.js +7 -8
  35. package/cjs/sync/offline/syncTasks/fromObjectSyncTask.js +7 -2
  36. package/cjs/sync/polling/pollingManagerSS.js +3 -3
  37. package/cjs/sync/polling/updaters/splitChangesUpdater.js +13 -5
  38. package/cjs/sync/streaming/parseUtils.js +0 -1
  39. package/cjs/sync/streaming/pushManager.js +2 -3
  40. package/cjs/utils/LRUCache/index.js +1 -2
  41. package/cjs/utils/constants/browser.js +1 -4
  42. package/cjs/utils/lang/index.js +6 -9
  43. package/cjs/utils/lang/objectAssign.js +12 -77
  44. package/cjs/utils/lang/sets.js +3 -110
  45. package/cjs/utils/settingsValidation/index.js +0 -9
  46. package/cjs/utils/settingsValidation/logger/builtinLogger.js +1 -2
  47. package/cjs/utils/settingsValidation/storage/storageCS.js +12 -1
  48. package/esm/evaluator/index.js +3 -3
  49. package/esm/evaluator/matchers/semver_inlist.js +1 -2
  50. package/esm/evaluator/matchers/whitelist.js +1 -2
  51. package/esm/listeners/browser.js +1 -2
  52. package/esm/logger/browser/DebugLogger.js +1 -2
  53. package/esm/logger/browser/ErrorLogger.js +1 -2
  54. package/esm/logger/browser/InfoLogger.js +1 -2
  55. package/esm/logger/browser/WarnLogger.js +1 -2
  56. package/esm/logger/index.js +1 -2
  57. package/esm/sdkClient/clientCS.js +5 -8
  58. package/esm/sdkClient/sdkClientMethodCS.js +1 -1
  59. package/esm/sdkFactory/index.js +2 -4
  60. package/esm/services/decorateHeaders.js +1 -2
  61. package/esm/storages/AbstractSplitsCacheAsync.js +7 -0
  62. package/esm/storages/AbstractSplitsCacheSync.js +7 -0
  63. package/esm/storages/KeyBuilderCS.js +0 -9
  64. package/esm/storages/dataLoader.js +30 -62
  65. package/esm/storages/inLocalStorage/MySegmentsCacheInLocal.js +1 -21
  66. package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +14 -7
  67. package/esm/storages/inLocalStorage/index.js +2 -5
  68. package/esm/storages/inMemory/InMemoryStorageCS.js +4 -16
  69. package/esm/storages/inMemory/SegmentsCacheInMemory.js +3 -4
  70. package/esm/storages/inMemory/SplitsCacheInMemory.js +2 -3
  71. package/esm/storages/inMemory/UniqueKeysCacheInMemory.js +2 -3
  72. package/esm/storages/inMemory/UniqueKeysCacheInMemoryCS.js +2 -3
  73. package/esm/storages/inRedis/RedisAdapter.js +2 -3
  74. package/esm/storages/inRedis/SplitsCacheInRedis.js +2 -2
  75. package/esm/storages/inRedis/TelemetryCacheInRedis.js +3 -4
  76. package/esm/storages/inRedis/UniqueKeysCacheInRedis.js +1 -2
  77. package/esm/storages/pluggable/SplitsCachePluggable.js +2 -2
  78. package/esm/storages/pluggable/TelemetryCachePluggable.js +6 -7
  79. package/esm/storages/pluggable/UniqueKeysCachePluggable.js +1 -2
  80. package/esm/storages/pluggable/inMemoryWrapper.js +7 -8
  81. package/esm/sync/offline/syncTasks/fromObjectSyncTask.js +8 -3
  82. package/esm/sync/polling/pollingManagerSS.js +3 -3
  83. package/esm/sync/polling/updaters/splitChangesUpdater.js +14 -6
  84. package/esm/sync/streaming/parseUtils.js +0 -1
  85. package/esm/sync/streaming/pushManager.js +2 -3
  86. package/esm/utils/LRUCache/index.js +1 -2
  87. package/esm/utils/constants/browser.js +0 -3
  88. package/esm/utils/lang/index.js +6 -9
  89. package/esm/utils/lang/objectAssign.js +12 -77
  90. package/esm/utils/lang/sets.js +2 -107
  91. package/esm/utils/settingsValidation/index.js +0 -9
  92. package/esm/utils/settingsValidation/logger/builtinLogger.js +1 -2
  93. package/esm/utils/settingsValidation/storage/storageCS.js +10 -0
  94. package/package.json +2 -2
  95. package/src/evaluator/index.ts +5 -5
  96. package/src/evaluator/matchers/semver_inlist.ts +1 -2
  97. package/src/evaluator/matchers/whitelist.ts +1 -3
  98. package/src/listeners/browser.ts +1 -2
  99. package/src/logger/browser/DebugLogger.ts +1 -2
  100. package/src/logger/browser/ErrorLogger.ts +1 -2
  101. package/src/logger/browser/InfoLogger.ts +1 -2
  102. package/src/logger/browser/WarnLogger.ts +1 -2
  103. package/src/logger/index.ts +3 -4
  104. package/src/sdkClient/clientCS.ts +5 -8
  105. package/src/sdkClient/sdkClientMethodCS.ts +1 -1
  106. package/src/sdkFactory/index.ts +2 -5
  107. package/src/sdkFactory/types.ts +1 -1
  108. package/src/services/decorateHeaders.ts +1 -2
  109. package/src/storages/AbstractSplitsCacheAsync.ts +9 -2
  110. package/src/storages/AbstractSplitsCacheSync.ts +9 -2
  111. package/src/storages/KeyBuilderCS.ts +0 -13
  112. package/src/storages/dataLoader.ts +32 -63
  113. package/src/storages/inLocalStorage/MySegmentsCacheInLocal.ts +1 -21
  114. package/src/storages/inLocalStorage/SplitsCacheInLocal.ts +16 -8
  115. package/src/storages/inLocalStorage/index.ts +2 -6
  116. package/src/storages/inMemory/InMemoryStorageCS.ts +4 -19
  117. package/src/storages/inMemory/SegmentsCacheInMemory.ts +4 -5
  118. package/src/storages/inMemory/SplitsCacheInMemory.ts +4 -5
  119. package/src/storages/inMemory/UniqueKeysCacheInMemory.ts +4 -5
  120. package/src/storages/inMemory/UniqueKeysCacheInMemoryCS.ts +4 -5
  121. package/src/storages/inRedis/RedisAdapter.ts +3 -4
  122. package/src/storages/inRedis/SplitsCacheInRedis.ts +3 -3
  123. package/src/storages/inRedis/TelemetryCacheInRedis.ts +3 -4
  124. package/src/storages/inRedis/UniqueKeysCacheInRedis.ts +1 -2
  125. package/src/storages/pluggable/SegmentsCachePluggable.ts +0 -1
  126. package/src/storages/pluggable/SplitsCachePluggable.ts +3 -3
  127. package/src/storages/pluggable/TelemetryCachePluggable.ts +6 -7
  128. package/src/storages/pluggable/UniqueKeysCachePluggable.ts +1 -2
  129. package/src/storages/pluggable/inMemoryWrapper.ts +8 -9
  130. package/src/storages/types.ts +9 -5
  131. package/src/sync/offline/syncTasks/fromObjectSyncTask.ts +7 -3
  132. package/src/sync/polling/pollingManagerSS.ts +2 -3
  133. package/src/sync/polling/updaters/splitChangesUpdater.ts +15 -8
  134. package/src/sync/streaming/parseUtils.ts +0 -1
  135. package/src/sync/streaming/pushManager.ts +3 -4
  136. package/src/sync/submitters/types.ts +3 -4
  137. package/src/types.ts +9 -18
  138. package/src/utils/LRUCache/index.ts +2 -3
  139. package/src/utils/constants/browser.ts +0 -4
  140. package/src/utils/lang/index.ts +6 -7
  141. package/src/utils/lang/objectAssign.ts +13 -92
  142. package/src/utils/lang/sets.ts +3 -125
  143. package/src/utils/settingsValidation/index.ts +0 -10
  144. package/src/utils/settingsValidation/logger/builtinLogger.ts +1 -2
  145. package/src/utils/settingsValidation/storage/storageCS.ts +13 -0
  146. package/src/utils/settingsValidation/types.ts +0 -2
  147. package/types/logger/index.d.ts +1 -2
  148. package/types/sdkClient/clientCS.d.ts +2 -3
  149. package/types/sdkFactory/types.d.ts +1 -1
  150. package/types/storages/AbstractSplitsCacheAsync.d.ts +6 -2
  151. package/types/storages/AbstractSplitsCacheSync.d.ts +6 -2
  152. package/types/storages/KeyBuilderCS.d.ts +0 -2
  153. package/types/storages/dataLoader.d.ts +6 -17
  154. package/types/storages/inLocalStorage/SplitsCacheInLocal.d.ts +7 -2
  155. package/types/storages/inMemory/SplitsCacheInMemory.d.ts +1 -2
  156. package/types/storages/inMemory/UniqueKeysCacheInMemory.d.ts +2 -3
  157. package/types/storages/inRedis/SplitsCacheInRedis.d.ts +1 -2
  158. package/types/storages/pluggable/SplitsCachePluggable.d.ts +1 -2
  159. package/types/storages/pluggable/inMemoryWrapper.d.ts +1 -2
  160. package/types/storages/types.d.ts +7 -5
  161. package/types/sync/polling/updaters/splitChangesUpdater.d.ts +1 -2
  162. package/types/sync/submitters/types.d.ts +3 -4
  163. package/types/types.d.ts +9 -17
  164. package/types/utils/LRUCache/index.d.ts +1 -2
  165. package/types/utils/constants/browser.d.ts +0 -2
  166. package/types/utils/lang/objectAssign.d.ts +3 -0
  167. package/types/utils/lang/sets.d.ts +1 -61
  168. package/types/utils/settingsValidation/index.d.ts +0 -1
  169. package/types/utils/settingsValidation/storage/storageCS.d.ts +5 -0
  170. package/types/utils/settingsValidation/types.d.ts +0 -2
  171. package/cjs/integrations/browser.js +0 -31
  172. package/cjs/integrations/ga/GaToSplit.js +0 -257
  173. package/cjs/integrations/ga/GoogleAnalyticsToSplit.js +0 -14
  174. package/cjs/integrations/ga/SplitToGa.js +0 -123
  175. package/cjs/integrations/ga/SplitToGoogleAnalytics.js +0 -14
  176. package/cjs/integrations/ga/types.js +0 -2
  177. package/cjs/sdkClient/sdkClientMethodCSWithTT.js +0 -76
  178. package/cjs/utils/lang/maps.js +0 -96
  179. package/esm/integrations/browser.js +0 -27
  180. package/esm/integrations/ga/GaToSplit.js +0 -250
  181. package/esm/integrations/ga/GoogleAnalyticsToSplit.js +0 -10
  182. package/esm/integrations/ga/SplitToGa.js +0 -120
  183. package/esm/integrations/ga/SplitToGoogleAnalytics.js +0 -10
  184. package/esm/integrations/ga/types.js +0 -1
  185. package/esm/sdkClient/sdkClientMethodCSWithTT.js +0 -72
  186. package/esm/utils/lang/maps.js +0 -92
  187. package/src/integrations/browser.ts +0 -35
  188. package/src/integrations/ga/GaToSplit.ts +0 -299
  189. package/src/integrations/ga/GoogleAnalyticsToSplit.ts +0 -14
  190. package/src/integrations/ga/SplitToGa.ts +0 -135
  191. package/src/integrations/ga/SplitToGoogleAnalytics.ts +0 -14
  192. package/src/integrations/ga/autoRequire.js +0 -33
  193. package/src/integrations/ga/types.ts +0 -153
  194. package/src/sdkClient/sdkClientMethodCSWithTT.ts +0 -98
  195. package/src/utils/lang/maps.ts +0 -108
@@ -1,92 +0,0 @@
1
- /**
2
- * Map implementation based on es6-map polyfill (https://github.com/medikoo/es6-map/blob/master/polyfill.js),
3
- * with the minimal features used by the SDK.
4
-
5
- Copyright (C) 2013 Mariusz Nowak (www.medikoo.com)
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining a copy
8
- of this software and associated documentation files (the "Software"), to deal
9
- in the Software without restriction, including without limitation the rights
10
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- copies of the Software, and to permit persons to whom the Software is
12
- furnished to do so, subject to the following conditions:
13
-
14
- The above copyright notice and this permission notice shall be included in
15
- all copies or substantial portions of the Software.
16
-
17
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
- THE SOFTWARE.
24
- **/
25
- var MapPoly = /** @class */ (function () {
26
- // unlike ES6 `Map`, it only accepts an array as first argument iterable
27
- function MapPoly(entries) {
28
- var _this = this;
29
- this.__mapKeysData__ = [];
30
- this.__mapValuesData__ = [];
31
- if (Array.isArray(entries))
32
- entries.forEach(function (entry) { _this.set(entry[0], entry[1]); });
33
- }
34
- MapPoly.prototype.clear = function () {
35
- if (!this.__mapKeysData__.length)
36
- return;
37
- this.__mapKeysData__.length = 0;
38
- this.__mapValuesData__.length = 0;
39
- };
40
- MapPoly.prototype.delete = function (key) {
41
- var index = this.__mapKeysData__.indexOf(key);
42
- if (index === -1)
43
- return false;
44
- this.__mapKeysData__.splice(index, 1);
45
- this.__mapValuesData__.splice(index, 1);
46
- return true;
47
- };
48
- MapPoly.prototype.forEach = function (callbackfn, thisArg) {
49
- for (var i = 0; i < this.__mapKeysData__.length; i++) {
50
- callbackfn.call(thisArg, this.__mapValuesData__[i], this.__mapKeysData__[i], this);
51
- }
52
- };
53
- MapPoly.prototype.get = function (key) {
54
- var index = this.__mapKeysData__.indexOf(key);
55
- if (index === -1)
56
- return;
57
- return this.__mapValuesData__[index];
58
- };
59
- MapPoly.prototype.has = function (key) {
60
- return this.__mapKeysData__.indexOf(key) !== -1;
61
- };
62
- MapPoly.prototype.set = function (key, value) {
63
- var index = this.__mapKeysData__.indexOf(key);
64
- if (index === -1)
65
- index = this.__mapKeysData__.push(key) - 1;
66
- this.__mapValuesData__[index] = value;
67
- return this;
68
- };
69
- Object.defineProperty(MapPoly.prototype, "size", {
70
- get: function () {
71
- return this.__mapKeysData__.length;
72
- },
73
- enumerable: false,
74
- configurable: true
75
- });
76
- return MapPoly;
77
- }());
78
- export { MapPoly };
79
- /**
80
- * return the Map constructor to use. If native Map is not available or it doesn't support the required features (e.g., IE11),
81
- * a ponyfill with minimal features is returned instead.
82
- *
83
- * Exported for testing purposes only.
84
- */
85
- export function __getMapConstructor() {
86
- // eslint-disable-next-line compat/compat
87
- if (typeof Array.from === 'function' && typeof Map === 'function' && Map.prototype && Map.prototype.values) {
88
- return Map;
89
- }
90
- return MapPoly;
91
- }
92
- export var _Map = __getMapConstructor();
@@ -1,35 +0,0 @@
1
- import { GOOGLE_ANALYTICS_TO_SPLIT, SPLIT_TO_GOOGLE_ANALYTICS } from '../utils/constants/browser';
2
- import { IIntegration, IIntegrationManager, IIntegrationFactoryParams } from './types';
3
- import { BrowserIntegration } from './ga/types';
4
- import { pluggableIntegrationsManagerFactory } from './pluggable';
5
- import { GoogleAnalyticsToSplit } from './ga/GoogleAnalyticsToSplit';
6
- import { SplitToGoogleAnalytics } from './ga/SplitToGoogleAnalytics';
7
-
8
- /**
9
- * IntegrationsManager factory for the browser variant of the isomorphic JS SDK.
10
- * The integrations manager instantiates integration modules, and bypass tracked events and impressions to them.
11
- *
12
- * @param integrations valid integration settings object for browser sdk
13
- * @param params information of the Sdk factory instance that integrations can access to
14
- *
15
- * @returns integration manager or undefined if `integrations` are not present in settings.
16
- */
17
- export function integrationsManagerFactory(
18
- integrations: BrowserIntegration[],
19
- params: IIntegrationFactoryParams
20
- ): IIntegrationManager | undefined {
21
-
22
- // maps integration config items into integration factories to reuse the pluggable integration manager
23
- const integrationFactories: Array<(params: IIntegrationFactoryParams) => IIntegration | void> = integrations
24
- .map(integrationOptions => {
25
- switch (integrationOptions.type) {
26
- case GOOGLE_ANALYTICS_TO_SPLIT: return GoogleAnalyticsToSplit(integrationOptions);
27
- case SPLIT_TO_GOOGLE_ANALYTICS: return SplitToGoogleAnalytics(integrationOptions);
28
- }
29
- })
30
- .filter(integrationFactory => {
31
- return integrationFactory && typeof integrationFactory === 'function';
32
- });
33
-
34
- return pluggableIntegrationsManagerFactory(integrationFactories, params);
35
- }
@@ -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');