From d3578756d481f46900a5d2dc5c5c6bfc5326057e Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Thu, 16 Mar 2023 00:52:39 +0000 Subject: [PATCH] Add event tracking for topic load --- src/components/Analytics/analytics.ts | 6 ++++++ src/components/TopicOverlay/topic.js | 21 +++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/components/Analytics/analytics.ts b/src/components/Analytics/analytics.ts index b103a1937..f8ba9bc00 100644 --- a/src/components/Analytics/analytics.ts +++ b/src/components/Analytics/analytics.ts @@ -2,6 +2,8 @@ export {}; declare global { interface Window { + // To selectively enable/disable debug logs + __DEBUG__: boolean; gtag: any; fireEvent: (props: GAEventType) => void; } @@ -27,6 +29,10 @@ window.fireEvent = (props: GAEventType) => { return; } + if (window.__DEBUG__) { + console.log('Analytics event fired', props); + } + window.gtag('event', action, { event_category: category, event_label: label, diff --git a/src/components/TopicOverlay/topic.js b/src/components/TopicOverlay/topic.js index 1ebd0c59e..dfa927952 100644 --- a/src/components/TopicOverlay/topic.js +++ b/src/components/TopicOverlay/topic.js @@ -177,7 +177,16 @@ export class Topic { this.activeTopicId = topicId; this.resetDOM(); - this.renderTopicFromUrl(`/best-practices/${bestPracticeId}/${topicId.replaceAll(':', '/')}`); + + const topicUrl = `/best-practices/${bestPracticeId}/${topicId.replaceAll(':', '/')}`; + + window.fireEvent({ + category: `BestPracticesClick`, + action: `Load Topic`, + label: topicUrl, + }); + + this.renderTopicFromUrl(topicUrl).then(() => null); } handleRoadmapTopicClick(e) { @@ -192,7 +201,15 @@ export class Topic { this.activeTopicId = topicId; this.resetDOM(); - this.renderTopicFromUrl(`/${roadmapId}/${topicId.replaceAll(':', '/')}`); + const topicUrl = `/${roadmapId}/${topicId.replaceAll(':', '/')}`; + + window.fireEvent({ + category: `RoadmapClick`, + action: `Load Topic`, + label: topicUrl, + }); + + this.renderTopicFromUrl(topicUrl).then(() => null); } querySvgElementsByTopicId(topicId) {