1+ const gtmUtils = require ( './lib/gtm-utils' ) ;
2+
13module . exports = {
2- improve : '@apostrophecms/seo' ,
3- init ( self ) {
4- // Ensure SEO components are injected into the template
5- self . apos . template . prepend ( 'body' , '@apostrophecms/seo:tagManagerBody' ) ;
6- self . apos . template . append ( 'head' , '@apostrophecms/seo:tagManagerHead' ) ;
7- self . apos . template . prepend ( 'head' , '@apostrophecms/seo:metaHead' ) ;
4+ options : {
5+ googleTagManager : {
6+ id : process . env . GOOGLE_TAG_MANAGER_ID ,
7+ } ,
88 } ,
9+ // No init hook required (layout renders components explicitly).
910 components ( self ) {
10- /*
11- * Resolve GTM ID from global override or module options and validate it
12- */
13- const sanitizeGtmId = ( id ) => {
14- const value = String ( id || '' ) . trim ( ) ;
15- if ( / ^ g t m - [ \d a - z ] + $ / iu. test ( value ) ) {
16- return value . toUpperCase ( ) ;
17- }
18- return '' ;
19- } ;
20- const resolveGtmId = ( req ) => {
21- const fromGlobal = req ?. data ?. global ?. seoGoogleTagManager ;
22- const fromOptions = self . options ?. googleTagManager ?. id ;
23- const candidate =
24- ( fromGlobal && String ( fromGlobal ) . trim ( ) ) ||
25- ( fromOptions && String ( fromOptions ) . trim ( ) ) ;
26- return sanitizeGtmId ( candidate ) ;
27- } ;
28-
11+ const getGtmId = ( req ) => gtmUtils . resolveGtmId ( req , self . options ) ;
2912 return {
30- metaHead ( req , data ) {
31- // Only on front-end page requests
32- if ( ! req ?. data ?. page ) {
33- return { } ;
34- }
35- return { } ;
36- } ,
3713 tagManagerBody ( req , data ) {
3814 if ( ! req ?. data ?. page ) {
3915 return { } ;
4016 }
41- const gtmId = resolveGtmId ( req ) ;
17+ const gtmId = getGtmId ( req ) ;
4218 if ( gtmId ) {
4319 return { gtmId } ;
4420 }
@@ -48,7 +24,7 @@ module.exports = {
4824 if ( ! req ?. data ?. page ) {
4925 return { } ;
5026 }
51- const gtmId = resolveGtmId ( req ) ;
27+ const gtmId = getGtmId ( req ) ;
5228 if ( gtmId ) {
5329 return { gtmId } ;
5430 }
0 commit comments