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

✨ log grapher views to google analytics #4076

Merged
merged 1 commit into from
Nov 1, 2024
Merged

Conversation

sophiamersmann
Copy link
Member

@sophiamersmann sophiamersmann commented Oct 22, 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)

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?

@owidbot
Copy link
Contributor

owidbot commented Oct 22, 2024

Quick links (staging server):

Site Admin Wizard

Login: ssh owid@staging-site-log-grapher-views

SVG tester:

Number of differences (default views): 0 ✅
Number of differences (all views): 0 ✅

Edited: 2024-10-22 14:22:11 UTC
Execution time: 1.23 seconds

@sophiamersmann sophiamersmann force-pushed the log-grapher-views branch 7 times, most recently from 0a1cf71 to 64fcc54 Compare October 30, 2024 09:03
@sophiamersmann sophiamersmann marked this pull request as ready for review October 30, 2024 09:33
Copy link
Contributor

@danyx23 danyx23 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks very nice! I wonder if it would make sense to also do something like this for Explorers right away? There the whole navigation is even more interesting

@sophiamersmann
Copy link
Member Author

yes, would be good to do now, also because this stuff is now fresh in my mind. I created an issue and added it to my cycle – I might have some time towards the end of the week

Copy link
Member Author

sophiamersmann commented Nov 1, 2024

Merge activity

  • Nov 1, 11:16 AM EDT: A user started a stack merge that includes this pull request via Graphite.
  • Nov 1, 11:17 AM EDT: Graphite rebased this pull request as part of a merge.
  • Nov 1, 11:18 AM EDT: A user merged this pull request with Graphite.

@sophiamersmann sophiamersmann merged commit c2d1713 into master Nov 1, 2024
14 of 16 checks passed
@sophiamersmann sophiamersmann deleted the log-grapher-views branch November 1, 2024 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create a custom event for every chart view (in grapher, not explorers)
4 participants