Skip to content

Commit

Permalink
Tweaking datalayer script improve overwrite robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
dlennox24 committed Apr 15, 2024
1 parent 7533864 commit 9ff9e83
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ if (window.location.pathname.match(/^\/dataset\/.*$/g)) {
};

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
const publisherObjIndex = window.dataLayer.findIndex((o) => o.id === 'datasetPublisher');
const publisherObj = {
id: 'datasetPublisher',
event: 'new_view',
agency: getPublisher(),
});
dataSetName: document.querySelectorAll('h1[itemprop="name"]')[0]?.innerText || null,
uri: window.location.pathname,
};

if (publisherObjIndex === -1) {
window.dataLayer.push(publisherObj);
} else {
window.dataLayer[publisherObjIndex] = publisherObj;
}
}

0 comments on commit 9ff9e83

Please sign in to comment.