Skip to content

Commit

Permalink
chore(glean): add page's UTM parameters to pings (#9595)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoMcA committed Oct 16, 2023
1 parent b064256 commit 4c72586
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
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 UTM_PARAMETER_NAMES = [
"source",
"medium",
"campaign",
"term",
"content",
] as const;
type UTMParameters = Partial<
Record<(typeof UTM_PARAMETER_NAMES)[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 UTM_PARAMETER_NAMES.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

0 comments on commit 4c72586

Please sign in to comment.