Skip to content

Commit

Permalink
Add and expand analytics
Browse files Browse the repository at this point in the history
Tracking subscriptions only on the click of the submit button is not
accurate, because the user might click the button also with
invalid/empty input.
  • Loading branch information
rakyi committed Jul 24, 2024
1 parent bd93754 commit a2f9d9d
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 16 deletions.
9 changes: 9 additions & 0 deletions packages/@ourworldindata/grapher/src/core/GrapherAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export enum EventCategory {
SiteSearchClick = "owid.site_search_click",
SiteSearchFilterClick = "owid.site_search_filter_click",
SiteInstantSearchClick = "owid.site_instantsearch_click",
SiteFormSubmit = "owid.site_form_submit",
DetailOnDemand = "owid.detail_on_demand",
}

Expand Down Expand Up @@ -126,6 +127,14 @@ export class GrapherAnalytics {
})
}

logSiteFormSubmit(action: string, label?: string): void {
this.logToGA({
event: EventCategory.SiteFormSubmit,
eventAction: action,
eventTarget: label,
})
}

logKeyboardShortcut(shortcut: string, combo: string): void {
this.logToGA({
event: EventCategory.KeyboardShortcut,
Expand Down
2 changes: 2 additions & 0 deletions site/DataInsightsNewsletterBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from "react"
import { useState, useEffect } from "react"
import Cookies from "js-cookie"
import cx from "classnames"
import { NewsletterSubscriptionContext } from "./newsletter.js"
import DataInsightsNewsletter from "./gdocs/components/DataInsightsNewsletter.js"

const COOKIE_NAME = "is_data_insights_newsletter_banner_hidden"
Expand All @@ -28,6 +29,7 @@ export default function DataInsightsNewsletterBanner() {
className={cx("data-insights-newsletter-banner", {
"data-insights-newsletter-banner--visible": isVisible,
})}
context={NewsletterSubscriptionContext.FloatingDataInsights}
onClose={handleOnClose}
/>
)
Expand Down
16 changes: 10 additions & 6 deletions site/NewsletterSubscription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ import { faTimes, faEnvelopeOpenText } from "@fortawesome/free-solid-svg-icons"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
import { SiteAnalytics } from "./SiteAnalytics.js"
import { TextInput } from "@ourworldindata/components"
import { NewsletterSubscriptionContext } from "./newsletter.js"

const analytics = new SiteAnalytics()

export enum NewsletterSubscriptionContext {
Homepage = "homepage",
Floating = "floating",
}

export const NewsletterSubscription = ({
context,
}: {
context?: NewsletterSubscriptionContext
context?:
| NewsletterSubscriptionContext.Homepage
| NewsletterSubscriptionContext.Floating
}) => {
const [isOpen, setIsOpen] = useState(false)

Expand Down Expand Up @@ -92,6 +90,12 @@ export const NewsletterSubscriptionForm = ({
id="mc-embedded-subscribe-form"
name="mc-embedded-subscribe-form"
target="_blank"
onSubmit={() =>
analytics.logSiteFormSubmit(
"newsletter-subscribe",
`Subscribe [${context ?? "other-contexts"}]`
)
}
>
<span className="NewsletterSubscriptionForm__header">
Receive our latest work by email.
Expand Down
6 changes: 2 additions & 4 deletions site/SiteNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import {
faXmark,
faEnvelopeOpenText,
} from "@fortawesome/free-solid-svg-icons"
import {
NewsletterSubscriptionContext,
NewsletterSubscriptionForm,
} from "./NewsletterSubscription.js"
import { NewsletterSubscriptionContext } from "./newsletter.js"
import { NewsletterSubscriptionForm } from "./NewsletterSubscription.js"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
import { SiteNavigationTopics } from "./SiteNavigationTopics.js"
import { SiteLogos } from "./SiteLogos.js"
Expand Down
2 changes: 1 addition & 1 deletion site/SiteTools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React from "react"
import ReactDOM from "react-dom"
import { FeedbackPrompt } from "./Feedback.js"
import { ScrollDirection, useScrollDirection } from "./hooks.js"
import { NewsletterSubscriptionContext } from "./newsletter.js"
import {
NewsletterSubscription,
NewsletterSubscriptionForm,
NewsletterSubscriptionContext,
} from "./NewsletterSubscription.js"
// import { FontAwesomeIcon } from "@fortawesome/react-fontawesome/index.js"
// import { faHandshake } from "@fortawesome/free-solid-svg-icons"
Expand Down
20 changes: 20 additions & 0 deletions site/gdocs/components/DataInsightsNewsletter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import cx from "classnames"
import { faXmark } from "@fortawesome/free-solid-svg-icons"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
import { Button, TextInput } from "@ourworldindata/components"
import { SiteAnalytics } from "../../SiteAnalytics.js"
import { NewsletterSubscriptionContext } from "../../newsletter.js"

const analytics = new SiteAnalytics()

export default function DataInsightsNewsletter({
className,
context,
onClose,
}: {
className?: string
context:
| NewsletterSubscriptionContext.HomepageDataInsights
| NewsletterSubscriptionContext.FloatingDataInsights
onClose?: () => void
}) {
return (
Expand Down Expand Up @@ -48,6 +56,12 @@ export default function DataInsightsNewsletter({
action="https://ourworldindata.us8.list-manage.com/subscribe/post?u=18058af086319ba6afad752ec&id=2e166c1fc1"
method="post"
target="_blank"
onSubmit={() =>
analytics.logSiteFormSubmit(
"newsletter-subscribe",
`Subscribe [${context}]`
)
}
>
<TextInput
className="data-insights-newsletter__email-input"
Expand All @@ -62,6 +76,12 @@ export default function DataInsightsNewsletter({
theme="solid-vermillion"
text="Subscribe"
icon={null}
onClick={() =>
analytics.logSiteClick(
"newsletter-subscribe",
`Subscribe [${context}]`
)
}
/>
</form>
<p className="data-insights-newsletter__note note-1-medium">
Expand Down
5 changes: 4 additions & 1 deletion site/gdocs/components/LatestDataInsightsBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import cx from "classnames"

import { AttachmentsContext } from "../OwidGdoc.js"
import { Button } from "@ourworldindata/components"
import { NewsletterSubscriptionContext } from "../../newsletter.js"
import LatestDataInsights from "./LatestDataInsights.js"
import DataInsightsNewsletter from "./DataInsightsNewsletter.js"

Expand Down Expand Up @@ -33,7 +34,9 @@ export default function LatestDataInsightsBlock({
className="span-cols-12 col-start-2"
latestDataInsights={latestDataInsights}
/>
<DataInsightsNewsletter />
<DataInsightsNewsletter
context={NewsletterSubscriptionContext.HomepageDataInsights}
/>
</section>
)
}
6 changes: 2 additions & 4 deletions site/gdocs/pages/Homepage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import {
faThreads,
} from "@fortawesome/free-brands-svg-icons"
import React from "react"
import {
NewsletterSubscriptionContext,
NewsletterSubscriptionForm,
} from "../../NewsletterSubscription.js"
import { NewsletterSubscriptionContext } from "../../newsletter.js"
import { NewsletterSubscriptionForm } from "../../NewsletterSubscription.js"
import { ArticleBlocks } from "../components/ArticleBlocks.js"
import {
CategoryWithEntries,
Expand Down
6 changes: 6 additions & 0 deletions site/newsletter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum NewsletterSubscriptionContext {
Homepage = "homepage",
Floating = "floating",
HomepageDataInsights = "homepageDataInsights",
FloatingDataInsights = "floatingDataInsights",
}

0 comments on commit a2f9d9d

Please sign in to comment.