Skip to content

Commit

Permalink
Adding org and publisher distinction in gtm datalayer
Browse files Browse the repository at this point in the history
  • Loading branch information
dlennox24 committed Apr 22, 2024
1 parent 9ff9e83 commit 6a6a0a0
Showing 1 changed file with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,38 @@ if (window.location.pathname.match(/^\/dataset\/.*$/g)) {
const getPublisher = () => {
const nodeList = document.querySelectorAll(`[property="dct:publisher"]`);
if (nodeList.length === 1) {
return nodeList[0]?.innerHTML.replaceAll(/<[^>]*>/gi, '');
return nodeList[0]?.innerHTML.replaceAll(/<[^>]*>/gi, '').trim();
}

// fall through that we may not need
return 'err - multiple or no publishers';
return 'err - multiple or no publisher';
};

const getOrgKeys = () => {
const idNodeList = document.querySelectorAll('div[class="org_type"] a');
const nameNodeList = document.querySelectorAll('div[class="org_type"] h1[class="heading"]');
const keys = {
organizationId: 'err - multiple or no organization',
organizationName: 'err - multiple or no organization',
};

if (idNodeList.length === 1) {
keys.organizationId = idNodeList[0]?.getAttribute('href').split('/organization/').pop();
}

if (nameNodeList.length === 1) {
keys.organizationName = nameNodeList[0]?.innerHTML.trim();
}

return keys;
};

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

0 comments on commit 6a6a0a0

Please sign in to comment.