Skip to content

Commit cc2eb87

Browse files
committed
Use axiom to capture events
1 parent fa39108 commit cc2eb87

7 files changed

+56
-7
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ build/
33
background.js
44
.DS_Store
55
*.zip
6+
.env

build.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import archiver from 'archiver'
22
import autoprefixer from 'autoprefixer'
3+
import * as dotenv from 'dotenv'
34
import esbuild from 'esbuild'
45
import postcssPlugin from 'esbuild-style-plugin'
56
import fs from 'fs-extra'
7+
import process from 'node:process'
68
import tailwindcss from 'tailwindcss'
79

10+
dotenv.config()
11+
812
const outdir = 'build'
913

1014
async function deleteOldDir() {
@@ -25,6 +29,7 @@ async function runEsbuild() {
2529
minify: true,
2630
define: {
2731
'process.env.NODE_ENV': '"production"',
32+
'process.env.AXIOM_TOKEN': JSON.stringify(process.env.AXIOM_TOKEN || 'UNDEFINED'),
2833
},
2934
jsxFactory: 'h',
3035
jsxFragment: 'Fragment',

package-lock.json

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"archiver": "^5.3.1",
3737
"autoprefixer": "^10.4.13",
3838
"chokidar-cli": "^3.0.0",
39+
"dotenv": "^16.0.3",
3940
"esbuild": "^0.16.4",
4041
"esbuild-style-plugin": "^1.6.1",
4142
"eslint": "^8.30.0",

src/analytics.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
export function captureEvent(event: string, properties?: object) {
2-
// TODO
2+
fetch('https://api.axiom.co/v1/datasets/extension/ingest', {
3+
method: 'POST',
4+
headers: {
5+
'Content-Type': 'application/x-ndjson',
6+
Authorization: `Bearer ${process.env.AXIOM_TOKEN}`,
7+
},
8+
body: JSON.stringify({ event, ...(properties || {}) }),
9+
}).catch((err) => console.error('captureEvent error', err))
310
}

src/content-script/ChatGPTQuery.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function ChatGPTQuery(props: Props) {
6969

7070
useEffect(() => {
7171
if (status === 'success') {
72-
captureEvent('showAnswer', { status })
72+
captureEvent('show_answer', { host: location.host, language: navigator.language })
7373
}
7474
}, [props.question, status])
7575

src/content-script/Promotion.tsx

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
1+
import { useCallback } from 'react'
2+
import { captureEvent } from '../analytics'
13
import { PromotionResponse } from './api'
24

35
interface Props {
46
data: PromotionResponse
57
}
68

79
function Promotion({ data }: Props) {
10+
const capturePromotionClick = useCallback(() => {
11+
captureEvent('click_promotion', { link: data.url })
12+
}, [data.url])
13+
814
if (data.image && !data.text) {
915
return (
10-
<a href={data.url} target="_blank" rel="noreferrer" className="mt-5">
16+
<a
17+
href={data.url}
18+
target="_blank"
19+
rel="noreferrer"
20+
className="mt-5"
21+
onClick={capturePromotionClick}
22+
>
1123
<img src={data.image.url} className="w-full" />
1224
</a>
1325
)
1426
}
1527
return (
1628
<div className="chat-gpt-card flex flex-row gap-2 mt-5 gpt-promotion">
1729
{!!data.image && (
18-
<a href={data.url} target="_blank" rel="noreferrer">
30+
<a href={data.url} target="_blank" rel="noreferrer" onClick={capturePromotionClick}>
1931
<img
2032
src={data.image.url}
2133
width={data.image.size || 100}
@@ -26,22 +38,28 @@ function Promotion({ data }: Props) {
2638
<div className="flex flex-col justify-between">
2739
<div>
2840
{!!data.title && (
29-
<a href={data.url} target="_blank" rel="noreferrer">
41+
<a href={data.url} target="_blank" rel="noreferrer" onClick={capturePromotionClick}>
3042
<p className="font-bold">{data.title}</p>
3143
</a>
3244
)}
3345
{!!data.text &&
3446
(data.title ? (
3547
<p>{data.text}</p>
3648
) : (
37-
<a href={data.url} target="_blank" rel="noreferrer">
49+
<a href={data.url} target="_blank" rel="noreferrer" onClick={capturePromotionClick}>
3850
<p>{data.text}</p>
3951
</a>
4052
))}
4153
</div>
4254
<div className="flex flex-row justify-between">
4355
{!!data.footer && (
44-
<a href={data.footer.url} target="_blank" rel="noreferrer" className="text-xs">
56+
<a
57+
href={data.footer.url}
58+
target="_blank"
59+
rel="noreferrer"
60+
className="text-xs"
61+
onClick={capturePromotionClick}
62+
>
4563
{data.footer.text}
4664
</a>
4765
)}
@@ -51,6 +69,7 @@ function Promotion({ data }: Props) {
5169
target="_blank"
5270
rel="noreferrer"
5371
className="text-xs rounded-sm border border-solid px-[2px] text-inherit"
72+
onClick={capturePromotionClick}
5473
>
5574
{data.label.text}
5675
</a>

0 commit comments

Comments
 (0)