Skip to content

Commit

Permalink
update gtag (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
wirekang authored Feb 14, 2024
1 parent 1ec3c76 commit 92f90f3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
8 changes: 0 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Kysely Playground</title>
<script src="https://www.googletagmanager.com/gtag/js?l=dataLayer&amp;id=G-G1QNWZ9NSP" async></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "G-G1QNWZ9NSP");
</script>
</head>
<body data-theme="dark">
<div id="header">
Expand Down
13 changes: 13 additions & 0 deletions src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { DEBUG, SETTING_KEYS } from "./lib/constants";
import { SettingsUtils } from "./lib/utility/settings-utils";
import { PanelContainerController } from "./controllers/panel-container-controller";
import { DomUtils } from "./lib/utility/dom-utils";
import { GtagUtils } from "./lib/utility/gtag-utils";

const lazy = null as unknown;
const D = {
Expand Down Expand Up @@ -119,6 +120,7 @@ function setup() {
setupMobileModeController();
setLoading(false);
setupOpenInNewTabController();
setupGtag();
}

function setLoading(v: boolean) {
Expand Down Expand Up @@ -249,6 +251,7 @@ function setupResultController() {
if (e.message && e.message.trim().startsWith("ResizeObserver")) {
return;
}
GtagUtils.event("exception", { description: e, fatal: true });
D.resultController.appendMessage(
"error",
`ERROR: There is an unexpected error. Checkout the developer console.`,
Expand All @@ -259,6 +262,7 @@ function setupResultController() {
D.resultController.clear();
D.resultController.appendMessage("info", "Loading...");
D.resultController.onClickCode = (v) => {
GtagUtils.event("click_result_code");
copyText(v, "Code copied");
};
}
Expand All @@ -274,6 +278,7 @@ function setupVersionController() {
D.versionController.setValue(D.kyselyModule.id);
D.versionController.onChange((id) => {
const newModule = D.kyselyManager.getModule(id)!;
GtagUtils.event("change_version", { type: newModule.type, name: newModule.name });
patchState({
kysely: {
type: newModule.type,
Expand All @@ -287,6 +292,7 @@ function setupDialectController() {
D.dialectController.setOptions([...D.kyselyModule.dialects]);
D.dialectController.setValue(D.state.dialect);
D.dialectController.onChange((dialect: any) => {
GtagUtils.event("change_dialect", { dialect });
patchState({ dialect });
});
}
Expand All @@ -301,6 +307,7 @@ function setupSwitchThemeController() {

function toggleTypeEditor() {
const hidden = D.panel0.isHidden();
GtagUtils.event("toggle_type_editor", { on: !hidden });
D.panel0.setHidden(!hidden);
if (hidden) {
D.panelContainerController.resetSizes();
Expand Down Expand Up @@ -410,6 +417,7 @@ function setupHotKeys() {
}

async function save(shorten: boolean) {
GtagUtils.event("save", { shorten });
await useLoading(async () => {
if (SettingsUtils.get("save:format-before-save")) {
await formatEditors();
Expand All @@ -423,6 +431,7 @@ async function save(shorten: boolean) {
}

async function formatEditors() {
GtagUtils.event("format");
await useLoading(async () => {
try {
const printWidth = SettingsUtils.get("ts-format:wider-width") ? 100 : 70;
Expand Down Expand Up @@ -493,3 +502,7 @@ async function copyText(v: string, msg: string) {
ToastUtils.show("error", e.message ?? e.toString());
}
}

function setupGtag() {
GtagUtils.init({ dialect: D.state.dialect, version: D.state.kysely?.name });
}
16 changes: 16 additions & 0 deletions src/lib/utility/gtag-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export class GtagUtils {
static init(params: any) {
const w = window as any;
w.dataLayer = w.dataLayer || [];
w.gtag = function () {
w.dataLayer.push(arguments);
};
w.gtag("js", new Date());
w.gtag("config", "G-G1QNWZ9NSP", { ...params });
}

static event(name: string, params: any = {}) {
// @ts-ignore
window.gtag("event", name, params);
}
}

0 comments on commit 92f90f3

Please sign in to comment.