From 23bf432514c67ac7714cb51846f4ffc8353afd41 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 14 Jul 2020 11:14:10 +0200 Subject: [PATCH] Updated AMP output to include Google Analytics refs #11980 This adds a new handlebars helper {{amp_analytics}} which outputs the GA configuration script if the amp_gtag_id setting is present. We also update {{amp_components}} to output the GA script in the head if the setting is present --- .../apps/amp/lib/helpers/amp_analytics.js | 35 +++++++++++++++++++ .../apps/amp/lib/helpers/amp_components.js | 4 +++ core/frontend/apps/amp/lib/helpers/index.js | 2 ++ core/frontend/apps/amp/lib/views/amp.hbs | 1 + 4 files changed, 42 insertions(+) create mode 100644 core/frontend/apps/amp/lib/helpers/amp_analytics.js diff --git a/core/frontend/apps/amp/lib/helpers/amp_analytics.js b/core/frontend/apps/amp/lib/helpers/amp_analytics.js new file mode 100644 index 0000000000..cc757104ad --- /dev/null +++ b/core/frontend/apps/amp/lib/helpers/amp_analytics.js @@ -0,0 +1,35 @@ +// # Amp Components Helper +// Usage: `{{amp_analytics}}` +// +// Outputs inline scripts used for analytics + +const proxy = require('../../../../services/proxy'); + +const SafeString = proxy.SafeString; +const settingsCache = proxy.settingsCache; + +function ampComponents() { + let components = []; + + const ampGtagId = settingsCache.get('amp_gtag_id'); + if (ampGtagId) { + components.push(` + + + + `); + } + + return new SafeString(components.join('\n')); +} + +module.exports = ampComponents; diff --git a/core/frontend/apps/amp/lib/helpers/amp_components.js b/core/frontend/apps/amp/lib/helpers/amp_components.js index 149f6f140f..017c68f0a1 100644 --- a/core/frontend/apps/amp/lib/helpers/amp_components.js +++ b/core/frontend/apps/amp/lib/helpers/amp_components.js @@ -39,6 +39,10 @@ function ampComponents() { components.push(''); } + if (proxy.settingsCache.get('amp_gtag_id')) { + components.push(''); + } + return new SafeString(components.join('\n')); } diff --git a/core/frontend/apps/amp/lib/helpers/index.js b/core/frontend/apps/amp/lib/helpers/index.js index da7567c5a1..940c8e1bde 100644 --- a/core/frontend/apps/amp/lib/helpers/index.js +++ b/core/frontend/apps/amp/lib/helpers/index.js @@ -6,6 +6,8 @@ function registerAmpHelpers(ghost) { ghost.helpers.register('amp_components', require('./amp_components')); + ghost.helpers.register('amp_analytics', require('./amp_analytics')); + // we use the {{ghost_head}} helper, but call it {{amp_ghost_head}}, so it's consistent ghost.helpers.registerAsync('amp_ghost_head', ghostHead); } diff --git a/core/frontend/apps/amp/lib/views/amp.hbs b/core/frontend/apps/amp/lib/views/amp.hbs index 14dd1af297..597453ec13 100644 --- a/core/frontend/apps/amp/lib/views/amp.hbs +++ b/core/frontend/apps/amp/lib/views/amp.hbs @@ -622,5 +622,6 @@

Read more posts →

Published with Ghost + {{amp_analytics}} \ No newline at end of file