@splitsoftware/splitio-commons 1.4.0 → 1.4.1-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.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GaToSplit = exports.fixEventTypeId = exports.validateEventData = exports.validateIdentities = exports.defaultPrefix = exports.defaultMapper = void 0;
3
+ exports.autoRequireScript = exports.GaToSplit = exports.fixEventTypeId = exports.validateEventData = exports.validateIdentities = exports.defaultPrefix = exports.defaultMapper = void 0;
4
4
  /* eslint-disable no-undef */
5
5
  var objectAssign_1 = require("../../utils/lang/objectAssign");
6
6
  var lang_1 = require("../../utils/lang");
@@ -12,8 +12,9 @@ var logNameMapper = 'ga-to-split:mapper';
12
12
  * that the global command queue has been renamed or not yet defined.
13
13
  * @param {string} pluginName The plugin name identifier.
14
14
  * @param {Function} pluginConstructor The plugin constructor function.
15
+ * @param {boolean} autoRequire Whether the integration should automatically queue a 'require' command for each 'create' command.
15
16
  */
16
- function providePlugin(pluginName, pluginConstructor) {
17
+ function providePlugin(pluginName, pluginConstructor, autoRequire) {
17
18
  // get reference to global command queue. Init it if not defined yet.
18
19
  // @ts-expect-error
19
20
  var gaAlias = window.GoogleAnalyticsObject || 'ga';
@@ -27,6 +28,8 @@ function providePlugin(pluginName, pluginConstructor) {
27
28
  // provides the plugin for use with analytics.js.
28
29
  // @ts-expect-error
29
30
  window[gaAlias]('provide', pluginName, pluginConstructor);
31
+ if (autoRequire)
32
+ autoRequireScript();
30
33
  }
31
34
  // Default mapping: object used for building the default mapper from hits to Split events
32
35
  var defaultMapping = {
@@ -246,6 +249,34 @@ function GaToSplit(sdkOptions, params) {
246
249
  return SplitTracker;
247
250
  }());
248
251
  // Register the plugin, even if config is invalid, since, if not provided, it will block `ga` command queue.
249
- providePlugin('splitTracker', SplitTracker);
252
+ providePlugin('splitTracker', SplitTracker, sdkOptions.autoRequire);
250
253
  }
251
254
  exports.GaToSplit = GaToSplit;
255
+ function autoRequireScript() {
256
+ (function (i, r) {
257
+ i['GoogleAnalyticsObject'] = r;
258
+ i[r] = i[r] || function () { i[r].q.push(arguments); };
259
+ i[r].q = i[r].q || [];
260
+ var ts = {}; // Tracker names
261
+ var o = i[r].q.push; // Reference to Array.prototype.push
262
+ i[r].q.push = function () {
263
+ var result = o.apply(this, arguments);
264
+ var v = arguments[0];
265
+ if (v && v[0] === 'create') {
266
+ var t = typeof v[2] === 'object' && typeof v[2].name === 'string' ?
267
+ v[2].name : // `ga('create', 'UA-ID', { name: 'trackerName', ... })`
268
+ typeof v[3] === 'object' && typeof v[3].name === 'string' ?
269
+ v[3].name : // `ga('create', 'UA-ID', 'auto', { name: 'trackerName', ... })`
270
+ typeof v[3] === 'string' ?
271
+ v[3] : // `ga('create', 'UA-ID', 'auto', 'trackerName')`
272
+ undefined; // No name tracker, `ga('create', 'UA-ID', 'auto')`
273
+ if (!ts[t]) {
274
+ ts[t] = true;
275
+ i[r](t ? t + '.require' : 'require', 'splitTracker'); // Auto-require
276
+ }
277
+ }
278
+ return result;
279
+ };
280
+ })(window, 'ga');
281
+ }
282
+ exports.autoRequireScript = autoRequireScript;
@@ -9,8 +9,9 @@ var logNameMapper = 'ga-to-split:mapper';
9
9
  * that the global command queue has been renamed or not yet defined.
10
10
  * @param {string} pluginName The plugin name identifier.
11
11
  * @param {Function} pluginConstructor The plugin constructor function.
12
+ * @param {boolean} autoRequire Whether the integration should automatically queue a 'require' command for each 'create' command.
12
13
  */
13
- function providePlugin(pluginName, pluginConstructor) {
14
+ function providePlugin(pluginName, pluginConstructor, autoRequire) {
14
15
  // get reference to global command queue. Init it if not defined yet.
15
16
  // @ts-expect-error
16
17
  var gaAlias = window.GoogleAnalyticsObject || 'ga';
@@ -24,6 +25,8 @@ function providePlugin(pluginName, pluginConstructor) {
24
25
  // provides the plugin for use with analytics.js.
25
26
  // @ts-expect-error
26
27
  window[gaAlias]('provide', pluginName, pluginConstructor);
28
+ if (autoRequire)
29
+ autoRequireScript();
27
30
  }
28
31
  // Default mapping: object used for building the default mapper from hits to Split events
29
32
  var defaultMapping = {
@@ -240,5 +243,32 @@ export function GaToSplit(sdkOptions, params) {
240
243
  return SplitTracker;
241
244
  }());
242
245
  // Register the plugin, even if config is invalid, since, if not provided, it will block `ga` command queue.
243
- providePlugin('splitTracker', SplitTracker);
246
+ providePlugin('splitTracker', SplitTracker, sdkOptions.autoRequire);
247
+ }
248
+ export function autoRequireScript() {
249
+ (function (i, r) {
250
+ i['GoogleAnalyticsObject'] = r;
251
+ i[r] = i[r] || function () { i[r].q.push(arguments); };
252
+ i[r].q = i[r].q || [];
253
+ var ts = {}; // Tracker names
254
+ var o = i[r].q.push; // Reference to Array.prototype.push
255
+ i[r].q.push = function () {
256
+ var result = o.apply(this, arguments);
257
+ var v = arguments[0];
258
+ if (v && v[0] === 'create') {
259
+ var t = typeof v[2] === 'object' && typeof v[2].name === 'string' ?
260
+ v[2].name : // `ga('create', 'UA-ID', { name: 'trackerName', ... })`
261
+ typeof v[3] === 'object' && typeof v[3].name === 'string' ?
262
+ v[3].name : // `ga('create', 'UA-ID', 'auto', { name: 'trackerName', ... })`
263
+ typeof v[3] === 'string' ?
264
+ v[3] : // `ga('create', 'UA-ID', 'auto', 'trackerName')`
265
+ undefined; // No name tracker, `ga('create', 'UA-ID', 'auto')`
266
+ if (!ts[t]) {
267
+ ts[t] = true;
268
+ i[r](t ? t + '.require' : 'require', 'splitTracker'); // Auto-require
269
+ }
270
+ }
271
+ return result;
272
+ };
273
+ })(window, 'ga');
244
274
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@splitsoftware/splitio-commons",
3
- "version": "1.4.0",
3
+ "version": "1.4.1-rc.0",
4
4
  "description": "Split Javascript SDK common components",
5
5
  "main": "cjs/index.js",
6
6
  "module": "esm/index.js",
@@ -21,8 +21,9 @@ const logNameMapper = 'ga-to-split:mapper';
21
21
  * that the global command queue has been renamed or not yet defined.
22
22
  * @param {string} pluginName The plugin name identifier.
23
23
  * @param {Function} pluginConstructor The plugin constructor function.
24
+ * @param {boolean} autoRequire Whether the integration should automatically queue a 'require' command for each 'create' command.
24
25
  */
25
- function providePlugin(pluginName: string, pluginConstructor: Function) {
26
+ function providePlugin(pluginName: string, pluginConstructor: Function, autoRequire?: boolean) {
26
27
  // get reference to global command queue. Init it if not defined yet.
27
28
  // @ts-expect-error
28
29
  const gaAlias = window.GoogleAnalyticsObject || 'ga';
@@ -33,6 +34,8 @@ function providePlugin(pluginName: string, pluginConstructor: Function) {
33
34
  // provides the plugin for use with analytics.js.
34
35
  // @ts-expect-error
35
36
  window[gaAlias]('provide', pluginName, pluginConstructor);
37
+
38
+ if (autoRequire) autoRequireScript();
36
39
  }
37
40
 
38
41
  // Default mapping: object used for building the default mapper from hits to Split events
@@ -284,5 +287,38 @@ export function GaToSplit(sdkOptions: GoogleAnalyticsToSplitOptions, params: IIn
284
287
  }
285
288
 
286
289
  // Register the plugin, even if config is invalid, since, if not provided, it will block `ga` command queue.
287
- providePlugin('splitTracker', SplitTracker);
290
+ providePlugin('splitTracker', SplitTracker, sdkOptions.autoRequire);
291
+ }
292
+
293
+ export function autoRequireScript() {
294
+ (function (i: any, r: any) {
295
+ i['GoogleAnalyticsObject'] = r;
296
+ i[r] = i[r] || function () { i[r].q.push(arguments); };
297
+ i[r].q = i[r].q || [];
298
+
299
+ var ts: any = {}; // Tracker names
300
+ var o = i[r].q.push; // Reference to Array.prototype.push
301
+ i[r].q.push = function () {
302
+ var result = o.apply(this, arguments);
303
+
304
+ var v = arguments[0];
305
+ if (v && v[0] === 'create') {
306
+ var t = typeof v[2] === 'object' && typeof v[2].name === 'string' ?
307
+ v[2].name : // `ga('create', 'UA-ID', { name: 'trackerName', ... })`
308
+ typeof v[3] === 'object' && typeof v[3].name === 'string' ?
309
+ v[3].name : // `ga('create', 'UA-ID', 'auto', { name: 'trackerName', ... })`
310
+ typeof v[3] === 'string' ?
311
+ v[3] : // `ga('create', 'UA-ID', 'auto', 'trackerName')`
312
+ undefined; // No name tracker, `ga('create', 'UA-ID', 'auto')`
313
+
314
+ if (!ts[t]) {
315
+ ts[t] = true;
316
+ i[r](t ? t + '.require' : 'require', 'splitTracker'); // Auto-require
317
+ }
318
+ }
319
+
320
+ return result;
321
+ };
322
+
323
+ })(window, 'ga');
288
324
  }
@@ -53,6 +53,13 @@ export interface GoogleAnalyticsToSplitOptions {
53
53
  * If not provided, events are sent using the key and traffic type provided at SDK config
54
54
  */
55
55
  identities?: Identity[],
56
+ /**
57
+ * Optional flag to automatically require the `splitTracker` plugin for all created trackers.
58
+ * If true, it will spy on the ga command queue, and run `ga('[trackerName].require', 'splitTracker');` for each 'create' command.
59
+ * @property {boolean} autoRequire
60
+ * @default false
61
+ */
62
+ autoRequire?: boolean,
56
63
  }
57
64
 
58
65
  /**
@@ -38,3 +38,4 @@ export declare function fixEventTypeId(log: ILogger, eventTypeId: any): any;
38
38
  * @param {object} log factory logger
39
39
  */
40
40
  export declare function GaToSplit(sdkOptions: GoogleAnalyticsToSplitOptions, params: IIntegrationFactoryParams): void;
41
+ export declare function autoRequireScript(): void;
@@ -52,6 +52,13 @@ export interface GoogleAnalyticsToSplitOptions {
52
52
  * If not provided, events are sent using the key and traffic type provided at SDK config
53
53
  */
54
54
  identities?: Identity[];
55
+ /**
56
+ * Optional flag to automatically require the `splitTracker` plugin for all created trackers.
57
+ * If true, it will spy on the ga command queue, and run `ga('[trackerName].require', 'splitTracker');` for each 'create' command.
58
+ * @property {boolean} autoRequire
59
+ * @default false
60
+ */
61
+ autoRequire?: boolean;
55
62
  }
56
63
  /**
57
64
  * Enable 'Google Analytics to Split' integration, to track Google Analytics hits as Split events.