@blotoutio/edgetag-sdk-browser 1.28.1 → 1.30.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 (2) hide show
  1. package/index.js +208 -60
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  (function () {
2
2
  'use strict';
3
3
 
4
- var api$1 = /*#__PURE__*/Object.freeze({
4
+ var api = /*#__PURE__*/Object.freeze({
5
5
  __proto__: null,
6
6
  get consent () { return consent; },
7
7
  get data () { return data; },
@@ -510,12 +510,17 @@
510
510
  };
511
511
 
512
512
  // eslint-disable-next-line @nx/enforce-module-boundaries
513
- let edgeTagSettings;
513
+ const getGlobalSettings = () => {
514
+ var _a;
515
+ // settings must be globally shared in order for each tag to be able to
516
+ // process the correct destinations when receiving stubs/calls to process
517
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
518
+ // @ts-ignore extending globalThis is a pain, ignore.
519
+ return ((_a = globalThis.edgeTagSettings) !== null && _a !== void 0 ? _a : (globalThis.edgeTagSettings = {}));
520
+ };
514
521
  const initSettings = (destination, options) => {
515
- if (!edgeTagSettings) {
516
- edgeTagSettings = {};
517
- }
518
- edgeTagSettings[destination] = {
522
+ const settings = getGlobalSettings();
523
+ settings[destination] = {
519
524
  destination,
520
525
  initialized: false,
521
526
  stubs: [],
@@ -532,29 +537,27 @@
532
537
  };
533
538
  };
534
539
  const setSetting = (destination, options) => {
535
- if (!edgeTagSettings) {
536
- return;
537
- }
540
+ const settings = getGlobalSettings();
538
541
  if (!destination) {
539
- Object.keys(edgeTagSettings).forEach((key) => {
540
- edgeTagSettings[key] = {
541
- ...edgeTagSettings[key],
542
+ Object.keys(settings).forEach((key) => {
543
+ settings[key] = {
544
+ ...settings[key],
542
545
  ...options,
543
546
  };
544
547
  });
545
548
  return;
546
549
  }
547
- edgeTagSettings[destination] = {
548
- ...edgeTagSettings[destination],
550
+ settings[destination] = {
551
+ ...settings[destination],
549
552
  ...options,
550
553
  };
551
554
  };
552
555
  const getSetting = (destination, key) => {
553
556
  var _a;
554
- return (_a = edgeTagSettings === null || edgeTagSettings === void 0 ? void 0 : edgeTagSettings[destination]) === null || _a === void 0 ? void 0 : _a[key];
557
+ return (_a = getGlobalSettings()[destination]) === null || _a === void 0 ? void 0 : _a[key];
555
558
  };
556
559
  const getInstances = () => {
557
- return Object.keys(edgeTagSettings || {});
560
+ return Object.keys(getGlobalSettings());
558
561
  };
559
562
  const addChannel = (destination, pkg, tagName) => upsert(getSetting(destination, 'channels'), pkg, (names) => names.add(tagName), () => new Set());
560
563
  const getProviderVariables = (destination, packageId) => {
@@ -953,7 +956,7 @@
953
956
  referrer: getReferrer(destination),
954
957
  search: getSearch(destination),
955
958
  locale: getLocale(),
956
- sdkVersion: "1.28.1" ,
959
+ sdkVersion: "1.30.0" ,
957
960
  ...(payload || {}),
958
961
  };
959
962
  let storage = {};
@@ -1059,7 +1062,7 @@
1059
1062
  const stubs = getSetting(destination, 'stubs');
1060
1063
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1061
1064
  // @ts-ignore
1062
- stubs.forEach((stub) => api$1[stub.name](...(stub.arguments || [])));
1065
+ stubs.forEach((stub) => api[stub.name](...(stub.arguments || [])));
1063
1066
  setSetting(destination, {
1064
1067
  stubs: [],
1065
1068
  });
@@ -1284,6 +1287,7 @@
1284
1287
  userId,
1285
1288
  data,
1286
1289
  manifestVariables: variable.variableSet,
1290
+ destination,
1287
1291
  });
1288
1292
  }
1289
1293
  }
@@ -1810,6 +1814,7 @@
1810
1814
  userId,
1811
1815
  data: { [key]: value },
1812
1816
  manifestVariables: variable.variableSet,
1817
+ destination,
1813
1818
  });
1814
1819
  }
1815
1820
  }
@@ -2015,81 +2020,224 @@
2015
2020
  }
2016
2021
  }
2017
2022
 
