Skip to content

Commit

Permalink
Disable Plausible at init (#1182)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb authored Apr 12, 2023
1 parent 751c541 commit 0a5f4ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
8 changes: 7 additions & 1 deletion frontend/src/composables/use-analytics.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { computed } from "vue"
import { computed, onMounted } from "vue"
import { useContext } from "@nuxtjs/composition-api"

import type { Events, EventName } from "~/types/analytics"
import { useUiStore } from "~/stores/ui"
import { useFeatureFlagStore } from "~/stores/feature-flag"

/**
* The `ctx` parameter must be supplied if using this composable outside the
Expand All @@ -11,6 +12,11 @@ import { useUiStore } from "~/stores/ui"
export const useAnalytics = () => {
const { $plausible, $ua, i18n } = useContext()
const uiStore = useUiStore()
const featureFlagStore = useFeatureFlagStore()

onMounted(() => {
featureFlagStore.syncAnalyticsWithLocalStorage()
})

/**
* the Plausible props that work identically on the server-side and the
Expand Down
18 changes: 11 additions & 7 deletions frontend/src/stores/feature-flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,18 @@ export const useFeatureFlagStore = defineStore(FEATURE_FLAG, {
const flag = this.flags[name]
if (getFlagStatus(flag) === SWITCHABLE) {
flag.preferredState = targetState

// For Plausible to stop tracking `plausible_ignore` must be set.
// Ref: https://plausible.io/docs/excluding-localstorage
if (name === "analytics") {
const storage = useStorage<boolean | null>("plausible_ignore", null)
storage.value = targetState === ON ? null : true
}
if (name === "analytics") this.syncAnalyticsWithLocalStorage()
} else warn(`Cannot set preferred state for non-switchable flag: ${name}`)
},
/**
* For Plausible to stop tracking `plausible_ignore` must be set in
* `localStorage`.
* @see {@link https://plausible.io/docs/excluding-localstorage}
*/
syncAnalyticsWithLocalStorage() {
const storage = useStorage<boolean | null>("plausible_ignore", null)
storage.value =
getFeatureState(this.flags["analytics"]) === ON ? null : true
},
},
})

0 comments on commit 0a5f4ab

Please sign in to comment.