Skip to content

Commit

Permalink
Fix: GA4 に正しくイベントが送信されていなかった問題を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
tsukumijima committed Dec 30, 2024
1 parent ab16054 commit 5a42bcd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/components/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,8 @@ onMounted(async () => {
} else {
isProjectFileLoaded.value = false;
}
// Google アナリティクスに page_view イベントを送信する
analytics.trackEvent("page_view");
});
</script>
13 changes: 8 additions & 5 deletions src/composables/useAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import ga4mp, { GA4Instance } from "@analytics-debugger/ga4mp";

const GA4_MEASUREMENT_ID = "G-TEMWCS6D7B";
const enabled = ref(false);
const ga4track: GA4Instance = ga4mp([GA4_MEASUREMENT_ID], {
debug: import.meta.env.DEV,
non_personalized_ads: true,
});
let ga4instance: GA4Instance | null = null;

/**
* Google Analytics 4 を Electron で使うための composable
Expand All @@ -27,7 +24,13 @@ export function useAnalytics() {
eventParameters?: Record<string, any>,
) => {
if (!enabled.value) return;
ga4track.trackEvent(eventName, eventParameters);
if (ga4instance == null) {
ga4instance = ga4mp([GA4_MEASUREMENT_ID], {
debug: true,
non_personalized_ads: true,
});
}
ga4instance.trackEvent(eventName, eventParameters);
};

return {
Expand Down
3 changes: 1 addition & 2 deletions src/plugins/ipcMessageReceiverPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ export const ipcMessageReceiver: Plugin = {
DETECT_RESIZED: debounce(
(_, { width, height }: { width: number; height: number }) => {
// window.dataLayer?.push({ event: "windowResize", width, height });
const analytics = useAnalytics();
analytics.trackEvent("windowResize", { width, height });
useAnalytics().trackEvent("electron_window_resize", { width, height });
},
300,
),
Expand Down
22 changes: 12 additions & 10 deletions src/store/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,18 @@ export const settingStore = createPartialStore<SettingStoreTypes>({
mutation(state, { acceptRetrieveTelemetry }) {
state.acceptRetrieveTelemetry = acceptRetrieveTelemetry;
},
action({ mutations }, { acceptRetrieveTelemetry }) {
action({ state, mutations }, { acceptRetrieveTelemetry }) {
/*
window.dataLayer?.push({
event: "updateAcceptRetrieveTelemetry",
acceptRetrieveTelemetry: acceptRetrieveTelemetry == "Accepted",
});
*/
const analytics = useAnalytics();
analytics.trackEvent("updateAcceptRetrieveTelemetry", {
acceptRetrieveTelemetry: acceptRetrieveTelemetry == "Accepted",
});
if (acceptRetrieveTelemetry !== state.acceptRetrieveTelemetry) {
useAnalytics().trackEvent("update_accept_retrieve_telemetry", {
acceptRetrieveTelemetry: acceptRetrieveTelemetry == "Accepted",
});
}
void window.backend.setSetting(
"acceptRetrieveTelemetry",
acceptRetrieveTelemetry,
Expand All @@ -280,17 +281,18 @@ export const settingStore = createPartialStore<SettingStoreTypes>({
mutation(state, { acceptTerms }) {
state.acceptTerms = acceptTerms;
},
action({ mutations }, { acceptTerms }) {
action({ state, mutations }, { acceptTerms }) {
/*
window.dataLayer?.push({
event: "updateAcceptTerms",
acceptTerms: acceptTerms == "Accepted",
});
*/
const analytics = useAnalytics();
analytics.trackEvent("updateAcceptTerms", {
acceptTerms: acceptTerms == "Accepted",
});
if (acceptTerms !== state.acceptTerms) {
useAnalytics().trackEvent("update_accept_terms", {
acceptTerms: acceptTerms == "Accepted",
});
}
void window.backend.setSetting("acceptTerms", acceptTerms);
mutations.SET_ACCEPT_TERMS({ acceptTerms });
},
Expand Down

0 comments on commit 5a42bcd

Please sign in to comment.