Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[Full changelog](https://github.com/mozilla/glean.js/compare/v5.0.5...main)

* [#2011](https://github.com/mozilla/glean.js/pull/2011): Bug 1987158 - Capture page `url`/`referrer`/`title` with `recordElementClick()`.

# v5.0.5 (2025-07-17)

* [#1993](https://github.com/mozilla/glean.js/pull/1993): Add try-catch around uses of sessionStorage and localStorage in core.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ An example HTML button that will fire a click event:
</button>
```

Glean also automatically captures the following values for click events:

- url: `window.location.href`
- referrer: `document.referrer`
- title: `document.title`

## Click event API

Glean.js provides an API for manually collecting click events.
Expand Down
23 changes: 21 additions & 2 deletions glean/src/core/glean_metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type ElementClickEventContext = {
id?: string;
type?: string;
label?: string;
url?: string;
referrer?: string;
title?: string;
};

/**
Expand Down Expand Up @@ -51,7 +54,7 @@ namespace GleanMetrics {
disabled: false,
},
// extras defined in `src/metrics.yaml`.
["id", "type", "label"]
["id", "type", "label", "url", "referrer", "title"]
),
pageId: new UUIDMetric(
{
Expand Down Expand Up @@ -124,8 +127,24 @@ namespace GleanMetrics {
const elementClickEventContext: ElementClickEventContext = {
...(dataset.gleanId && { id: dataset.gleanId }),
...(dataset.gleanType && { type: dataset.gleanType }),
...(dataset.gleanLabel && { label: dataset.gleanLabel })
...(dataset.gleanLabel && { label: dataset.gleanLabel }),
url:
(typeof window !== "undefined"
? window.location.href
: "URL_NOT_PROVIDED_OR_AVAILABLE"
),
referrer:
(typeof document !== "undefined"
? document.referrer
: "REFERRER_NOT_PROVIDED_OR_AVAILABLE"
),
title:
(typeof document !== "undefined"
? document.title
: "TITLE_NOT_PROVIDED_OR_AVAILABLE"
),
};

return elementClickEventContext;
}

Expand Down
9 changes: 9 additions & 0 deletions glean/src/metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,15 @@ glean:
label:
description: The label of the element clicked. For automatic collection, its value is the element's `data-glean-label` data attribute value.
type: string
url:
description: The URL of the page where the element was clicked.
type: string
referrer:
description: The referrer of the page where the element was clicked.
type: string
title:
description: The title of the page where the element was clicked.
type: string
page_id:
type: uuid
description: |
Expand Down