@highfivve/ad-tag 5.8.11 → 5.8.12

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.
@@ -3,15 +3,26 @@ import { resolveAdUnitPath } from '../adUnitPath';
3
3
  import { formatKey } from 'ad-tag/ads/keyValues';
4
4
  import { isGamInterstitial } from 'ad-tag/ads/auctions/interstitialContext';
5
5
  const sessionStorageKey = 'h5v-fc';
6
+ const localStorageKey = 'h5v-pi';
6
7
  const hasPacingInterval = (config) => !!config.conditions && !!config.conditions.pacingInterval;
7
8
  export const createFrequencyCapping = (config, _window, now, logger) => {
8
9
  const positionImpSchedules = new Map();
9
10
  const positionLastImpressionNumberOfAdRequests = new Map();
10
11
  const bidderImpSchedules = new Map();
11
12
  const positionAdRequests = new Map();
12
- let numAdRequests = 0;
13
- const pageImpressionStorageKey = 'h5v_pi';
14
- let currentPageImpression = Number(_window.localStorage.getItem(pageImpressionStorageKey) ?? '0');
13
+ let sessionNumAdRequests = 0;
14
+ let totalNumAdRequests = 0;
15
+ let existingLocalStorageData = null;
16
+ try {
17
+ const stored = _window.localStorage.getItem(localStorageKey);
18
+ if (stored) {
19
+ existingLocalStorageData = JSON.parse(stored);
20
+ totalNumAdRequests = existingLocalStorageData.requestAds;
21
+ }
22
+ }
23
+ catch (e) {
24
+ logger.debug('fc', 'failed to parse localStorage data, starting fresh', e);
25
+ }
15
26
  const pacingIntervalConfigs = config.bidders?.filter(hasPacingInterval) ?? [];
16
27
  const bidWonConfigs = pacingIntervalConfigs.filter(config => !config.conditions.pacingInterval?.events ||
17
28
  config.conditions.pacingInterval?.events?.includes('bidWon')) ?? [];
@@ -22,12 +33,20 @@ export const createFrequencyCapping = (config, _window, now, logger) => {
22
33
  const data = {
23
34
  bCaps: Array.from(bidderImpSchedules.entries()).reduce((acc, [key, schedules]) => ({ ...acc, [key]: schedules }), {}),
24
35
  pCaps: Array.from(positionImpSchedules.entries()).reduce((acc, [adUnitPath, schedules]) => ({ ...acc, [adUnitPath]: schedules }), {}),
25
- pLastImpAdRequests: Array.from(positionLastImpressionNumberOfAdRequests.entries()).reduce((acc, [adUnitPath, numAdRequests]) => ({ ...acc, [adUnitPath]: numAdRequests }), {}),
26
- requestAds: numAdRequests
36
+ pLastImpAdRequests: Array.from(positionLastImpressionNumberOfAdRequests.entries()).reduce((acc, [adUnitPath, sessionNumAdRequests]) => ({
37
+ ...acc,
38
+ [adUnitPath]: sessionNumAdRequests
39
+ }), {}),
40
+ requestAds: sessionNumAdRequests
27
41
  };
28
42
  _window.sessionStorage.setItem(sessionStorageKey, JSON.stringify(data));
29
43
  }
30
- _window.localStorage.setItem(pageImpressionStorageKey, JSON.stringify(currentPageImpression));
44
+ const dataToStoreInLocalStorage = {
45
+ createdAt: existingLocalStorageData?.createdAt ?? Date.now(),
46
+ ts: Date.now(),
47
+ requestAds: totalNumAdRequests
48
+ };
49
+ _window.localStorage.setItem(localStorageKey, JSON.stringify(dataToStoreInLocalStorage));
31
50
  };
32
51
  const cap = (startTimestamp, config, intervalInMs, bid) => {
33
52
  const bidders = config.bidders;
@@ -69,7 +88,7 @@ export const createFrequencyCapping = (config, _window, now, logger) => {
69
88
  capPosition(now(), adUnitPath, config.conditions.pacingInterval);
70
89
  }
71
90
  if (config.conditions.pacingRequestAds) {
72
- positionLastImpressionNumberOfAdRequests.set(adUnitPath, numAdRequests);
91
+ positionLastImpressionNumberOfAdRequests.set(adUnitPath, sessionNumAdRequests);
73
92
  persist();
74
93
  }
75
94
  });
