Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 2 additions & 8 deletions website/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,8 @@ function createAposConfig() {
},
},
},
// GTM setup and dependencies
'@apostrophecms/seo': {
options: {
googleTagManager: {
id: process.env.GOOGLE_TAG_MANAGER_ID,
},
},
},
// Enable local SEO module with GTM integration
'@apostrophecms/seo': {},
'@apostrophecms/global': {},
// Make getEnv function available to templates
'@apostrophecms/template': {
Expand Down
39 changes: 13 additions & 26 deletions website/modules/@apostrophecms/seo/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
const gtmUtils = require('./lib/gtm-utils');

module.exports = {
improve: '@apostrophecms/seo',
init(self) {
// Ensure SEO components are injected into the template
self.apos.template.prepend('body', '@apostrophecms/seo:tagManagerBody');
self.apos.template.append('head', '@apostrophecms/seo:tagManagerHead');
self.apos.template.prepend('head', '@apostrophecms/seo:metaHead');
options: {
googleTagManager: {
id: process.env.GOOGLE_TAG_MANAGER_ID,
},
},
components(self) {
init(self) {
/*
* Resolve GTM ID from global override or module options and validate it
* SEO components are called directly from layout.html template
* No automatic injection needed to avoid duplication
*/
const sanitizeGtmId = (id) => {
const value = String(id || '').trim();
if (/^gtm-[\da-z]+$/iu.test(value)) {
return value.toUpperCase();
}
return '';
};
const resolveGtmId = (req) => {
const fromGlobal = req?.data?.global?.seoGoogleTagManager;
const fromOptions = self.options?.googleTagManager?.id;
const candidate =
(fromGlobal && String(fromGlobal).trim()) ||
(fromOptions && String(fromOptions).trim());
return sanitizeGtmId(candidate);
};

},
components(self) {
return {
metaHead(req, data) {
// Only on front-end page requests
Expand All @@ -38,7 +25,7 @@ module.exports = {
if (!req?.data?.page) {
return {};
}
const gtmId = resolveGtmId(req);
const gtmId = gtmUtils.resolveGtmId(req, self.options);
if (gtmId) {
return { gtmId };
}
Expand All @@ -48,7 +35,7 @@ module.exports = {
if (!req?.data?.page) {
return {};
}
const gtmId = resolveGtmId(req);
const gtmId = gtmUtils.resolveGtmId(req, self.options);
if (gtmId) {
return { gtmId };
}
Expand Down
21 changes: 21 additions & 0 deletions website/modules/@apostrophecms/seo/lib/gtm-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const sanitizeGtmId = function (id) {
const value = String(id || '')
.trim()
.toUpperCase();
// Strict GTM container format, e.g., "GTM-XXXXXXX"
if (/^GTM-[\dA-Z]+$/u.test(value)) {
return value;
}
return '';
};

const resolveGtmId = function (req, options) {
const fromGlobal = req?.data?.global?.seoGoogleTagManager;
const fromOptions = options?.googleTagManager?.id;
const candidate =
(fromGlobal && String(fromGlobal).trim()) ||
(fromOptions && String(fromOptions).trim());
return sanitizeGtmId(candidate);
};

module.exports = { sanitizeGtmId, resolveGtmId };
1 change: 0 additions & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"dependencies": {
"@apostrophecms/form": "^1.4.2",
"@apostrophecms/import-export": "^3.2.0",
"@apostrophecms/seo": "^1.3.1",
"@barba/core": "^2.10.3",
"abort-controller": "^3.0.0",
"apostrophe": "^4.17.0",
Expand Down
2 changes: 2 additions & 0 deletions website/views/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
{# ✅ Add favicon in the <head> section #}
{% block extraHead %}
<link rel="icon" href="{{ apos.asset.url('/modules/asset/favicon/favicon.ico') }}" type="image/x-icon">
{% component '@apostrophecms/seo:tagManagerHead' with data %}
{% endblock %}

{% block beforeMain %}
{% component '@apostrophecms/seo:tagManagerBody' with data %}
<a class="sr-only" href="#main">Skip to content</a>
<div
class="bp-wrapper relative"
Expand Down
Loading