From 1c40c211f559a60127c9c94604733d10147308fd Mon Sep 17 00:00:00 2001 From: Rishabh Date: Mon, 20 Sep 2021 15:11:54 +0530 Subject: [PATCH] Added new alpha analytics script for capturing events refs https://github.com/TryGhost/Team/issues/1063 refs https://github.com/TryGhost/Team/issues/1062 Member activity is a labs alpha feature which aims at capturing member events for site owner if switched on. The event metadata captures the site page/post where the event originates from, and the post/page id is included as content of new ghost analytics meta tag. This change sets up new analytics script in portal if analytics is switched on, and fires post/page view event when a valid analytics id is found. --- ghost/portal/src/analytics.js | 22 ++++++++++++++++++++++ ghost/portal/src/index.js | 14 ++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 ghost/portal/src/analytics.js diff --git a/ghost/portal/src/analytics.js b/ghost/portal/src/analytics.js new file mode 100644 index 0000000000..db921b37dc --- /dev/null +++ b/ghost/portal/src/analytics.js @@ -0,0 +1,22 @@ +import setupGhostApi from './utils/api'; + +function sendEntryViewEvent({analyticsId, api}) { + if (analyticsId) { + api.analytics.pushEvent({ + event_name: 'entry_view', + member_id: '', + member_status: '', + entry_id: analyticsId, + source_url: window.location.href + }); + } +} + +function setupAnalytics({siteUrl, analyticsId}) { + const GhostApi = setupGhostApi({siteUrl}); + // Fire page/post view event + sendEntryViewEvent({analyticsId, api: GhostApi}); + return {}; +} + +export default setupAnalytics; diff --git a/ghost/portal/src/index.js b/ghost/portal/src/index.js index de1209b610..6585c71bd2 100644 --- a/ghost/portal/src/index.js +++ b/ghost/portal/src/index.js @@ -2,6 +2,7 @@ import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; +import setupAnalytics from './analytics'; const ROOT_DIV_ID = 'ghost-portal-root'; @@ -30,15 +31,24 @@ function handleTokenUrl() { } } -function setup() { +function setupAnalyticsScript({siteUrl}) { + const analyticsTag = document.querySelector('meta[name=ghost-analytics-id]'); + const analyticsId = analyticsTag?.content; + if (siteUrl && analyticsTag) { + setupAnalytics({siteUrl, analyticsId}); + } +} + +function setup({siteUrl}) { addRootDiv(); handleTokenUrl(); + setupAnalyticsScript({siteUrl}); } function init() { const customSiteUrl = getSiteUrl(); const siteUrl = customSiteUrl || window.location.origin; - setup(); + setup({siteUrl}); ReactDOM.render(