Skip to content

Commit 378c8c8

Browse files
authored
[Chore] Re-enable link tracking events (#16759)
* [Chore] Re-enable link tracking events * regex fix * remove logging * Update path * Refactor
1 parent cfc4c27 commit 378c8c8

File tree

2 files changed

+166
-128
lines changed

2 files changed

+166
-128
lines changed

src/components/overrides/Head.astro

Lines changed: 134 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -10,166 +10,172 @@ import { getPageDescription } from "~/util/description";
1010
const currentSection = Astro.url.pathname.split("/")[1];
1111
1212
if (currentSection) {
13-
const product = await getEntry("products", currentSection)
14-
15-
if (product) {
16-
if (product.data.meta.title) {
17-
const title = `${Astro.props.entry.data.title} | ${product.data.meta.title}`;
18-
Astro.props.entry.data.head.push({
19-
tag: "title",
20-
attrs: {},
21-
content: title
22-
});
23-
Astro.props.entry.data.head.push({
24-
tag: "meta",
25-
attrs: { property: "og:title", content: title },
26-
});
27-
}
28-
if (product.data.product.title) {
29-
const productName = product.data.product.title;
30-
Astro.props.entry.data.head.push({
31-
tag: "meta",
32-
attrs: {
33-
name: "pcx_product",
34-
content: productName
35-
},
36-
content: "",
37-
});
38-
Astro.props.entry.data.head.push({
39-
tag: "meta",
40-
attrs: {
41-
name: "algolia_product_filter",
42-
content: productName
43-
},
44-
content: "",
45-
});
46-
}
47-
if (product.data.product.group) {
48-
Astro.props.entry.data.head.push({
49-
tag: "meta",
50-
attrs: {
51-
name: "pcx_content_group",
52-
content: product.data.product.group
53-
},
54-
content: "",
55-
});
56-
}
57-
}
13+
const product = await getEntry("products", currentSection);
14+
15+
if (product) {
16+
if (product.data.meta.title) {
17+
const title = `${Astro.props.entry.data.title} | ${product.data.meta.title}`;
18+
Astro.props.entry.data.head.push({
19+
tag: "title",
20+
attrs: {},
21+
content: title,
22+
});
23+
Astro.props.entry.data.head.push({
24+
tag: "meta",
25+
attrs: { property: "og:title", content: title },
26+
});
27+
}
28+
if (product.data.product.title) {
29+
const productName = product.data.product.title;
30+
Astro.props.entry.data.head.push({
31+
tag: "meta",
32+
attrs: {
33+
name: "pcx_product",
34+
content: productName,
35+
},
36+
content: "",
37+
});
38+
Astro.props.entry.data.head.push({
39+
tag: "meta",
40+
attrs: {
41+
name: "algolia_product_filter",
42+
content: productName,
43+
},
44+
content: "",
45+
});
46+
}
47+
if (product.data.product.group) {
48+
Astro.props.entry.data.head.push({
49+
tag: "meta",
50+
attrs: {
51+
name: "pcx_content_group",
52+
content: product.data.product.group,
53+
},
54+
content: "",
55+
});
56+
}
57+
}
5858
}
5959
60-
Astro.props.entry.data.description ??= await getPageDescription(Astro.props.entry);
60+
Astro.props.entry.data.description ??= await getPageDescription(
61+
Astro.props.entry,
62+
);
6163
6264
// Adding metadata used for reporting and search indexing
6365
6466
// content type
6567
if (Astro.props.entry.data.pcx_content_type) {
66-
const contentType = Astro.props.entry.data.pcx_content_type
67-
Astro.props.entry.data.head.push({
68-
tag: "meta",
69-
attrs: {
70-
name: "pcx_content_type",
71-
content: Astro.props.entry.data.pcx_content_type
72-
},
73-
content: "",
74-
});
75-
Astro.props.entry.data.head.push({
76-
tag: "meta",
77-
attrs: {
78-
name: "algolia_content_type",
79-
content: Astro.props.entry.data.pcx_content_type
80-
},
81-
content: "",
82-
});
68+
const contentType = Astro.props.entry.data.pcx_content_type;
69+
Astro.props.entry.data.head.push({
70+
tag: "meta",
71+
attrs: {
72+
name: "pcx_content_type",
73+
content: Astro.props.entry.data.pcx_content_type,
74+
},
75+
content: "",
76+
});
77+
Astro.props.entry.data.head.push({
78+
tag: "meta",
79+
attrs: {
80+
name: "algolia_content_type",
81+
content: Astro.props.entry.data.pcx_content_type,
82+
},
83+
content: "",
84+
});
8385
}
8486
8587
// other products
8688
if (Astro.props.entry.data.products) {
87-
const additionalProducts = Astro.props.entry.data.products
88-
Astro.props.entry.data.head.push({
89-
tag: "meta",
90-
attrs: {
91-
name: "pcx_additional_products",
92-
content: additionalProducts.toString()
93-
},
94-
content: "",
95-
});
89+
const additionalProducts = Astro.props.entry.data.products;
90+
Astro.props.entry.data.head.push({
91+
tag: "meta",
92+
attrs: {
93+
name: "pcx_additional_products",
94+
content: additionalProducts.toString(),
95+
},
96+
content: "",
97+
});
9698
}
9799
98100
// other products
99101
if (Astro.props.entry.data.tags) {
100-
const pageTags = Astro.props.entry.data.tags
101-
Astro.props.entry.data.head.push({
102-
tag: "meta",
103-
attrs: {
104-
name: "pcx_tags",
105-
content: pageTags.toString()
106-
},
107-
content: "",
108-
});
102+
const pageTags = Astro.props.entry.data.tags;
103+
Astro.props.entry.data.head.push({
104+
tag: "meta",
105+
attrs: {
106+
name: "pcx_tags",
107+
content: pageTags.toString(),
108+
},
109+
content: "",
110+
});
109111
}
110112
111113
if (Astro.props.entry.data.updated) {
112-
const daysBetween = differenceInCalendarDays(new Date(), Astro.props.entry.data.updated)
113-
Astro.props.entry.data.head.push({
114-
tag: "meta",
115-
attrs: {
116-
name: "pcx_last_reviewed",
117-
content: daysBetween
118-
},
119-
content: "",
120-
});
114+
const daysBetween = differenceInCalendarDays(
115+
new Date(),
116+
Astro.props.entry.data.updated,
117+
);
118+
Astro.props.entry.data.head.push({
119+
tag: "meta",
120+
attrs: {
121+
name: "pcx_last_reviewed",
122+
content: daysBetween,
123+
},
124+
content: "",
125+
});
121126
}
122127
123128
// end metadata
124129
125130
if (Astro.props.entry.data.pcx_content_type === "changelog") {
126-
const href = new URL(Astro.site);
127-
href.pathname = Astro.props.entry.slug.concat("/index.xml");
128-
129-
Astro.props.entry.data.head.push({
130-
tag: "link",
131-
attrs: {
132-
rel: "alternate",
133-
type: "application/rss+xml",
134-
href: href,
135-
},
136-
content: "",
137-
});
131+
const href = new URL(Astro.site);
132+
href.pathname = Astro.props.entry.slug.concat("/index.xml");
133+
134+
Astro.props.entry.data.head.push({
135+
tag: "link",
136+
attrs: {
137+
rel: "alternate",
138+
type: "application/rss+xml",
139+
href: href,
140+
},
141+
content: "",
142+
});
138143
}
139144
140145
if (Astro.props.entry.data.external_link) {
141-
Astro.props.entry.data.head.push({
142-
tag: "meta",
143-
attrs: {
144-
content: "noindex",
145-
name: "robots",
146-
},
147-
content: ""
148-
});
149-
Astro.props.entry.data.head.push({
150-
tag: "meta",
151-
attrs: {
152-
content: `0; url=${Astro.props.entry.data.external_link}`,
153-
"http-equiv": "refresh",
154-
},
155-
content: ""
156-
});
146+
Astro.props.entry.data.head.push({
147+
tag: "meta",
148+
attrs: {
149+
content: "noindex",
150+
name: "robots",
151+
},
152+
content: "",
153+
});
154+
Astro.props.entry.data.head.push({
155+
tag: "meta",
156+
attrs: {
157+
content: `0; url=${Astro.props.entry.data.external_link}`,
158+
"http-equiv": "refresh",
159+
},
160+
content: "",
161+
});
157162
}
158163
---
159164

160165
<script>
161-
import littlefoot from "littlefoot";
162-
163-
document.addEventListener("DOMContentLoaded", () => {
164-
littlefoot();
165-
document
166-
.querySelectorAll("span.littlefoot")
167-
.forEach((x) => x.classList.add("not-content"));
168-
});
166+
import littlefoot from "littlefoot";
167+
168+
document.addEventListener("DOMContentLoaded", () => {
169+
littlefoot();
170+
document
171+
.querySelectorAll("span.littlefoot")
172+
.forEach((x) => x.classList.add("not-content"));
173+
});
169174
</script>
170175
<script>
171-
import mermaid from "mermaid";
176+
import mermaid from "mermaid";
172177

173-
mermaid.initialize({ startOnLoad: true, theme: "neutral" });
178+
mermaid.initialize({ startOnLoad: true, theme: "neutral" });
174179
</script>
180+
<script src="src/scripts/analytics.ts"></script>
175181
<Default {...Astro.props}><slot /></Default>

src/scripts/analytics.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const links = document.querySelectorAll<HTMLAnchorElement>("a");
2+
3+
function $zarazLinkEvent(type: string, link: HTMLAnchorElement) {
4+
zaraz.track(type, { href: link.href, hostname: link.hostname });
5+
}
6+
7+
addEventListener("DOMContentLoaded", () => {
8+
if (links.length > 0) {
9+
for (const link of links) {
10+
const linkURL = new URL(link.href);
11+
const cfSubdomainRegex = new RegExp(`^[^.]+?\\.cloudflare\\.com`);
12+
if (linkURL.hostname !== "developers.cloudflare.com") {
13+
if (
14+
linkURL.hostname === "workers.cloudflare.com" &&
15+
linkURL.pathname.startsWith("/playground#")
16+
) {
17+
link.addEventListener("click", () => {
18+
$zarazLinkEvent("playground link click", link);
19+
});
20+
} else if (cfSubdomainRegex.test(linkURL.hostname)) {
21+
link.addEventListener("click", () => {
22+
$zarazLinkEvent("Cross Domain Click", link);
23+
});
24+
} else {
25+
link.addEventListener("click", () => {
26+
$zarazLinkEvent("external link click", link);
27+
});
28+
}
29+
}
30+
}
31+
}
32+
});

0 commit comments

Comments
 (0)