2018
- var api = new API();
2023
+ var library = new API();
2019
2024
 
2020
2025
  (function () {
2021
- const handleStubs = (event) => {
2026
+ const getTagDestination = (args) => {
2027
+ if (args?.length < 4) {
2028
+ return
2029
+ }
2030
+
2031
+ return args[3].destination
2032
+ };
2033
+
2034
+ const getInitUrl = (args) => {
2035
+ if (args?.length < 1) {
2036
+ return
2037
+ }
2038
+
2039
+ return args[0].edgeURL
2040
+ };
2041
+
2042
+ const addTagToDestination = (destination, tag) => {
2043
+ if (!window.edgetag.destinations[destination]) {
2044
+ window.edgetag.destinations[destination] = {};
2045
+ }
2046
+
2047
+ if (!window.edgetag.destinations[destination].tags) {
2048
+ window.edgetag.destinations[destination].tags = [];
2049
+ }
2050
+
2051
+ window.edgetag.destinations[destination].tags.push({
2052
+ name: 'tag',
2053
+ arguments: tag,
2054
+ });
2055
+ };
2056
+
2057
+ const handleInit = (args) => {
2058
+ if (!args?.length) {
2059
+ console.error('EdgeTag SDK: Init event is missing arguments');
2060
+ return
2061
+ }
2062
+
2063
+ const destination = args[0]?.edgeURL;
2064
+ if (!destination) {
2065
+ console.error('EdgeTag SDK: edgeURL is required for initialization');
2066
+ return
2067
+ }
2068
+
2069
+ if (!window.edgetag.destinations?.[destination]?.initialized) {
2070
+ window.edgetag.stubs.push(['init', ...args]);
2071
+ return
2072
+ }
2073
+
2074
+ const tags = [];
2075
+
2076
+ if (
2077
+ window.edgetag.destinations['all'].tags &&
2078
+ !window.edgetag.destinations['all'].resolvedInits.has(destination)
2079
+ ) {
2080
+ const destinationTags = window.edgetag.destinations['all'].tags.map(
2081
+ (tag) => {
2082
+ const newTag = structuredClone(tag);
2083
+ newTag['arguments'][3] = {
2084
+ ...newTag['arguments'][3],
2085
+ destination,
2086
+ };
2087
+
2088
+ return newTag
2089
+ }
2090
+ );
2091
+ tags.push(...destinationTags);
2092
+ window.edgetag.destinations['all'].resolvedInits.add(destination);
2093
+ }
2094
+
2095
+ if (window.edgetag.destinations[destination].tags) {
2096
+ tags.push(...window.edgetag.destinations[destination].tags);
2097
+ window.edgetag.destinations[destination].tags = [];
2098
+ }
2099
+
2100
+ if (tags.length) {
2101
+ args[0]['afterManifestEvents'] = tags;
2102
+ }
2103
+
2104
+ library.init(...args);
2105
+ };
2106
+
2107
+ const handleTag = (args) => {
2108
+ const destination = getTagDestination(args);
2109
+
2110
+ if (destination && !window.edgetag.destinations[destination]?.initialized) {
2111
+ addTagToDestination(destination, args);
2112
+ return
2113
+ }
2114
+
2115
+ library.tag(...args);
2116
+ };
2117
+
2118
+ const processStub = (name, args) => {
2022
2119
  try {
2023
- return library[event.name](...event.arguments)
2120
+ if (name === 'init') {
2121
+ handleInit(args);
2122
+ return
2123
+ }
2124
+
2125
+ if (name === 'tag') {
2126
+ handleTag(args);
2127
+ return
2128
+ }
2129
+
2130
+ return library[name](...args)
2024
2131
  } catch (e) {
2025
2132
  console.error(e);
2026
2133
  }
2027
2134
  };
2028
2135
 
2029
- const library = api;
2030
- let stubs = [];
2136
+ const handleEvent = (args) => {
2137
+ const sliced = [].slice.call(args);
2138
+ if (!Array.isArray(sliced) || !sliced.length) {
2139
+ return
2140
+ }
2141
+
2142
+ return processStub(sliced[0], sliced.slice(1))
2143
+ };
2144
+
2145
+ let stubs = window.edgetag?.stubs;
2146
+ if (!stubs) {
2147
+ stubs = [];
2148
+ }
2149
+
2031
2150
  if (!window.edgetag?.live) {
2032
- stubs = window.edgetag?.stubs || [];
2151
+ const destinations = window.edgetag?.destinations;
2033
2152
 
2034
2153
  // this needs to be regular function for arguments to work
2035
2154
  window.edgetag = function () {
2036
- const sliced = [].slice.call(arguments);
2037
- if (!Array.isArray(sliced)) {
2038
- return
2039
- }
2040
-
2041
- try {
2042
- return library[sliced[0]](...sliced.slice(1))
2043
- } catch (e) {
2044
- console.error(e);
2045
- }
2155
+ return handleEvent(arguments)
2046
2156
  };
2047
2157
 
2048
2158
  window.edgetag.live = true;
2159
+ window.edgetag.destinations = destinations || {};
2160
+ }
2161
+
2162
+ window.edgetag.stubs = [];
2163
+
2164
+ window.edgetag.handleStubs = () => {
2165
+ if (!window.edgetag.stubs || !Array.isArray(window.edgetag.stubs)) {
2166
+ console.error('EdgeTag SDK: Invalid stubs array');
2167
+ return
2168
+ }
2169
+
2170
+ stubs = window.edgetag.stubs;
2171
+ window.edgetag.stubs = [];
2172
+
2173
+ stubs.forEach((stub) => {
2174
+ handleEvent(stub);
2175
+ });
2176
+ };
2177
+
2178
+ if (!window.edgetag.destinations) {
2179
+ window.edgetag.destinations = {};
2049
2180
  }
2050
2181
 
2051
- const beforeManifestEvents = [];
2052
- const afterManifestEvents = [];
2053
- const initCalls = [];
2182
+ if (!stubs.length) {
2183
+ return
2184
+ }
2185
+
2186
+ const inits = [];
2187
+ const tags = [];
2054
2188
 
2055
- stubs.forEach((arg) => {
2056
- const sliced = [].slice.call(arg);
2189
+ stubs.forEach((stub) => {
2190
+ const sliced = [].slice.call(stub);
2057
2191
  if (!Array.isArray(sliced)) {
2058
2192
  return
2059
2193
  }
2060
2194
 
2061
- if (sliced[0] === 'init') {
2062
- initCalls.push(sliced.slice(1));
2195
+ const name = sliced[0];
2196
+ const args = sliced.slice(1);
2197
+
2198
+ if (name === 'init') {
2199
+ const destination = getInitUrl(args);
2200
+ if (!window.edgetag.destinations[destination]) {
2201
+ window.edgetag.destinations[destination] = {
2202
+ tags: [],
2203
+ };
2204
+ }
2205
+
2206
+ inits.push(args);
2063
2207
  return
2064
2208
  }
2065
2209
 
2066
- if (sliced[0] === 'tag') {
2067
- afterManifestEvents.push({
2068
- name: sliced[0],
2069
- arguments: sliced.slice(1),
2070
- });
2210
+ if (name === 'tag') {
2211
+ tags.push(args);
2071
2212
  return
2072
2213
  }
2073
2214
 
2074
- beforeManifestEvents.push({
2075
- name: sliced[0],
2076
- arguments: sliced.slice(1),
2077
- });
2215
+ processStub(name, args);
2078
2216
  });
2079
2217
 
2080
- if (initCalls?.length) {
2081
- initCalls[initCalls.length - 1][0]['afterManifestEvents'] =
2082
- afterManifestEvents;
2218
+ tags.forEach((tag) => {
2219
+ const destination = getTagDestination(tag);
2220
+ if (destination) {
2221
+ addTagToDestination(destination, tag);
2222
+ return
2223
+ }
2083
2224
 
2084
- initCalls.forEach((initCall) => {
2085
- handleStubs({
2086
- name: 'init',
2087
- arguments: initCall,
2088
- });
2225
+ if (!window.edgetag.destinations['all']) {
2226
+ window.edgetag.destinations['all'] = {
2227
+ tags: [],
2228
+ resolvedInits: new Set(),
2229
+ };
2230
+ }
2231
+
2232
+ window.edgetag.destinations['all'].tags.push({
2233
+ name: 'tag',
2234
+ arguments: tag,
2089
2235
  });
2090
- }
2236
+ });
2091
2237
 
2092
- beforeManifestEvents.forEach(handleStubs);
2238
+ inits.forEach((init) => {
2239
+ window.edgetag.stubs.push(['init', ...init]);
2240
+ });
2093
2241
  })();
2094
2242
 
2095
2243
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/edgetag-sdk-browser",
3
- "version": "1.28.1",
3
+ "version": "1.30.0",
4
4
  "description": "Browser SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",