From 2870da0e9e9639bd594d7dc570283a76e0701dbe Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 23 Jun 2019 22:57:04 -0400 Subject: [PATCH] feat: analytics code rendering --- dev/templates/master.pug | 4 ++ server/graph/resolvers/analytics.js | 1 + server/master.js | 1 + server/models/analytics.js | 47 +++++++++++++++++++ .../modules/analytics/azureinsights/code.yml | 10 ++++ .../analytics/azureinsights/definition.yml | 10 ---- server/modules/analytics/countly/code.yml | 27 +++++++++++ .../modules/analytics/countly/definition.yml | 27 ----------- server/modules/analytics/elasticapm/code.yml | 10 ++++ .../analytics/elasticapm/definition.yml | 10 ---- server/modules/analytics/fathom/code.yml | 16 +++++++ .../modules/analytics/fathom/definition.yml | 16 ------- server/modules/analytics/fullstory/code.yml | 18 +++++++ .../analytics/fullstory/definition.yml | 18 ------- server/modules/analytics/google/code.yml | 10 ++++ .../modules/analytics/google/definition.yml | 12 +---- server/modules/analytics/gtm/code.yml | 13 +++++ server/modules/analytics/gtm/definition.yml | 15 +----- server/modules/analytics/hotjar/code.yml | 12 +++++ .../modules/analytics/hotjar/definition.yml | 12 ----- server/views/master.pug | 4 ++ 21 files changed, 175 insertions(+), 118 deletions(-) create mode 100644 server/modules/analytics/azureinsights/code.yml create mode 100644 server/modules/analytics/countly/code.yml create mode 100644 server/modules/analytics/elasticapm/code.yml create mode 100644 server/modules/analytics/fathom/code.yml create mode 100644 server/modules/analytics/fullstory/code.yml create mode 100644 server/modules/analytics/google/code.yml create mode 100644 server/modules/analytics/gtm/code.yml create mode 100644 server/modules/analytics/hotjar/code.yml diff --git a/dev/templates/master.pug b/dev/templates/master.pug index cebdf93f..c6b6a246 100644 --- a/dev/templates/master.pug +++ b/dev/templates/master.pug @@ -67,7 +67,11 @@ html <% } %> <% } %> + != analyticsCode.head + block head body + != analyticsCode.bodyStart block body + != analyticsCode.bodyEnd diff --git a/server/graph/resolvers/analytics.js b/server/graph/resolvers/analytics.js index f8dc2821..50193705 100644 --- a/server/graph/resolvers/analytics.js +++ b/server/graph/resolvers/analytics.js @@ -44,6 +44,7 @@ module.exports = { return result }, {}) }).where('key', str.key) + await WIKI.cache.del('analytics') } return { responseResult: graphHelper.generateSuccess('Providers updated successfully') diff --git a/server/master.js b/server/master.js index 103d30ab..cda6451e 100644 --- a/server/master.js +++ b/server/master.js @@ -153,6 +153,7 @@ module.exports = async () => { company: WIKI.config.company } res.locals.langs = await WIKI.models.locales.getNavLocales({ cache: true }) + res.locals.analyticsCode = await WIKI.models.analytics.getCode({ cache: true }) next() }) diff --git a/server/models/analytics.js b/server/models/analytics.js index 6f1b8962..d2c8db55 100644 --- a/server/models/analytics.js +++ b/server/models/analytics.js @@ -93,4 +93,51 @@ module.exports = class Analytics extends Model { } } } + + static async getCode ({ cache = false } = {}) { + if (cache) { + const analyticsCached = await WIKI.cache.get('analytics') + if (analyticsCached) { + return analyticsCached + } + } + try { + const analyticsCode = { + head: '', + bodyStart: '', + bodyEnd: '' + } + const providers = await WIKI.models.analytics.getProviders(true) + + for (let provider of providers) { + const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/analytics', provider.key, 'code.yml'), 'utf8') + let code = yaml.safeLoad(def) + code.head = _.defaultTo(code.head, '') + code.bodyStart = _.defaultTo(code.bodyStart, '') + code.bodyEnd = _.defaultTo(code.bodyEnd, '') + + _.forOwn(provider.config, (value, key) => { + code.head = _.replace(code.head, `{{${key}}}`, value) + code.bodyStart = _.replace(code.bodyStart, `{{${key}}}`, value) + code.bodyEnd = _.replace(code.bodyEnd, `{{${key}}}`, value) + }) + + analyticsCode.head += code.head + analyticsCode.bodyStart += code.bodyStart + analyticsCode.bodyEnd += code.bodyEnd + } + + await WIKI.cache.set('analytics', analyticsCode, 300) + + return analyticsCode + } catch (err) { + WIKI.logger.warn('Error while getting analytics code: ', err) + return { + head: '', + bodyStart: '', + bodyEnd: '' + } + } + + } } diff --git a/server/modules/analytics/azureinsights/code.yml b/server/modules/analytics/azureinsights/code.yml new file mode 100644 index 00000000..82a3c441 --- /dev/null +++ b/server/modules/analytics/azureinsights/code.yml @@ -0,0 +1,10 @@ +head: | + diff --git a/server/modules/analytics/azureinsights/definition.yml b/server/modules/analytics/azureinsights/definition.yml index 4d7b39ef..ffa89e03 100644 --- a/server/modules/analytics/azureinsights/definition.yml +++ b/server/modules/analytics/azureinsights/definition.yml @@ -11,13 +11,3 @@ props: title: Instrumentation Key hint: Found in the Azure Portal in your Application Insights resource panel order: 1 -codeHead: | - diff --git a/server/modules/analytics/countly/code.yml b/server/modules/analytics/countly/code.yml new file mode 100644 index 00000000..e675ad00 --- /dev/null +++ b/server/modules/analytics/countly/code.yml @@ -0,0 +1,27 @@ +head: | + diff --git a/server/modules/analytics/countly/definition.yml b/server/modules/analytics/countly/definition.yml index 163ebef7..dd8a4f7f 100644 --- a/server/modules/analytics/countly/definition.yml +++ b/server/modules/analytics/countly/definition.yml @@ -16,30 +16,3 @@ props: title: Server URL hint: The Count.ly server to report to. e.g. https://us-example.count.ly order: 2 -codeHead: | - diff --git a/server/modules/analytics/elasticapm/code.yml b/server/modules/analytics/elasticapm/code.yml new file mode 100644 index 00000000..f6124142 --- /dev/null +++ b/server/modules/analytics/elasticapm/code.yml @@ -0,0 +1,10 @@ +head: | + + + diff --git a/server/modules/analytics/elasticapm/definition.yml b/server/modules/analytics/elasticapm/definition.yml index c3907812..0bd0bf74 100644 --- a/server/modules/analytics/elasticapm/definition.yml +++ b/server/modules/analytics/elasticapm/definition.yml @@ -24,13 +24,3 @@ props: hint: e.g. production/development/test default: '' order: 3 -codeHead: | - - - diff --git a/server/modules/analytics/fathom/code.yml b/server/modules/analytics/fathom/code.yml new file mode 100644 index 00000000..6ac14c85 --- /dev/null +++ b/server/modules/analytics/fathom/code.yml @@ -0,0 +1,16 @@ +head: | + + + diff --git a/server/modules/analytics/fathom/definition.yml b/server/modules/analytics/fathom/definition.yml index d193b9eb..ffa095e2 100644 --- a/server/modules/analytics/fathom/definition.yml +++ b/server/modules/analytics/fathom/definition.yml @@ -16,19 +16,3 @@ props: title: Site ID hint: The alphanumeric identifier of your site order: 2 -codeHead: | - - - diff --git a/server/modules/analytics/fullstory/code.yml b/server/modules/analytics/fullstory/code.yml new file mode 100644 index 00000000..cbfbade4 --- /dev/null +++ b/server/modules/analytics/fullstory/code.yml @@ -0,0 +1,18 @@ +head: | + diff --git a/server/modules/analytics/fullstory/definition.yml b/server/modules/analytics/fullstory/definition.yml index b76b6833..e950c157 100644 --- a/server/modules/analytics/fullstory/definition.yml +++ b/server/modules/analytics/fullstory/definition.yml @@ -11,21 +11,3 @@ props: title: Organization ID hint: A 5 alphanumeric identifier, e.g. XXXXX order: 1 -codeHead: | - diff --git a/server/modules/analytics/google/code.yml b/server/modules/analytics/google/code.yml new file mode 100644 index 00000000..9d621056 --- /dev/null +++ b/server/modules/analytics/google/code.yml @@ -0,0 +1,10 @@ +head: | + + + diff --git a/server/modules/analytics/google/definition.yml b/server/modules/analytics/google/definition.yml index a6907f9d..5a6b30b0 100644 --- a/server/modules/analytics/google/definition.yml +++ b/server/modules/analytics/google/definition.yml @@ -1,6 +1,6 @@ key: google title: Google Analytics -description: Google specializes in Internet-related services and products, which include online advertising technologies, search engine, cloud computing, software, and hardware. +description: Google Analytics is a web analytics service offered by Google that tracks and reports website traffic. author: requarks.io logo: https://static.requarks.io/logo/google-analytics.svg website: https://analytics.google.com/ @@ -11,13 +11,3 @@ props: title: Property Tracking ID hint: UA-XXXXXXX-X order: 1 -codeHead: | - - - diff --git a/server/modules/analytics/gtm/code.yml b/server/modules/analytics/gtm/code.yml new file mode 100644 index 00000000..b6d2737e --- /dev/null +++ b/server/modules/analytics/gtm/code.yml @@ -0,0 +1,13 @@ +head: | + + + +bodyStart: | + + + diff --git a/server/modules/analytics/gtm/definition.yml b/server/modules/analytics/gtm/definition.yml index 51fcb3b9..3500532b 100644 --- a/server/modules/analytics/gtm/definition.yml +++ b/server/modules/analytics/gtm/definition.yml @@ -1,6 +1,6 @@ key: gtm title: Google Tag Manager -description: Google specializes in Internet-related services and products, which include online advertising technologies, search engine, cloud computing, software, and hardware. +description: Google Tag Manager is a tag management system created by Google to manage JavaScript and HTML tags used for tracking and analytics on websites. author: requarks.io logo: https://static.requarks.io/logo/google-tag-manager.svg website: https://tagmanager.google.com @@ -11,16 +11,3 @@ props: title: Container Tracking ID hint: GTM-XXXXXXX order: 1 -codeHead: | - - - -codeBodyStart: | - - - diff --git a/server/modules/analytics/hotjar/code.yml b/server/modules/analytics/hotjar/code.yml new file mode 100644 index 00000000..b8386810 --- /dev/null +++ b/server/modules/analytics/hotjar/code.yml @@ -0,0 +1,12 @@ +head: | + + diff --git a/server/modules/analytics/hotjar/definition.yml b/server/modules/analytics/hotjar/definition.yml index f3a452b8..ef966bd2 100644 --- a/server/modules/analytics/hotjar/definition.yml +++ b/server/modules/analytics/hotjar/definition.yml @@ -11,15 +11,3 @@ props: title: Site ID hint: A numeric identifier of your site order: 1 -codeHead: | - - diff --git a/server/views/master.pug b/server/views/master.pug index 6014d3bf..6e582a9f 100644 --- a/server/views/master.pug +++ b/server/views/master.pug @@ -51,7 +51,11 @@ html + != analyticsCode.head + block head body + != analyticsCode.bodyStart block body + != analyticsCode.bodyEnd