From 8c2366bf56b182fb199f44ddd8f0d66d2419dcad Mon Sep 17 00:00:00 2001 From: Thomas Turrell-Croft Date: Wed, 15 Dec 2021 21:38:55 +0000 Subject: [PATCH] Add support for Google Analitics global site tag --- .../src/lib/plugins-google-analytics.ts | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/libs/plugins/google-analytics/src/lib/plugins-google-analytics.ts b/libs/plugins/google-analytics/src/lib/plugins-google-analytics.ts index 0829191eb..a7dbf6e64 100644 --- a/libs/plugins/google-analytics/src/lib/plugins-google-analytics.ts +++ b/libs/plugins/google-analytics/src/lib/plugins-google-analytics.ts @@ -1,4 +1,4 @@ -import { registerPlugin, getMyConfig } from '@scullyio/scully'; +import { getMyConfig, registerPlugin } from '@scullyio/scully'; export const GoogleAnalytics = 'googleAnalytics'; @@ -8,9 +8,32 @@ export const googleAnalyticsPlugin = async (html: string): Promise => { if (!googleAnalyticsConfig) { throw new Error('googleAnalytics plugin missing Global Site Tag'); } - const siteTag: string = googleAnalyticsConfig['globalSiteTag']; - const googleAnalyticsScript = ` + const googleAnalyticsScript = getSnippet(googleAnalyticsConfig['globalSiteTag']); + + return html.replace(/<\/head/i, `${googleAnalyticsScript} []; + +registerPlugin('postProcessByHtml', GoogleAnalytics, googleAnalyticsPlugin, validator); + +function getSnippet(siteTag: string) { + if (siteTag.startsWith('G-')) { + return ` + + + +`; + } + + return ` - + `; - - return html.replace(/<\/head/i, `${googleAnalyticsScript} []; - -registerPlugin('postProcessByHtml', GoogleAnalytics, googleAnalyticsPlugin, validator); +}