Skip to content

Commit

Permalink
Migrate to Glean events from custom pings (Fixes mozilla#13639) (mozi…
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgibson committed Sep 7, 2023
1 parent 850a825 commit 0369bb9
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 263 deletions.
32 changes: 14 additions & 18 deletions docs/attribution/0001-analytics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,8 @@ See the `Glean Book`_ for more developer reference documentation.

Glean is currently behind a feature switch called ``SWITCH_GLEAN_ANALYTICS``.
When the switch is enabled pages will load the Glean JavaScript bundle,
which will do things like register page views and capture link clicks. Our
implementation leverages the same HTML data attributes that we use for
:abbr:`GTM (Google Tag Manager)` when tracking link clicks, so any attributes
you add for :abbr:`GTM (Google Tag Manager)` should also be captured by Glean
automatically.
which will do things like record page hits and link click events that we want
to measure.

Debugging pings
---------------
Expand Down Expand Up @@ -386,9 +383,8 @@ Defining metrics and pings
All of the data we send to the Glean pipeline is defined in
:abbr:`YAML (Yet Another Markup Language)` schema files in the ``./glean/``
project root directory. The ``metrics.yaml`` file defines all the different
metrics types we record, and the ``pings.yaml`` file defines the name of each
ping event we use to send collections of individual metrics. These are all
automatically documented in ``./glean/docs/``.
metrics types and events we record, and the ``pings.yaml`` file defines
any custom pings we use to send collections of individual metrics.

.. Note::

Expand All @@ -408,50 +404,50 @@ It will also first lint the schema files.

.. Important::

All metrics and pings we record using Glean must first undergo a `data review`_
All metrics and events we record using Glean must first undergo a `data review`_
before being made active in production. Therefore anytime we make new additions
to these files, those changes should also undergo review.

Using Glean pings in individual page bundles
Using Glean events in individual page bundles
--------------------------------------------

Our analytics code for Glean lives in a single bundle in the base template,
which is intended to be shared across all web pages. This bundle automatically
initializes Glean and records page view pings. It also creates some helpers
that can be used across different page bundles to record interaction pings
initializes Glean and records page hit events. It also creates some helpers
that can be used across different page bundles to record interaction events
such as link clicks and form submissions.

The ``Mozilla.Glean.pagePing()`` helper can be used to record pings that are
The ``Mozilla.Glean.pageEvent()`` helper can be used to record events that are
specific to a page, such as successful form completions:

.. code-block:: javascript
if (typeof window.Mozilla.Glean !== 'undefined') {
window.Mozilla.Glean.pagePing({
window.Mozilla.Glean.pageEvent({
label: 'newsletter-sign-up-success',
type: 'mozilla-and-you' // type is optional
});
}
It can also be used to record non-interaction pings that are not directly
It can also be used to record non-interaction events that are not directly
initiated by a visitor:

.. code-block:: javascript
if (typeof window.Mozilla.Glean !== 'undefined') {
window.Mozilla.Glean.pagePing({
window.Mozilla.Glean.pageEvent({
label: 'firefox-default',
nonInteraction: true
});
}
The ``Mozilla.Glean.clickPing()`` helper can be used to record click pings
The ``Mozilla.Glean.clickEvent()`` helper can be used to record click events
that are specific to an element in a page, such as a link or button.

.. code-block:: javascript
if (typeof window.Mozilla.Glean !== 'undefined') {
window.Mozilla.Glean.clickPing({
window.Mozilla.Glean.clickEvent({
label: 'firefox-download',
type: 'macOS, release, en-US', // type is optional
position: 'primary' // position is optional
Expand Down
82 changes: 54 additions & 28 deletions glean/metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ page:
lifetime: application
send_in_pings:
- page-view
- interaction
- non-interaction
- events
description: |
The time a page was viewed.
data_sensitivity:
Expand All @@ -25,8 +24,7 @@ page:
lifetime: application
send_in_pings:
- page-view
- interaction
- non-interaction
- events
description: |
The URL path of the page that was viewed, excluding locale.
data_sensitivity:
Expand All @@ -43,8 +41,7 @@ page:
lifetime: application
send_in_pings:
- page-view
- interaction
- non-interaction
- events
description: |
The locale of the page that was viewed.
data_sensitivity:
Expand All @@ -61,8 +58,7 @@ page:
lifetime: application
send_in_pings:
- page-view
- interaction
- non-interaction
- events
description: |
Query parameters associated with the URL of
the page that was viewed.
Expand All @@ -89,8 +85,7 @@ page:
lifetime: application
send_in_pings:
- page-view
- interaction
- non-interaction
- events
description: |
The referring URL that linked to the page that was viewed.
data_sensitivity:
Expand All @@ -109,8 +104,7 @@ page:
lifetime: application
send_in_pings:
- page-view
- interaction
- non-interaction
- events
data_sensitivity:
- technical
bugs:
Expand All @@ -120,20 +114,55 @@ page:
notification_emails:
- marketing-websites-team@mozilla.com
expires: never
page_event:
hit:
type: event
description: |
An event which is sent every time a page is loaded.
data_sensitivity:
- web_activity
- technical
bugs:
- https://github.com/mozilla/bedrock/issues/10746
data_reviews:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1767442
notification_emails:
- marketing-websites-team@mozilla.com
expires: never
interaction:
type: event
description: |
An event containing metrics related to a page level
user interaction state that we want to measure.
Examples: form completion, scroll events.
data_sensitivity:
- web_activity
bugs:
- https://github.com/mozilla/bedrock/issues/10746
data_reviews:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1767442
notification_emails:
- marketing-websites-team@mozilla.com
expires: never
extra_keys:
label:
description: |
The label used to describe the event.
Example: 'newsletter-sign-up-success'
type: string
type:
description: |
The type of event.
Example: 'mozilla-and-you'
type: string
non_interaction:
type: event
lifetime: ping
send_in_pings:
- interaction
- non-interaction
description: |
An event containing metrics related to a page level
completion or state that want to measure. This can be
sent in either an interaction and non-interaction ping,
depending upon the use-case. Examples: form completion,
scroll events, banner impressions.
non-user initiated state that we want to measure.
Examples: banner impressions, conditional messaging.
data_sensitivity:
- web_activity
- technical
bugs:
- https://github.com/mozilla/bedrock/issues/10746
data_reviews:
Expand All @@ -145,23 +174,20 @@ page:
label:
description: |
The label used to describe the event.
Example: 'Newsletters: mozilla-and-you'
Example: 'banner-impression'
type: string
type:
description: |
The type of event.
Example: 'Newsletter Signup Success'
Example: 'get-firefox-banner'
type: string

element:
clicked:
type: event
lifetime: ping
send_in_pings:
- interaction
description: |
An event containing metrics related to which element
in the page was clicked.
An event containing metrics related to an element
in the page that was clicked.
data_sensitivity:
- web_activity
bugs:
Expand Down
29 changes: 1 addition & 28 deletions glean/pings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,7 @@ $schema: moz://mozilla.org/schemas/glean/pings/2-0-0

page-view:
description: |
A ping which is sent everytime a page is viewed.
include_client_id: true
send_if_empty: false
bugs:
- https://github.com/mozilla/bedrock/issues/10746
data_reviews:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1767442
notification_emails:
- marketing-websites-team@mozilla.com

interaction:
description: |
A ping which is sent when a page element is
interacted with.
include_client_id: true
send_if_empty: false
bugs:
- https://github.com/mozilla/bedrock/issues/10746
data_reviews:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1767442
notification_emails:
- marketing-websites-team@mozilla.com

non-interaction:
description: |
A ping which is sent when a non-user initiated event
occurs. Examples: a specific banner impression is
displayed, a video auto-plays on scroll.
A ping which is sent every time a page is viewed.
include_client_id: true
send_if_empty: false
bugs:
Expand Down
2 changes: 1 addition & 1 deletion media/js/base/send-to-device.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ SendToDevice.prototype.onFormSuccess = function () {

// Glean
if (typeof window.Mozilla.Glean !== 'undefined') {
window.Mozilla.Glean.pagePing({
window.Mozilla.Glean.pageEvent({
label: 'send-to-device-success',
type: 'email-address'
});
Expand Down
6 changes: 2 additions & 4 deletions media/js/glean/elements.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import { interaction as interactionPing } from '../libs/glean/pings.js';
import * as element from '../libs/glean/element.js';

function clickPing(obj) {
function clickEvent(obj) {
if (typeof obj !== 'object' && typeof obj.label !== 'string') {
return;
}
Expand All @@ -28,10 +27,9 @@ function clickPing(obj) {

try {
element.clicked.record(data);
interactionPing.submit();
} catch (e) {
//do nothing
}
}

export { clickPing };
export { clickEvent };
20 changes: 6 additions & 14 deletions media/js/glean/init.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import Glean from '@mozilla/glean/web';

import { initPageView, pagePing } from './page.es6';
import { clickPing } from './elements.es6';
import { initPageView, pageEvent } from './page.es6';
import { clickEvent } from './elements.es6';
import Utils from './utils.es6';

const shouldInitialize = Utils.hasValidURLScheme(window.location.href);
Expand Down Expand Up @@ -51,22 +51,14 @@ function initHelpers() {

// Create a global for external bundles to fire interaction pings.
window.Mozilla.Glean = {
pagePing: (obj) => {
pageEvent: (obj) => {
if (shouldInitialize) {
try {
pagePing(obj);
} catch (e) {
//do nothing
}
pageEvent(obj);
}
},
clickPing: (obj) => {
clickEvent: (obj) => {
if (shouldInitialize) {
try {
clickPing(obj);
} catch (e) {
//do nothing
}
clickEvent(obj);
}
}
};
Expand Down
Loading

0 comments on commit 0369bb9

Please sign in to comment.