Skip to content

Commit

Permalink
enhance(analytics): analytics tracking for narrative charts
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed Jan 13, 2025
1 parent 6287939 commit 775d64b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 deletions.
24 changes: 20 additions & 4 deletions packages/@ourworldindata/grapher/src/core/Grapher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ export class Grapher

chartViewInfo?: Pick<
ChartViewInfo,
"parentChartSlug" | "queryParamsForParentChart"
"name" | "parentChartSlug" | "queryParamsForParentChart"
> = undefined

selection =
Expand Down Expand Up @@ -3008,7 +3008,10 @@ export class Grapher
ref={this.base}
className={containerClasses}
style={containerStyle}
data-grapher-url={this.canonicalUrl}
data-grapher-url={JSON.stringify({
grapherUrl: this.canonicalUrl,
chartViewName: this.chartViewInfo?.name,
})}
>
{this.commandPalette}
{this.uncaughtError ? this.renderError() : this.renderReady()}
Expand Down Expand Up @@ -3102,9 +3105,22 @@ export class Grapher
if (entry.isIntersecting) {
this.hasBeenVisible = true

if (this.slug && !this.hasLoggedGAViewEvent) {
this.analytics.logGrapherView(this.slug)
if (!this.hasLoggedGAViewEvent) {
this.hasLoggedGAViewEvent = true

if (this.chartViewInfo) {
this.analytics.logGrapherView(
this.chartViewInfo.parentChartSlug,
{
chartViewName:
this.chartViewInfo.name,
}
)
this.hasLoggedGAViewEvent = true
} else if (this.slug) {
this.analytics.logGrapherView(this.slug)
this.hasLoggedGAViewEvent = true
}
}
}

Expand Down
46 changes: 35 additions & 11 deletions packages/@ourworldindata/grapher/src/core/GrapherAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ interface GAEvent {
eventTarget?: string
grapherPath?: string
grapherView?: string // specifies a view in a multi-dim data page
chartViewName?: string // specifies the name of a chart view / narrative chart
explorerPath?: string
explorerView?: string
}
Expand All @@ -76,11 +77,15 @@ export class GrapherAnalytics {
private version: string // Ideally the Git hash commit
private isDev: boolean

logGrapherView(slug: string, view?: Record<string, string>): void {
logGrapherView(
slug: string,
ctx?: { view?: Record<string, string>; chartViewName?: string }
): void {
this.logToGA({
event: EventCategory.GrapherView,
grapherPath: `/grapher/${slug}`,
grapherView: view ? JSON.stringify(view) : undefined,
grapherView: ctx?.view ? JSON.stringify(ctx.view) : undefined,
chartViewName: ctx?.chartViewName,
})
}

Expand Down Expand Up @@ -126,19 +131,23 @@ export class GrapherAnalytics {

logGrapherClick(
action: string = "unknown-action",
label?: string,
grapherUrl?: string
ctx: {
label?: string
grapherUrl?: string
chartViewName?: string
}
): void {
// GA4 trims metadata fields down to 100 characters, so we want to be concise and only send
// the pathname, e.g. `/grapher/life-expectancy` or `/explorers/migration`
const grapherUrlObj =
grapherUrl !== undefined ? new URL(grapherUrl) : undefined
ctx.grapherUrl !== undefined ? new URL(ctx.grapherUrl) : undefined

this.logToGA({
event: EventCategory.GrapherClick,
eventAction: action,
eventTarget: label,
eventTarget: ctx.label,
grapherPath: grapherUrlObj?.pathname,
chartViewName: ctx.chartViewName,
})
}

Expand Down Expand Up @@ -184,17 +193,32 @@ export class GrapherAnalytics {
)
if (!trackedElement) return

const grapherUrl = trackedElement
const grapherUrlRaw = trackedElement
.closest(`[${dataGrapherUrlAttr}]`)
?.getAttribute(dataGrapherUrlAttr)

if (grapherUrl)
if (grapherUrlRaw) {
let grapherUrlObj:
| {
grapherUrl: string
chartViewName: string
}
| undefined
try {
grapherUrlObj = JSON.parse(grapherUrlRaw)
} catch (e) {
console.warn("failed to parse grapherUrl", e)
}

this.logGrapherClick(
trackedElement.getAttribute(dataTrackAttr) || undefined,
trackedElement.innerText,
grapherUrl
{
label: trackedElement.innerText,
grapherUrl: grapherUrlObj?.grapherUrl,
chartViewName: grapherUrlObj?.chartViewName,
}
)
else
} else
this.logSiteClick(
trackedElement.getAttribute(dataTrackAttr) || undefined,
trackedElement.innerText
Expand Down

0 comments on commit 775d64b

Please sign in to comment.