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

chore(glean): add page's UTM parameters to pings #9595

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 24 additions & 1 deletion client/src/telemetry/generated/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

// AUTOGENERATED BY glean_parser v8.1.1. DO NOT EDIT. DO NOT COMMIT.

import UrlMetricType from "@mozilla/glean/private/metrics/url";
import LabeledMetricType from "@mozilla/glean/private/metrics/labeled";
import StringMetricType from "@mozilla/glean/private/metrics/string";
import UrlMetricType from "@mozilla/glean/private/metrics/url";

/**
* The HTTP status code of the page.
Expand Down Expand Up @@ -61,3 +62,25 @@ export const referrer = new UrlMetricType({
lifetime: "application",
disabled: false,
});

/**
* The UTM parameters of the page, used to attribute the source of traffic:
* "source": which site sent the traffic
* "medium": what type of link was used
* "campaign": what specific campaign or experiment does this relate to
* "term": here for completeness, the search term that was purchased/bid on
* "content": what specifically was clicked to bring the user to the site
*
* Generated from `page.utm`.
*/
export const utm = new LabeledMetricType(
{
category: "page",
name: "utm",
sendInPings: ["action", "page"],
lifetime: "application",
disabled: false,
},
StringMetricType,
["campaign", "content", "medium", "source", "term"]
);
24 changes: 24 additions & 0 deletions client/src/telemetry/glean-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ import { Doc } from "../../../libs/types/document";
export type ViewportBreakpoint = "xs" | "sm" | "md" | "lg" | "xl" | "xxl";
export type HTTPStatus = "200" | "404";

const UTMParameterNames = [
LeoMcA marked this conversation as resolved.
Show resolved Hide resolved
"source",
"medium",
"campaign",
"term",
"content",
] as const;
type UTMParameters = Partial<
Record<(typeof UTMParameterNames)[number], string>
>;

export type PageProps = {
referrer: string | undefined;
path: string | undefined;
Expand All @@ -26,6 +37,7 @@ export type PageProps = {
viewportRatio: number;
viewportHorizontalCoverage: number;
isBaseline?: string;
utm: UTMParameters;
};

export type PageEventProps = {
Expand Down Expand Up @@ -98,6 +110,9 @@ function glean(): GleanAnalytics {
if (page.isBaseline) {
pageMetric.isBaseline.set(page.isBaseline);
}
for (const param in page.utm) {
pageMetric.utm[param].set(page.utm[param]);
}
pageMetric.httpStatus.set(page.httpStatus);
if (page.geo) {
navigatorMetric.geo.set(page.geo);
Expand Down Expand Up @@ -203,6 +218,7 @@ export function useGleanPage(pageNotFound: boolean, doc?: Doc) {
: doc.baseline.is_baseline
? "baseline"
: "not_baseline",
utm: getUTMParameters(),
});
if (typeof userData !== "undefined" && path.current !== loc.pathname) {
path.current = loc.pathname;
Expand All @@ -223,3 +239,11 @@ export function useGleanClick() {
[glean, userData?.subscriptionType]
);
}

function getUTMParameters(): UTMParameters {
const searchParams = new URLSearchParams(document.location.search);
return UTMParameterNames.reduce((acc, name): UTMParameters => {
const param = searchParams.get(`utm_${name}`);
return param ? { ...acc, [name]: param } : acc;
}, {});
}
28 changes: 28 additions & 0 deletions client/src/telemetry/metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,34 @@ page:
notification_emails:
- mdn-team@mozilla.com
expires: 2024-09-05
utm:
type: labeled_string
lifetime: application
send_in_pings:
- page
- action
description: |
The UTM parameters of the page, used to attribute the source of traffic:
"source": which site sent the traffic
"medium": what type of link was used
"campaign": what specific campaign or experiment does this relate to
"term": here for completeness, the search term that was purchased/bid on
"content": what specifically was clicked to bring the user to the site
data_sensitivity:
- web_activity
bugs:
- "https://mozilla-hub.atlassian.net/browse/MP-545"
data_reviews:
- "https://bugzilla.mozilla.org/show_bug.cgi?id=1851150"
notification_emails:
- mdn-team@mozilla.com
expires: 2024-09-05
labels:
- source
- medium
- campaign
- term
- content
http_status:
type: string
description: |
Expand Down
Loading