Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a custom event for every chart view (in grapher, not explorers) #3834

Closed
larsyencken opened this issue Aug 1, 2024 · 1 comment · Fixed by #4076
Closed

Create a custom event for every chart view (in grapher, not explorers) #3834

larsyencken opened this issue Aug 1, 2024 · 1 comment · Fixed by #4076

Comments

@larsyencken
Copy link
Contributor

Context

  • We currently count chart views for a chart <slug> by looking at Google Analytics pageviews for https://ourworldindata.org/grapher/<slug>
  • Since many minor interactions with a chart trigger new pageview events, we look at the referrer and eliminate events that share the same chart as referrer

Problem

  • This approach under-counts chart views, since many of them will be embedded in articles
  • This approach makes it harder for us to define what we think a significant chart view is, which may become more relevant as multi-dimensional indicators are added at /grapher/<slug>
    • Switching dimensions would change the chart enough to count as a significant new pageview

Proposed solution

  • Create our own chart view event
    • Fired on first load of a Grapher page or Grapher embed
    • Not fired when the chart is interacted with, despite GET parameter changes
  • (in analytics) Use the new chart view event as the source of truth for future chart views
@danyx23
Copy link
Contributor

danyx23 commented Oct 3, 2024

Grapher views seem especially important. For explorers it would be great to spike this a bit and draft a map of how we would tackle this (what is the payload of the event?). For explorers, we should create a new follow-up issue then.

@danyx23 danyx23 changed the title Create a custom event for every chart view Create a custom event for every chart view (in grapher, not explorers) Oct 3, 2024
sophiamersmann added a commit that referenced this issue Nov 1, 2024
Resolves #3834

## Summary

A view event is logged to Google Analytics when Grapher is rendered.

## Details

- A view event is logged if Grapher is scrolled into view so that we only log an event when a Grapher chart has actually been looked at
- Grapher only logs a view event if it has a slug
    - Explorers used to pass their slug to Grapher for no good reason -> has been removed
    - Mdims used to pass a DUMMY slug to Graphers as a workaround -> has been removed

Logged events are of the following shape:

```
{
    "event": "owid.grapher_view",
    "grapherPath": "/grapher/share-of-population-in-extreme-poverty"
}
```

or, for multi-dim data pages,

```
{
    "event": "owid.grapher_view",
    "grapherPath": "/grapher/mdd-demo-energy",
    "grapherView": { "source": "all", "metric": "total" }
}
```

- Bobbie said it didn't matter if `grapherView` is stringified or not, so I went with the richer format
- The keys in `grapherView` (e.g. `source`, `metric`) are always complete and sorted

If I understand correctly, Google Tag Manager always includes the current page, so there is no need to explicitly pass that for embedded Graphers.

#### Share menu refactor

- The reason explorers and mdims pass a slug into Grapher is that a missing slug disables the share menu
- This didn't make much sense (unless I'm missing something), so I refactored the share menu such that the share menu button is hidden when the menu wouldn't have any items (which depends on the canonical and edit URLs)

## Testing

I tested this code by console logging when an event is fired, but I haven't checked to see if these events actually end up in GA. (I asked Bobbie how to do this and will check later today)

- Grapher/data page: http://staging-site-log-grapher-views/grapher/life-expectancy
  - [x] Should log one view event
- Article: http://staging-site-log-grapher-views/how-has-income-inequality-within-countries-evolved-over-the-past-century
  - [x] Should log a view event when Grapher is scrolled into view, but not earlier
- Multi-dim data page: http://staging-site-log-grapher-views/admin/grapher/mdd-demo-energy
  - [x] Should log an event on page load
  - [x] Should log an event on changing the view
- Homepage: http://staging-site-log-grapher-views
  - [x] Should log an event when scrolling to the first key indicator
  - [x] Should log an event when opening another key indicator, but not earlier
- Topic page: http://staging-site-log-grapher-views/poverty
  - [x] Should log an event when clicking on a key insight with a chart, but not earlier
  - [x] Should log an event on scrolling to the All charts section, but not earlier
  - [x] Should log an event on clicking the "Next" button in the All charts block, but not earlier
  - [x] Should not log an event for the embedded explorer
- Explorer page: http://staging-site-log-grapher-views/explorers/air-pollution
  - [x] Should not log an event

## Caveats

- In the All charts block, an event is logged when going back to a chart that has already been viewed
- In the Key indicator block, no event is logged when opening a key indicator that has been opened before

Not sure what's the right thing to do here, but I guess it doesn't matter that much and both are fine?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants