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

4567 - Adding custom gtm js file with basic dataset publisher pull #193

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Only execute this code on dataset pages
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, '').trim();
}

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 === 'customDatasetKeys');
const publisherObj = {
id: 'customDatasetKeys',
event: 'new_view',
publisher: getPublisher(),
...getOrgKeys(),
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;
}
}
1 change: 1 addition & 0 deletions ckanext/datagovtheme/fanstatic_library/webassets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ js:
- scripts/hideMaxListItem.js
- scripts/sorting.js
- scripts/vendor/uswds.js
- scripts/gtmCustomizations.js

styles:
# This name is used in development/debug mode, must match the css file in test_datagovtheme.py
Expand Down
Loading