@blotoutio/edgetag-sdk-browser 1.29.0 → 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 +206 -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.29.0" ,
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
  });
@@ -2017,81 +2020,224 @@
2017
2020
  }
2018
2021
  }
2019
2022
 
2020
- var api = new API();
2023
+ var library = new API();
2021
2024
 
2022
2025
  (function () {
2023
- 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) => {
2024
2119
  try {
2025
- 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)
2026
2131
  } catch (e) {
2027
2132
  console.error(e);
2028
2133
  }
2029
2134
  };
2030
2135
 
2031
- const library = api;
2032
- 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
+
2033
2150
  if (!window.edgetag?.live) {
2034
- stubs = window.edgetag?.stubs || [];
2151
+ const destinations = window.edgetag?.destinations;
2035
2152
 
2036
2153
  // this needs to be regular function for arguments to work
2037
2154
  window.edgetag = function () {
2038
- const sliced = [].slice.call(arguments);
2039
- if (!Array.isArray(sliced)) {
2040
- return
2041
- }
2042
-
2043
- try {
2044
- return library[sliced[0]](...sliced.slice(1))
2045
- } catch (e) {
2046
- console.error(e);
2047
- }
2155
+ return handleEvent(arguments)
2048
2156
  };
2049
2157
 
2050
2158
  window.edgetag.live = true;
2159
+ window.edgetag.destinations = destinations || {};
2051
2160
  }
2052
2161
 
2053
- const beforeManifestEvents = [];
2054
- const afterManifestEvents = [];
2055
- const initCalls = [];
2162
+ window.edgetag.stubs = [];
2056
2163
 
2057
- stubs.forEach((arg) => {
2058
- const sliced = [].slice.call(arg);
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 = {};
2180
+ }
2181
+
2182
+ if (!stubs.length) {
2183
+ return
2184
+ }
2185
+
2186
+ const inits = [];
2187
+ const tags = [];
2188
+
2189
+ stubs.forEach((stub) => {
2190
+ const sliced = [].slice.call(stub);
2059
2191
  if (!Array.isArray(sliced)) {
2060
2192
  return
2061
2193
  }
2062
2194
 
2063
- if (sliced[0] === 'init') {
2064
- 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);
2065
2207
  return
2066
2208
  }
2067
2209
 
2068
- if (sliced[0] === 'tag') {
2069
- afterManifestEvents.push({
2070
- name: sliced[0],
2071
- arguments: sliced.slice(1),
2072
- });
2210
+ if (name === 'tag') {
2211
+ tags.push(args);
2073
2212
  return
2074
2213
  }
2075
2214
 
2076
- beforeManifestEvents.push({
2077
- name: sliced[0],
2078
- arguments: sliced.slice(1),
2079
- });
2215
+ processStub(name, args);
2080
2216
  });
2081
2217
 
2082
- if (initCalls?.length) {
2083
- initCalls[initCalls.length - 1][0]['afterManifestEvents'] =
2084
- afterManifestEvents;
2218
+ tags.forEach((tag) => {
2219
+ const destination = getTagDestination(tag);
2220
+ if (destination) {
2221
+ addTagToDestination(destination, tag);
2222
+ return
2223
+ }
2085
2224
 
2086
- initCalls.forEach((initCall) => {
2087
- handleStubs({
2088
- name: 'init',
2089
- arguments: initCall,
2090
- });
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,
2091
2235
  });
2092
- }
2236
+ });
2093
2237
 
2094
- beforeManifestEvents.forEach(handleStubs);
2238
+ inits.forEach((init) => {
2239
+ window.edgetag.stubs.push(['init', ...init]);
2240
+ });
2095
2241
  })();
2096
2242
 
2097
2243
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/edgetag-sdk-browser",
3
- "version": "1.29.0",
3
+ "version": "1.30.0",
4
4
  "description": "Browser SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",