Skip to content

Commit

Permalink
Change slots to type Object.
Browse files Browse the repository at this point in the history
  • Loading branch information
from20020516 committed Sep 28, 2021
1 parent b3c2d38 commit 476ae68
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
34 changes: 14 additions & 20 deletions modules/fluctAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,26 +228,20 @@ let fluctAnalyticsAdapter = Object.assign(
});

/**
* @typedef {Object} Slot
* @property {string} code
* @property {string} path
*/

/**
* GPT slotから共通のpathを持つadUnitCodeを返す
* @param {Array<Slot>} slots
* GPT slotから共通のpathを持つ、browsi_ad_ではないのadUnitCodeを返す
* @param {Object} slots
* @param {string} adUnitCode
* @returns {string}
*/
export const getAdUnitCodeBeforeReplication = (slots, adUnitCode) => {
/** @type {Slot} */
const slot = find(slots, slot => {
/** @type {string} */
const path = slots[find(Object.keys(slots), slot => {
/** @type {string|null} @example browsi_ad_0_ai_1_rc_ */
const browsiPrefix = adUnitCode.match(/^browsi_.*_(?=\d*$)/g)?.[0]
return slot.code === adUnitCode
|| slot.code.match(new RegExp(`^${browsiPrefix}`, 'g'))?.[0]
})
return find(slots, _slot => _slot.path === slot.path && !isBrowsiDivId(_slot.code))?.code ?? adUnitCode
const browsiPrefix = adUnitCode.match(/^browsi_ad_.*_(?=\d*$)/g)?.[0]
return slot === adUnitCode
|| slot.match(new RegExp(`^${browsiPrefix}`, 'g'))?.[0]
})]
return find(Object.keys(slots), slot => !isBrowsiDivId(slots[slot]) && slots[slot] === path) ?? adUnitCode
}

/**
Expand All @@ -257,11 +251,11 @@ export const getAdUnitCodeBeforeReplication = (slots, adUnitCode) => {
const modifyBrowsiAdUnit = (adUnit) => {
if (!isBrowsiDivId(adUnit.code)) return adUnit
// e.g.
// [
// [{ code: 'div-gpt-ad-1629864618640-0', path: '/62532913/p_fluctmagazine_320x50_surface_15377' }]
// [{ code: 'browsi_ad_0_ai_1_rc_0', path: '/62532913/p_fluctmagazine_320x50_surface_15377' }]
// ]
const slots = window.googletag.pubads().getSlots().map(slot => ({ code: slot.getSlotElementId(), path: slot.getAdUnitPath() }))
// {
// 'div-gpt-ad-1629864618640-0': '/62532913/p_fluctmagazine_320x50_surface_15377',
// 'browsi_ad_0_ai_1_rc_0': '/62532913/p_fluctmagazine_320x50_surface_15377'
// }
const slots = googletag.pubads().getSlots().reduce((prev, slot) => Object.assign(prev, { [slot.getSlotElementId()]: slot.getAdUnitPath() }), {})
return {
...adUnit,
adUnitCode: getAdUnitCodeBeforeReplication(slots, adUnit.code),
Expand Down
16 changes: 8 additions & 8 deletions test/spec/modules/fluctAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ import {
} from 'modules/currency.js';

describe('正規表現にマッチしている', () => {
const slots = [
{ code: 'div-gpt-ad-1629864618640-0', path: '/62532913/p_fluctmagazine_320x50_surface_15377' },
{ code: 'browsi_ad_0_ai_1_rc_0', path: '/62532913/p_fluctmagazine_320x50_surface_15377' }
]
const slots = {
'div-gpt-ad-1629864618640-0': '/62532913/p_fluctmagazine_320x50_surface_15377',
'browsi_ad_0_ai_1_rc_0': '/62532913/p_fluctmagazine_320x50_surface_15377'
}

it('browsi枠のリフレッシュ回数を取得できる', () => {
const browsiAdUnit = getBrowsiRefreshCount(slots[1].code)
const browsiAdUnit = getBrowsiRefreshCount('browsi_ad_0_ai_1_rc_0')
expect(browsiAdUnit).to.equal('0')
})

it('browsi枠ではないためリフレッシュ回数を取得できない', () => {
const browsiAdUnit = getBrowsiRefreshCount(slots[0].code)
const browsiAdUnit = getBrowsiRefreshCount('div-gpt-ad-1629864618640-0')
expect(browsiAdUnit).to.equal(undefined)
})

it('browsi枠codeから複製前の枠codeを取得できる', () => {
const adUnitCode = getAdUnitCodeBeforeReplication(slots, slots[1].code)
expect(adUnitCode).to.equal(slots[0].code)
const adUnitCode = getAdUnitCodeBeforeReplication(slots, 'browsi_ad_0_ai_1_rc_0')
expect(adUnitCode).to.equal('div-gpt-ad-1629864618640-0')
})
})

Expand Down

0 comments on commit 476ae68

Please sign in to comment.