From 753386465ce872f5048f032f085625b2f4a971fb Mon Sep 17 00:00:00 2001 From: Daniel Lennox Date: Tue, 9 Apr 2024 14:09:05 -0600 Subject: [PATCH 1/3] Adding custom gtm js file with basic dataset publisher pull --- .../scripts/gtmCustomizations.js | 18 ++++++++++++++++++ .../fanstatic_library/webassets.yml | 1 + 2 files changed, 19 insertions(+) create mode 100644 ckanext/datagovtheme/fanstatic_library/scripts/gtmCustomizations.js diff --git a/ckanext/datagovtheme/fanstatic_library/scripts/gtmCustomizations.js b/ckanext/datagovtheme/fanstatic_library/scripts/gtmCustomizations.js new file mode 100644 index 00000000..42305af5 --- /dev/null +++ b/ckanext/datagovtheme/fanstatic_library/scripts/gtmCustomizations.js @@ -0,0 +1,18 @@ +// 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, ''); + } + + // fall through that we may not need + return 'err - multiple or no publishers'; + }; + + window.dataLayer = window.dataLayer || []; + window.dataLayer.push({ + event: 'new_view', + agency: getPublisher(), + }); +} diff --git a/ckanext/datagovtheme/fanstatic_library/webassets.yml b/ckanext/datagovtheme/fanstatic_library/webassets.yml index 32f01cd1..eea25755 100644 --- a/ckanext/datagovtheme/fanstatic_library/webassets.yml +++ b/ckanext/datagovtheme/fanstatic_library/webassets.yml @@ -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 From 9ff9e839e25179dd58572a64b09dc7e42191e81a Mon Sep 17 00:00:00 2001 From: Daniel Lennox Date: Mon, 15 Apr 2024 11:11:26 -0600 Subject: [PATCH 2/3] Tweaking datalayer script improve overwrite robustness --- .../fanstatic_library/scripts/gtmCustomizations.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ckanext/datagovtheme/fanstatic_library/scripts/gtmCustomizations.js b/ckanext/datagovtheme/fanstatic_library/scripts/gtmCustomizations.js index 42305af5..199be18a 100644 --- a/ckanext/datagovtheme/fanstatic_library/scripts/gtmCustomizations.js +++ b/ckanext/datagovtheme/fanstatic_library/scripts/gtmCustomizations.js @@ -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; + } } From 6a6a0a08f608045ac0b9ecf9a3888d7563a69eca Mon Sep 17 00:00:00 2001 From: Daniel Lennox Date: Mon, 22 Apr 2024 14:51:50 -0600 Subject: [PATCH 3/3] Adding org and publisher distinction in gtm datalayer --- .../scripts/gtmCustomizations.js | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/ckanext/datagovtheme/fanstatic_library/scripts/gtmCustomizations.js b/ckanext/datagovtheme/fanstatic_library/scripts/gtmCustomizations.js index 199be18a..52d78d7e 100644 --- a/ckanext/datagovtheme/fanstatic_library/scripts/gtmCustomizations.js +++ b/ckanext/datagovtheme/fanstatic_library/scripts/gtmCustomizations.js @@ -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, };