Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Disable Plausible at init (#1182)

Dhruv Bhanushali0a5f4ab2ce5d
diff --git a/frontend/src/composables/use-analytics.ts b/frontend/src/composables/use-analytics.ts
index 09f9e6733..d1568ea99 100644
--- a/frontend/src/composables/use-analytics.ts
+++ b/frontend/src/composables/use-analytics.ts
@@ -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
@@ -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
diff --git a/frontend/src/stores/feature-flag.ts b/frontend/src/stores/feature-flag.ts
index ae418b390..a45ee1b44 100644
--- a/frontend/src/stores/feature-flag.ts
+++ b/frontend/src/stores/feature-flag.ts
@@ -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
+ },
},
})