Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: catalogue can use google analytics #4473

Merged
merged 6 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/nuxt3-ssr/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ ENV NUXT_PUBLIC_COHORT_ONLY=$NUXT_PUBLIC_COHORT_ONLY
# pass optional analytics key
ENV NUXT_PUBLIC_ANALYTICS_KEY=$NUXT_PUBLIC_ANALYTICS_KEY

# pass optional name of analytics provider (for example: "google-analytics" defaults to "siteimprove")
ENV NUXT_PUBLIC_ANALYTICS_PROVIDER=$NUXT_PUBLIC_ANALYTICS_PROVIDER

## Start the server
CMD node .output/server/index.mjs

Expand Down
20 changes: 19 additions & 1 deletion apps/nuxt3-ssr/app.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
<script setup lang="ts">
const config = useRuntimeConfig();
const route = useRoute();
const { initialize } = useGtag()

const analyticsService = computed(() => {
if( typeof config.public.analyticsProvider === "string" ) {
if(config.public.analyticsProvider.includes("siteimprove") ) {
return "siteimprove";
}
else if(config.public.analyticsProvider === "google-analytics" ) {
return "google-analytics";
} else {
return "";
}
}
});

const isAnalyticsAllowedCookie = useCookie("mg_allow_analytics", {
maxAge: 34560000,
Expand All @@ -18,6 +32,10 @@ function setAnalyticsCookie(value: boolean) {
}
}

if(import.meta.client && config.public.analyticsKey && isAnalyticsAllowedCookie.value && analyticsService.value === "google-analytics") {
initialize(config.public.analyticsKey);
}

const faviconHref = config.public.emx2Theme
? `/_nuxt-styles/img/${config.public.emx2Theme}.ico`
: "/_nuxt-styles/img/molgenis.ico";
Expand All @@ -40,7 +58,7 @@ useHead({
}
},
script:
config.public.analyticsKey && isAnalyticsAllowedCookie.value
config.public.analyticsKey && isAnalyticsAllowedCookie.value && analyticsService.value === "siteimprove"
? [
{
src: `https://siteimproveanalytics.com/js/siteanalyze_${config.public.analyticsKey}.js`,
Expand Down
2 changes: 2 additions & 0 deletions apps/nuxt3-ssr/interfaces/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,5 @@ export interface UIResource {
id: string;
logo: { url: string };
}

export type analyticsSericves = "siteimprove" | "google-analytics";
7 changes: 6 additions & 1 deletion apps/nuxt3-ssr/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { defineNuxtConfig } from "nuxt/config";
export default defineNuxtConfig({
extends: ["../tailwind-components"],
devtools: { enabled: true },
modules: ["@nuxt/image", "@nuxt/test-utils/module"],
modules: ["@nuxt/image", "@nuxt/test-utils/module", "nuxt-gtag"],
tailwindcss: {
cssPath: "../tailwind-components/assets/css/main.css",
configPath: "../tailwind-components/tailwind.config.js",
Expand All @@ -15,6 +15,7 @@ export default defineNuxtConfig({
emx2Logo: "",
siteTitle: "MOLGENIS",
analyticsKey: "",
analyticsProvider: "siteimprove",
cohortOnly: false,
apiBase:
process.env.NUXT_PUBLIC_API_BASE ||
Expand Down Expand Up @@ -49,4 +50,8 @@ export default defineNuxtConfig({
global: true,
},
],
// @ts-ignore // gtag is not in the types
gtag: {
initMode: 'manual',
}
});
1 change: 1 addition & 0 deletions apps/nuxt3-ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@vueuse/core": "10.7.0",
"floating-vue": "5.2.2",
"metadata-utils": "*",
"nuxt-gtag": "3.0.1",
"vite": "5.4.6",
"vue": "3.5.6"
}
Expand Down
Loading