site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Add GA tracking for source links

Open nfmohit opened this issue 5 months ago • 5 comments

Feature Description

We should add (opt-in) GA tracking for the source links underneath each widget area in the Site Kit dashboard.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • For each widget in the Site Kit dashboard that includes a source link, add GA tracking to track when an opted-in user clicks on the source link, based on the following specification:
Event Name Category Label Trigger Description
click_source_link {viewContext}_widget Widget slug Click User clicks the source link in any widget

Implementation Brief

The SourceLink component is deeply nested within many widgets, and prop-drilling the widget information to it is not practical or maintainable.
To solve this, we will introduce a new context provider to pass the widget information.

  • [ ] Create a new context in assets/js/googlesitekit/widgets/components/WidgetContext.js
    • [ ] Its default value should be undefined.
  • [ ] In assets/js/googlesitekit/widgets/components/WidgetAreaRenderer.js:
    • [ ] When rendering the widgets in widgetsOutput, wrap each widget output with a WidgetContext.Provider.
    • [ ] Pass the current widget as the provider’s value.
  • [ ] Create a new hook useWidget in assets/js/hooks/useWidget.js that is a simple wrapper around useContext to get the current surrounding widget
  • [ ] Update assets/js/components/SourceLink.js:
    • [ ] Use the new useWidget hook to access the surrounding widget’s information.
    • [ ] Use useViewContext to get the current viewContext (needed for the tracked event).
    • [ ] Create a new handleClick callback and pass it to the Link’s onClick prop. The callback should:
      • [ ] Return early if required information is missing (viewContext and widget.slug).
      • [ ] Call trackEvent when all three variables are available, with the following arguments:
        • category: `${viewContext}_widget`
        • action: "click_source_link"
        • label: widget.slug

Test Coverage

  • Add test coverage for this new behavior in assets/js/components/SourceLink.test.js

QA Brief

  • Set up Site Kit.
  • Use a browser extension such as Google Analytics Debugger to debug GA events.
  • Click on the source links on each of the widgets in the Site Kit dashboard and verify that the GA event mentioned in the ACs is correctly tracked.

Changelog entry

  • Add GA tracking for source links underneath each widget area in the Site Kit dashboard.

nfmohit avatar Jul 21 '25 09:07 nfmohit

Thanks @nfmohit, the AC seems reasonable but I think it would help to review the IB at the same time since this is quite limited in scope. How do you see this integrating?

aaemnnosttv avatar Jul 22 '25 19:07 aaemnnosttv

Thank you for drafting the IB, @abdelmalekkkkk!

Upon consultation with @aaemnnosttv, we've made a few changes to the ACs, namely:

  1. We've renamed the event name to click_source_link.
  2. We've changed the category to {viewContext}_widget.
  3. We've changed the label to contain the widget slug, i.e. we do not need to track the widget's modules.

Could you please update the IB to reflect the above changes?

  • Create a new hook useWidgetContext in assets/js/hooks/useWidgetContext.js

Additionally, could you rename the hook to useWidget instead? It's more inline with our convention with our other hooks for context.

Thank you!

nfmohit avatar Oct 27 '25 14:10 nfmohit

Thanks @nfmohit! I updated the IB to reflect those changes.

abdelmalekkkkk avatar Oct 29 '25 01:10 abdelmalekkkkk

IB ✅

nfmohit avatar Oct 31 '25 11:10 nfmohit

QA Update ✅

  • Tested on dev environment.
  • Verified source links of all widgets on main and entity dashboard.
  • Verified click on the source links on each of the widgets in the Site Kit dashboard and verified that the GA event mentioned in the ACs is correctly tracked.

Main Dashboard

Analytics All widget source link

Image

Search Funnel

Image

Search Console Popular Keywords

Image

Analytics Module Popular Pages GA4

Image

AdSense Module Overview

Image

AdSense Top Earning Pages

Image

Entity Dashboard

Analytics All Traffic widget

Image

Search Funnel GA4

Image

Search Console Popular Key words

Image

Analytics Overall Page Metrics GA4

Image

mohitwp avatar Dec 17 '25 13:12 mohitwp