0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

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
This commit is contained in:
Fabien O'Carroll 2020-07-14 11:14:10 +02:00 committed by Fabien 'egg' O'Carroll
parent 48eca5f618
commit 23bf432514
4 changed files with 42 additions and 0 deletions

View file

@ -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(`
<amp-analytics type="gtag" data-credentials="include">
<script type="application/json">
{
"vars" : {
"gtag_id": "${ampGtagId}",
"config" : {
"${ampGtagId}": { "groups": "default" }
}
}
}
</script>
</amp-analytics>
`);
}
return new SafeString(components.join('\n'));
}
module.exports = ampComponents;

View file

@ -39,6 +39,10 @@ function ampComponents() {
components.push('<script async custom-element="amp-audio" src="https://cdn.ampproject.org/v0/amp-audio-0.1.js"></script>');
}
if (proxy.settingsCache.get('amp_gtag_id')) {
components.push('<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>');
}
return new SafeString(components.join('\n'));
}

View file

@ -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);
}

View file

@ -622,5 +622,6 @@
<p><a href="{{@site.url}}">Read more posts →</a></p>
<a class="powered" href="https://ghost.org" target="_blank" rel="noopener"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 156 156"><g fill="none" fill-rule="evenodd"><rect fill="#15212B" width="156" height="156" rx="27"/><g transform="translate(36 36)" fill="#F6F8FA"><path d="M0 71.007A4.004 4.004 0 014 67h26a4 4 0 014 4.007v8.986A4.004 4.004 0 0130 84H4a4 4 0 01-4-4.007v-8.986zM50 71.007A4.004 4.004 0 0154 67h26a4 4 0 014 4.007v8.986A4.004 4.004 0 0180 84H54a4 4 0 01-4-4.007v-8.986z"/><rect y="34" width="84" height="17" rx="4"/><path d="M0 4.007A4.007 4.007 0 014.007 0h41.986A4.003 4.003 0 0150 4.007v8.986A4.007 4.007 0 0145.993 17H4.007A4.003 4.003 0 010 12.993V4.007z"/><rect x="67" width="17" height="17" rx="4"/></g></g></svg> Published with Ghost</a></span>
</footer>
{{amp_analytics}}
</body>
</html>