Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permutive RTD Module: fixes potential error when reading _pssps localStorage key #9474

Merged
merged 1 commit into from
Jan 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions modules/permutiveRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,19 @@ export function isPermutiveOnPage () {
* @return {Object}
*/
export function getSegments (maxSegs) {
const legacySegs = readSegments('_psegs').map(Number).filter(seg => seg >= 1000000).map(String)
const _ppam = readSegments('_ppam')
const _pcrprs = readSegments('_pcrprs')
const legacySegs = readSegments('_psegs', []).map(Number).filter(seg => seg >= 1000000).map(String)
const _ppam = readSegments('_ppam', [])
const _pcrprs = readSegments('_pcrprs', [])

const segments = {
ac: [..._pcrprs, ..._ppam, ...legacySegs],
rubicon: readSegments('_prubicons'),
appnexus: readSegments('_papns'),
gam: readSegments('_pdfps'),
ssp: readSegments('_pssps'),
rubicon: readSegments('_prubicons', []),
appnexus: readSegments('_papns', []),
gam: readSegments('_pdfps', []),
ssp: readSegments('_pssps', {
cohorts: [],
ssps: []
}),
}

for (const bidder in segments) {
Expand All @@ -338,15 +341,17 @@ export function getSegments (maxSegs) {

/**
* Gets an array of segment IDs from LocalStorage
* or returns an empty array
* or return the default value provided.
* @template A
* @param {string} key
* @return {string[]|number[]}
* @param {A} defaultValue
* @return {A}
*/
function readSegments (key) {
function readSegments (key, defaultValue) {
try {
return JSON.parse(storage.getDataFromLocalStorage(key) || '[]')
return JSON.parse(storage.getDataFromLocalStorage(key)) || defaultValue
} catch (e) {
return []
return defaultValue
}
}

Expand Down