Skip to content

Commit 687f296

Browse files
authored
[Docs Site] Restore analytics events for various elements (#19335)
1 parent 4dfb97b commit 687f296

File tree

7 files changed

+180
-55
lines changed

7 files changed

+180
-55
lines changed

src/components/Stream.astro

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ interface Props {
66
moreVideosLink: string;
77
}
88
9-
const { videoId, videoTitle, thumbnailTimeOrURL, moreVideosLink="true" } = Astro.props;
9+
const {
10+
videoId,
11+
videoTitle,
12+
thumbnailTimeOrURL,
13+
moreVideosLink = "true",
14+
} = Astro.props;
1015
1116
const customerId = "1mwganm1ma0xgnmj";
1217
const baseUrl = `https://customer-${customerId}.cloudflarestream.com/`;
@@ -32,25 +37,46 @@ if (thumbnailTimeOrURL !== undefined) {
3237
}
3338
---
3439

35-
<div style="position: relative; padding-top: 56.25%">
36-
<iframe
37-
src={url.toString()}
38-
style="border: none; position: absolute; top: 0; left: 0; height: 100%; width: 100%;"
39-
allow="accelerometer; gyroscope; autoplay; encrypted-media; picture-in-picture;"
40-
allowfullscreen="true"
41-
title={videoTitle}
42-
id={videoId}></iframe>
43-
</div>
44-
45-
{moreVideosLink=="true" &&
46-
<a href="https://www.youtube.com/@CloudflareDevelopers" target="_blank">Watch more videos on our Developer Channel</a>
47-
}
40+
<stream-player data-id={videoId} data-title={videoTitle}>
41+
<div style="position: relative; padding-top: 56.25%">
42+
<iframe
43+
src={url.toString()}
44+
style="border: none; position: absolute; top: 0; left: 0; height: 100%; width: 100%;"
45+
allow="accelerometer; gyroscope; autoplay; encrypted-media; picture-in-picture;"
46+
allowfullscreen="true"
47+
title={videoTitle}
48+
id={videoId}></iframe>
49+
</div>
50+
51+
{
52+
moreVideosLink == "true" && (
53+
<a href="https://www.youtube.com/@CloudflareDevelopers" target="_blank">
54+
Watch more videos on our Developer Channel
55+
</a>
56+
)
57+
}
58+
</stream-player>
4859

4960
<script is:inline src="https://embed.cloudflarestream.com/embed/sdk.latest.js"
5061
></script>
51-
<script is:inline define:vars={{ vidId: videoId, videoTitle }}>
52-
const video = document.getElementById(vidId);
53-
Stream(video).addEventListener("play", () => {
54-
zaraz.track("play docs video", { title: videoTitle });
55-
});
62+
63+
<script>
64+
import { track } from "~/util/zaraz";
65+
66+
declare function Stream(player: HTMLIFrameElement): HTMLVideoElement;
67+
68+
class StreamPlayer extends HTMLElement {
69+
connectedCallback() {
70+
const id = CSS.escape(this.dataset.id as string);
71+
const title = this.dataset.title as string;
72+
73+
const player = this.querySelector(`#${id}`) as HTMLIFrameElement;
74+
75+
Stream(player).addEventListener("play", () => {
76+
track("play docs video", { title: title });
77+
});
78+
}
79+
}
80+
81+
customElements.define("stream-player", StreamPlayer);
5682
</script>

src/scripts/analytics.ts

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,9 @@
1-
const links = document.querySelectorAll<HTMLAnchorElement>("a");
1+
import { registerLinks } from "./analytics/links";
2+
import { registerTabs } from "./analytics/tabs";
3+
import { registerDetails } from "./analytics/details";
4+
import { registerCopyButtons } from "./analytics/codeblocks";
25

3-
function $zarazLinkEvent(type: string, link: HTMLAnchorElement) {
4-
// @ts-expect-error TODO: type zaraz
5-
zaraz.track(type, { href: link.href, hostname: link.hostname });
6-
}
7-
function registerLinkAnalytics() {
8-
if (!links || links.length === 0) {
9-
return;
10-
}
11-
for (const link of links) {
12-
if (!link.href) {
13-
continue;
14-
}
15-
const linkURL = new URL(link.href);
16-
const cfSubdomainRegex = new RegExp(`^[^.]+?\\.cloudflare\\.com`);
17-
if (linkURL.hostname !== "developers.cloudflare.com") {
18-
if (
19-
linkURL.hostname === "workers.cloudflare.com" &&
20-
linkURL.pathname.startsWith("/playground#")
21-
) {
22-
link.addEventListener("click", () => {
23-
$zarazLinkEvent("playground link click", link);
24-
});
25-
} else if (cfSubdomainRegex.test(linkURL.hostname)) {
26-
link.addEventListener("click", () => {
27-
$zarazLinkEvent("Cross Domain Click", link);
28-
});
29-
} else {
30-
link.addEventListener("click", () => {
31-
$zarazLinkEvent("external link click", link);
32-
});
33-
}
34-
}
35-
}
36-
}
37-
registerLinkAnalytics();
6+
registerLinks();
7+
registerTabs();
8+
registerDetails();
9+
registerCopyButtons();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { track } from "~/util/zaraz";
2+
3+
export function registerCopyButtons() {
4+
const elements = document.querySelectorAll<HTMLElement>(
5+
".expressive-code > figure.frame",
6+
);
7+
8+
if (!elements || elements.length === 0) {
9+
return;
10+
}
11+
12+
for (const el of elements) {
13+
const hasTitle = el.classList.contains("has-title");
14+
15+
const title = hasTitle
16+
? el.querySelector<HTMLElement>(".header")?.innerText
17+
: "title not set";
18+
19+
const language =
20+
el.querySelector<HTMLPreElement>("pre")?.dataset.language ??
21+
"language not set";
22+
23+
const button = el.querySelector<HTMLButtonElement>(".copy > button");
24+
25+
if (!button) continue;
26+
27+
button.addEventListener("click", () => {
28+
track("copy button link click", {
29+
title,
30+
language,
31+
});
32+
});
33+
}
34+
}

src/scripts/analytics/details.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { track } from "~/util/zaraz";
2+
3+
export function registerDetails() {
4+
const elements = document.querySelectorAll<HTMLDetailsElement>("details");
5+
6+
if (!elements || elements.length === 0) {
7+
return;
8+
}
9+
10+
for (const el of elements) {
11+
const summary = el.querySelector("summary");
12+
13+
if (!summary) continue;
14+
15+
el.addEventListener("toggle", () => {
16+
if (!el.open) return;
17+
track("dropdown click", { text: summary.innerText });
18+
});
19+
}
20+
}

src/scripts/analytics/links.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { track } from "~/util/zaraz";
2+
3+
function registerLink(type: string, link: HTMLAnchorElement) {
4+
link.addEventListener("click", () => {
5+
track(type, { href: link.href, hostname: link.hostname });
6+
});
7+
}
8+
9+
export function registerLinks() {
10+
const elements = document.querySelectorAll<HTMLAnchorElement>("a[href]");
11+
12+
if (!elements || elements.length === 0) {
13+
return;
14+
}
15+
16+
for (const el of elements) {
17+
const { hostname, pathname } = new URL(el.href);
18+
19+
if (hostname === "developers.cloudflare.com" || hostname === "localhost") {
20+
continue;
21+
}
22+
23+
if (
24+
hostname === "workers.cloudflare.com" &&
25+
pathname.startsWith("/playground#")
26+
) {
27+
registerLink("playground link click", el);
28+
continue;
29+
}
30+
31+
if (hostname.endsWith(".cloudflare.com")) {
32+
registerLink("Cross Domain Click", el);
33+
continue;
34+
}
35+
36+
registerLink("external link click", el);
37+
}
38+
}

src/scripts/analytics/tabs.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { track } from "~/util/zaraz";
2+
3+
export function registerTabs() {
4+
const elements = document.querySelectorAll<HTMLAnchorElement>(
5+
"starlight-tabs a[role='tab']",
6+
);
7+
8+
if (!elements || elements.length === 0) {
9+
return;
10+
}
11+
12+
elements.forEach((el) =>
13+
el.addEventListener("click", () => {
14+
track("tab click", { selected_option: el.innerText });
15+
}),
16+
);
17+
}

src/util/zaraz.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
declare global {
2+
interface Window {
3+
zaraz?: {
4+
track: Track;
5+
};
6+
}
7+
}
8+
9+
type Track = (event: string, properties?: Record<string, any>) => void;
10+
11+
export const track: Track = (event, properties) => {
12+
if (!window.zaraz) {
13+
console.log("zaraz.track:", event, properties);
14+
return;
15+
}
16+
17+
window.zaraz.track(event, properties);
18+
};

0 commit comments

Comments
 (0)