Add GA tracking for source links
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.
- [ ] Its default value should be
- [ ] In
assets/js/googlesitekit/widgets/components/WidgetAreaRenderer.js:- [ ] When rendering the widgets in
widgetsOutput, wrap each widget output with aWidgetContext.Provider. - [ ] Pass the current
widgetas the provider’svalue.
- [ ] When rendering the widgets in
- [ ] Create a new hook
useWidgetinassets/js/hooks/useWidget.jsthat is a simple wrapper arounduseContextto get the current surrounding widget - [ ] Update
assets/js/components/SourceLink.js:- [ ] Use the new
useWidgethook to access the surrounding widget’s information. - [ ] Use
useViewContextto get the currentviewContext(needed for the tracked event). - [ ] Create a new
handleClickcallback and pass it to theLink’sonClickprop. The callback should:- [ ] Return early if required information is missing (
viewContextandwidget.slug). - [ ] Call
trackEventwhen all three variables are available, with the following arguments:- category:
`${viewContext}_widget` - action:
"click_source_link" - label:
widget.slug
- category:
- [ ] Return early if required information is missing (
- [ ] Use the new
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.
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?
Thank you for drafting the IB, @abdelmalekkkkk!
Upon consultation with @aaemnnosttv, we've made a few changes to the ACs, namely:
- We've renamed the event name to
click_source_link. - We've changed the category to
{viewContext}_widget. - 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
useWidgetContextinassets/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!
Thanks @nfmohit! I updated the IB to reflect those changes.
IB ✅
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
Search Funnel
Search Console Popular Keywords
Analytics Module Popular Pages GA4
AdSense Module Overview
AdSense Top Earning Pages
Entity Dashboard
Analytics All Traffic widget
Search Funnel GA4
Search Console Popular Key words
Analytics Overall Page Metrics GA4