@@ -79,7 +98,7 @@ export const createFrequencyCapping = (config, _window, now, logger) => {
79
98
  if (storedData) {
80
99
  try {
81
100
  const parsedData = JSON.parse(storedData);
82
- numAdRequests = parsedData.requestAds;
101
+ sessionNumAdRequests = parsedData.requestAds;
83
102
  Object.entries(parsedData.bCaps).forEach(([adUnitBidderKey, schedules]) => {
84
103
  schedules?.forEach(schedule => {
85
104
  const [adUnitCode, bidder] = adUnitBidderKey.split(':');
@@ -145,8 +164,8 @@ export const createFrequencyCapping = (config, _window, now, logger) => {
145
164
  positionAdRequests.clear();
146
165
  },
147
166
  afterRequestAds() {
148
- numAdRequests++;
149
- currentPageImpression++;
167
+ sessionNumAdRequests++;
168
+ totalNumAdRequests++;
150
169
  persist();
151
170
  },
152
171
  updateAdUnitPaths(adUnitPathVariables) {
@@ -162,11 +181,12 @@ export const createFrequencyCapping = (config, _window, now, logger) => {
162
181
  .some(positionConfig => {
163
182
  return ((positionConfig.adUnitPath === adUnitPath &&
164
183
  positionConfig.conditions.delay &&
165
- numAdRequests <
184
+ sessionNumAdRequests <
166
185
  positionConfig.conditions.delay.minRequestAds - (isGamInst ? 1 : 0)) ||
167
186
  (positionConfig.conditions.pacingRequestAds &&
168
187
  positionLastImpressionNumberOfAdRequests.has(adUnitPath) &&
169
- numAdRequests - (positionLastImpressionNumberOfAdRequests.get(adUnitPath) ?? 0) <
188
+ sessionNumAdRequests -
189
+ (positionLastImpressionNumberOfAdRequests.get(adUnitPath) ?? 0) <
170
190
  positionConfig.conditions.pacingRequestAds.requestAds) ||
171
191
  (positionConfig.conditions.pacingInterval &&
172
192
  (positionImpSchedules.get(adUnitPath) ?? []).length >=
@@ -186,11 +206,11 @@ export const createFrequencyCapping = (config, _window, now, logger) => {
186
206
  return ((pacingInterval &&
187
207
  (bidderImpSchedules.get(`${slotId}:${bidder}`) ?? []).length >=
188
208
  pacingInterval.maxImpressions) ||
189
- (delay && numAdRequests < delay.minRequestAds));
209
+ (delay && sessionNumAdRequests < delay.minRequestAds));
190
210
  });
191
211
  },
192
- getCurrentPageImpressionFromLocalStorage() {
193
- return currentPageImpression;
212
+ getTotalNumAdRequests() {
213
+ return totalNumAdRequests;
194
214
  }
195
215
  };
196
216
  };
@@ -92,11 +92,11 @@ export const createGlobalAuctionContext = (window, logger, eventService, config
92
92
  interstitialChannel: () => {
93
93
  return interstitial?.interstitialChannel();
94
94
  },
95
- hasMinimumPageImpressions(minRequestAds) {
95
+ hasMinimumRequestAds(minRequestAds) {
96
96
  if (!frequencyCapping) {
97
97
  return true;
98
98
  }
99
- const currentPageImpression = frequencyCapping.getCurrentPageImpressionFromLocalStorage();
99
+ const currentPageImpression = frequencyCapping.getTotalNumAdRequests();
100
100
  return currentPageImpression >= minRequestAds;
101
101
  },
102
102
  configureStep() {
@@ -26,7 +26,7 @@ export class AdReload {
26
26
  this.initialized = true;
27
27
  };
28
28
  this.setupAdVisibilityService = (config, window, logger) => {
29
- this.adVisibilityService = new AdVisibilityService(new UserActivityService(window, config.userActivityLevelControl, logger), this.refreshIntervalMs, config.refreshIntervalMsOverrides ?? {}, false, !!config.disableAdVisibilityChecks, config.viewabilityOverrides ?? {}, window, logger);
29
+ this.adVisibilityService = new AdVisibilityService(new UserActivityService(window, config.userActivityLevelControl, logger), config.refreshIntervalMs ?? this.refreshIntervalMs, config.refreshIntervalMsOverrides ?? {}, false, !!config.disableAdVisibilityChecks, config.viewabilityOverrides ?? {}, window, logger);
30
30
  };
31
31
  this.setupSlotRenderListener = (config, slotsToMonitor, reloadAdSlotCallback, window, logger) => window.googletag.pubads().addEventListener('slotRenderEnded', renderEndedEvent => {
32
32
  const { slot: googleTagSlot, campaignId, advertiserId, companyIds, yieldGroupIds, isEmpty: slotIsEmpty } = renderEndedEvent;
@@ -34,7 +34,7 @@ export const createUtiq = () => {
34
34
  return Promise.resolve();
35
35
  }
36
36
  const minAdRequests = config.delay?.enabled && config.delay.minAdRequests ? config.delay.minAdRequests : 0;
37
- if (!context.auction__.hasMinimumPageImpressions(minAdRequests)) {
37
+ if (!context.auction__.hasMinimumRequestAds(minAdRequests)) {
38
38
  context.logger__.info('Utiq', `not enough ad requests to load Utiq. ${minAdRequests} required.`);
39
39
  return Promise.resolve();
40
40
  }
@@ -1,3 +1,3 @@
1
1
  export const packageJson = {
2
- version: '5.8.11'
2
+ version: '5.8.12'
3
3
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@highfivve/ad-tag",
3
- "version": "5.8.11",
3
+ "version": "5.8.12",
4
4
  "license": "Apache-2.0",
5
5
  "description": "An ad tag implementation called moli",
6
6
  "main": "./lib/index.js",