Skip to content

Commit

Permalink
chore(glean): migrate to using data-glean everywhere (#9496)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoMcA committed Aug 29, 2023
1 parent af58be3 commit bd99229
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
7 changes: 4 additions & 3 deletions client/src/document/code/ai-explain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SSE } from "sse.js";
import { AI_EXPLAIN } from "../../telemetry/constants";

interface ExplainRequest {
language: string | undefined;
Expand All @@ -25,7 +26,7 @@ function explain(
function thumbs(upDown: string, title: string, callback: () => void): Element {
const thumbs = document.createElement("button");
thumbs.classList.add("icon", `icon-thumbs-${upDown}`);
thumbs.setAttribute("data-ai-explain", `thumbs_${upDown}`);
thumbs.dataset.glean = `${AI_EXPLAIN}: thumbs_${upDown}`;
thumbs.addEventListener("click", callback);
thumbs.title = title;
return thumbs;
Expand Down Expand Up @@ -80,7 +81,7 @@ export function addExplainButton(
button.textContent = "AI Explain";
button.classList.add("ai-explain-button");
button.type = "button";
button.setAttribute("data-ai-explain", "explain");
button.dataset.glean = `${AI_EXPLAIN}: explain`;
button.title = "Explain (parts of) this example";
buttonContainer.appendChild(button);
const info = document.createElement("div");
Expand All @@ -90,7 +91,7 @@ export function addExplainButton(
const infoToggle = document.createElement("button");
infoToggle.classList.add("ai-explain-info-toggle", "icon", "icon-note-info");
infoToggle.type = "button";
infoToggle.setAttribute("data-ai-explain", "info-toggle");
infoToggle.dataset.glean = `${AI_EXPLAIN}: info-toggle`;
infoToggle.title = "Toggle AI Explain info";
infoToggle.addEventListener("click", () =>
info.classList.toggle("visually-hidden")
Expand Down
3 changes: 2 additions & 1 deletion client/src/document/code/playground.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EditorContent, SESSION_KEY } from "../../playground/utils";
import { PLAYGROUND } from "../../telemetry/constants";

const LIVE_SAMPLE_PARTS = ["html", "css", "js"];

Expand Down Expand Up @@ -71,7 +72,7 @@ export function addBreakoutButton(

button.setAttribute("class", "play-button external");
button.type = "button";
button.setAttribute("data-play", id);
button.dataset.glean = `${PLAYGROUND}: breakout->${id}`;
button.title = "Open in Playground";
element.appendChild(button);

Expand Down
22 changes: 7 additions & 15 deletions client/src/telemetry/glean-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useEffect, useRef } from "react";
import { useLocation } from "react-router";
import { useUserData } from "../user-context";
import { handleSidebarClick } from "./sidebar-click";
import { AI_EXPLAIN, PLAYGROUND, VIEWPORT_BREAKPOINTS } from "./constants";
import { VIEWPORT_BREAKPOINTS } from "./constants";

export type ViewportBreakpoint = "xs" | "sm" | "md" | "lg" | "xl" | "xxl";
export type HTTPStatus = "200" | "404";
Expand Down Expand Up @@ -142,27 +142,19 @@ const gleanAnalytics = glean();
const GleanContext = React.createContext(gleanAnalytics);

function handleButtonClick(ev: MouseEvent, click: (source: string) => void) {
const button = ev?.target as Element;
if (button?.nodeName === "BUTTON") {
if (button.hasAttribute?.("data-play")) {
click(
`${PLAYGROUND}: breakout->${button.getAttribute("data-play") || ""}`
);
} else if (button.hasAttribute?.("data-ai-explain")) {
click(`${AI_EXPLAIN}: ${button.getAttribute("data-ai-explain") || ""}}`);
}
const button = ev?.target;
if (button instanceof HTMLButtonElement && button.dataset.glean) {
click(button.dataset.glean);
}
}

function handleLinkClick(ev: MouseEvent, click: (source: string) => void) {
const anchor = ev?.target;
if (anchor instanceof HTMLAnchorElement) {
if (anchor?.classList.contains("external")) {
click(`external-link: ${anchor.getAttribute("href") || ""}`);
} else if (anchor?.hasAttribute?.("data-pong")) {
click(`pong: ${anchor.getAttribute("data-pong") || ""}`);
} else if (anchor.dataset.glean) {
if (anchor.dataset.glean) {
click(anchor.dataset.glean);
} else if (anchor.classList.contains("external")) {
click(`external-link: ${anchor.getAttribute("href") || ""}`);
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions client/src/ui/organisms/placement/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ function RenderSideOrTopBanner({
<p className="pong-box">
<a
className="pong"
data-pong="pong->click"
data-glean="pong: pong->click"
href={`/pong/click?code=${encodeURIComponent(click)}`}
target="_blank"
rel="noreferrer"
Expand All @@ -333,7 +333,7 @@ function RenderSideOrTopBanner({
{cta && (
<a
className="pong-cta"
data-pong="pong->click"
data-glean="pong: pong->click"
href={`/pong/click?code=${encodeURIComponent(click)}`}
target="_blank"
rel="noreferrer"
Expand All @@ -344,7 +344,7 @@ function RenderSideOrTopBanner({
<a
href="/en-US/advertising"
className="pong-note"
data-pong="pong->about"
data-glean="pong: pong->about"
target="_blank"
rel="noreferrer"
>
Expand All @@ -354,7 +354,9 @@ function RenderSideOrTopBanner({

<a
className="no-pong"
data-pong={user?.isSubscriber ? "pong->settings" : "pong->plus"}
data-glean={
"pong: " + (user?.isSubscriber ? "pong->settings" : "pong->plus")
}
href={
user?.isSubscriber
? "/en-US/plus/settings?ref=nope"
Expand Down Expand Up @@ -385,7 +387,7 @@ function RenderHpPlacement({
>
<a
className="pong"
data-pong="pong->click"
data-glean="pong: pong->click"
href={`/pong/click?code=${encodeURIComponent(click)}`}
target="_blank"
rel="noreferrer"
Expand Down

0 comments on commit bd99229

Please sign in to comment.