From 718e932b44dde8c78d876c1e26480af7883a5c8a Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 17 Jan 2018 11:18:05 -0500 Subject: [PATCH] FEATURE: plugins can send more data to Google Tag Manager --- .../initializers/page-tracking.js.es6 | 12 ++--------- .../discourse/lib/page-tracker.js.es6 | 20 +++++++++++++++++++ .../discourse/lib/plugin-api.js.es6 | 17 +++++++++++++++- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 b/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 index ec1f3a446dc..acc8573d203 100644 --- a/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 +++ b/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 @@ -1,5 +1,5 @@ import { cleanDOM } from 'discourse/lib/clean-dom'; -import { startPageTracking } from 'discourse/lib/page-tracker'; +import { startPageTracking, googleTagManagerPageChanged } from 'discourse/lib/page-tracker'; import { viewTrackingRequired } from 'discourse/lib/ajax'; export default { @@ -35,15 +35,7 @@ export default { // And Google Tag Manager too if (typeof window.dataLayer !== 'undefined') { - appEvents.on('page:changed', data => { - window.dataLayer.push({ - 'event': 'virtualPageView', - 'page': { - 'title': data.title, - 'url': data.url - } - }); - }); + appEvents.on('page:changed', googleTagManagerPageChanged); } } }; diff --git a/app/assets/javascripts/discourse/lib/page-tracker.js.es6 b/app/assets/javascripts/discourse/lib/page-tracker.js.es6 index e6bd2c79e1b..77daeedce1a 100644 --- a/app/assets/javascripts/discourse/lib/page-tracker.js.es6 +++ b/app/assets/javascripts/discourse/lib/page-tracker.js.es6 @@ -38,3 +38,23 @@ export function startPageTracking(router, appEvents) { }); _started = true; } + +const _gtmPageChangedCallbacks = []; + +export function addGTMPageChangedCallback(callback) { + _gtmPageChangedCallbacks.push(callback); +} + +export function googleTagManagerPageChanged(data) { + let gtmData = { + 'event': 'virtualPageView', + 'page': { + 'title': data.title, + 'url': data.url + } + }; + + _.each(_gtmPageChangedCallbacks, callback => callback(gtmData)); + + window.dataLayer.push(gtmData); +} diff --git a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 index 1ef62e7cec1..b8377be96f6 100644 --- a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 +++ b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 @@ -22,9 +22,10 @@ import { registerIconRenderer, replaceIcon } from 'discourse-common/lib/icon-lib import { addNavItem } from 'discourse/models/nav-item'; import { replaceFormatter } from 'discourse/lib/utilities'; import { modifySelectKit } from "select-kit/mixins/plugin-api"; +import { addGTMPageChangedCallback } from 'discourse/lib/page-tracker'; // If you add any methods to the API ensure you bump up this number -const PLUGIN_API_VERSION = '0.8.15'; +const PLUGIN_API_VERSION = '0.8.16'; class PluginApi { constructor(version, container) { @@ -619,6 +620,20 @@ class PluginApi { modifySelectKit(pluginApiKey) { return modifySelectKit(pluginApiKey); } + + /** + * + * Registers a function that can inspect and modify the data that + * will be sent to Google Tag Manager when a page changed event is triggered. + * + * Example: + * + * addGTMPageChangedCallback( gtmData => gtmData.locale = I18n.currentLocale() ) + * + */ + addGTMPageChangedCallback(fn) { + addGTMPageChangedCallback(fn); + } } let _pluginv01;