Skip to content

Commit

Permalink
Add telemetry for certain events (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
rosslh authored Aug 25, 2024
1 parent 0090e19 commit bd52383
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:
working-directory: ./client
run: |
npm run build
env:
SUPABASE_PROJECT_ID: ${{ secrets.SUPABASE_PROJECT_ID }}
SUPABASE_ANON_KEY: ${{ secrets.SUPABASE_ANON_KEY }}
- name: Publish
uses: netlify/actions/cli@master
with:
Expand Down
6 changes: 6 additions & 0 deletions client/css/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ datalist {

> input[type="range"] {
width: 100px;

&::-webkit-slider-runnable-track {
background: #007aff;
border-radius: 48px;
}
}

> select.fullWidth {
Expand Down Expand Up @@ -254,6 +259,7 @@ select {
padding: 2px;
background-color: white;
color: black;
border-radius: 4px;

&:not(:disabled)hover {
opacity: 0.9;
Expand Down
18 changes: 18 additions & 0 deletions client/js/MandelbrotControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as L from "leaflet";
import debounce from "lodash/debounce";
import type MandelbrotMap from "./MandelbrotMap";
import throttle from "lodash/throttle";
import api from "./api";

type NumberInput = {
id: "iterations" | "exponent" | "re" | "im" | "zoom";
Expand Down Expand Up @@ -125,6 +126,20 @@ class MandelbrotControls {
}, 300);
}

private async logEvent(eventName: "image_save" | "share") {
await api.client?.from("events").insert([
{
event_name: eventName,
share_url: this.getShareUrl(),
re: String(this.map.config.re),
im: String(this.map.config.im),
zoom: this.map.config.zoom,
iterations: this.map.config.iterations,
session_id: api.sessionId,
},
]);
}

private handleSaveImageButton() {
let isSavingImage = false;

Expand Down Expand Up @@ -197,6 +212,8 @@ class MandelbrotControls {
saveImageSubmitButton.setAttribute("disabled", "true");
closeModalButton.setAttribute("disabled", "true");

this.logEvent("image_save");

this.map
.saveVisibleImage(width, height)
.catch((error) => {
Expand Down Expand Up @@ -261,6 +278,7 @@ class MandelbrotControls {
navigator.clipboard.writeText(this.getShareUrl()).then(() => {
alert("The URL for this view has been copied!");
});
this.logEvent("share");
};
}

Expand Down
21 changes: 21 additions & 0 deletions client/js/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createClient } from "@supabase/supabase-js";

const client =
process.env.SUPABASE_PROJECT_ID && process.env.SUPABASE_ANON_KEY
? createClient(
`https://${process.env.SUPABASE_PROJECT_ID}.supabase.co`,
process.env.SUPABASE_ANON_KEY,
)
: null;

let sessionId = `${Math.random()}|${Date.now()}`;
try {
sessionId = self.crypto.randomUUID() || sessionId;
} catch (e) {
console.warn("crypto.randomUUID() not available");
}

export default {
client,
sessionId,
};
132 changes: 127 additions & 5 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@typescript-eslint/parser": "^6.20.0",
"@wasm-tool/wasm-pack-plugin": "^1.7.0",
"css-loader": "^6.9.1",
"dotenv-webpack": "^8.1.0",
"eslint": "^8.56.0",
"file-loader": "^6.2.0",
"front-matter": "^4.0.2",
Expand All @@ -43,6 +44,7 @@
"webpack-dev-server": "^4.15.1"
},
"dependencies": {
"@supabase/supabase-js": "^2.45.2",
"file-saver": "^2.0.5",
"leaflet": "1.7.1",
"lodash": "^4.17.21",
Expand Down
2 changes: 2 additions & 0 deletions client/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { marked } = require("marked");
const frontMatter = require("front-matter");
const fs = require("fs");
const template = require("lodash/template");
const Dotenv = require("dotenv-webpack");

const blogDir = "./blog";
for (const file of fs.readdirSync(blogDir)) {
Expand Down Expand Up @@ -37,6 +38,7 @@ for (const file of fs.readdirSync(blogDir)) {
const appConfig = {
entry: "./js/main.ts",
plugins: [
new Dotenv(),
new HtmlWebpackPlugin({
template: "html/index.html",
root: path.resolve(__dirname, "."),
Expand Down

0 comments on commit bd52383

Please sign in to comment.