From 1281f190cc7c8a8926607fc56d0fee85206968e0 Mon Sep 17 00:00:00 2001 From: Jesus Ordosgoitty Date: Sat, 11 Apr 2026 11:50:15 -0400 Subject: [PATCH 1/2] feat: implement client-side language detection and persistent user preference redirection --- .astro/settings.json | 2 +- src/components/HeaderInteractive.tsx | 8 ++++ src/components/LanguageDetector.astro | 55 +++++++++++++++++++++++++++ src/layouts/BaseLayout.astro | 2 + src/lib/i18n.ts | 3 ++ 5 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/components/LanguageDetector.astro diff --git a/.astro/settings.json b/.astro/settings.json index 934e7f9..27baba6 100644 --- a/.astro/settings.json +++ b/.astro/settings.json @@ -1,5 +1,5 @@ { "_variables": { - "lastUpdateCheck": 1772339173160 + "lastUpdateCheck": 1775922065555 } } \ No newline at end of file diff --git a/src/components/HeaderInteractive.tsx b/src/components/HeaderInteractive.tsx index d0d48c2..325cdd9 100644 --- a/src/components/HeaderInteractive.tsx +++ b/src/components/HeaderInteractive.tsx @@ -46,6 +46,12 @@ const HeaderInteractive = ({ navItems, logoSrc, lang, altLangHref }: Props) => { const altLang = lang === 'es' ? 'EN' : 'ES'; + const handleLangSwitch = () => { + const targetLang = lang === 'es' ? 'en' : 'es'; + localStorage.setItem('preferred_lang', targetLang); + sessionStorage.removeItem('lang_redirected'); + }; + return (
{ {/* Language switcher */} {
diff --git a/src/components/LanguageDetector.astro b/src/components/LanguageDetector.astro new file mode 100644 index 0000000..6249090 --- /dev/null +++ b/src/components/LanguageDetector.astro @@ -0,0 +1,55 @@ +--- +// src/components/LanguageDetector.astro +// Client-side browser language detection with SEO-safe redirection. +// Only performs automatic detection on the home page to avoid breaking +// deep-linked / indexed pages. Manual preference (via header toggle) +// is respected on ALL pages. +import type { Lang } from '@/lib/i18n'; + +interface Props { + lang: Lang; + alternateUrl?: string; +} + +const { lang, alternateUrl } = Astro.props; +--- + + diff --git a/src/layouts/BaseLayout.astro b/src/layouts/BaseLayout.astro index 8e11d5c..0eaddc2 100644 --- a/src/layouts/BaseLayout.astro +++ b/src/layouts/BaseLayout.astro @@ -3,6 +3,7 @@ import BaseHead from '@/components/BaseHead.astro'; import Header from '@/components/Header.astro'; import Footer from '@/components/Footer.astro'; +import LanguageDetector from '@/components/LanguageDetector.astro'; import type { Lang } from '@/lib/i18n'; import '@/index.css'; @@ -20,6 +21,7 @@ const { title, description, lang = 'en', alternateUrl } = Astro.props; +
diff --git a/src/lib/i18n.ts b/src/lib/i18n.ts index 8b7ce39..c930d06 100644 --- a/src/lib/i18n.ts +++ b/src/lib/i18n.ts @@ -5,6 +5,9 @@ const translations = { en, es } as const; export type Lang = 'en' | 'es'; +/** localStorage key for the user's explicitly chosen language */ +export const PREFERRED_LANG_KEY = 'preferred_lang' as const; + export function getLang(url: URL): Lang { const [, lang] = url.pathname.split('/'); if (lang === 'es') return 'es'; From 5314100099ecdadf180cdd7ea30d4116c5f93a23 Mon Sep 17 00:00:00 2001 From: Jesus Ordosgoitty Date: Sat, 11 Apr 2026 12:02:28 -0400 Subject: [PATCH 2/2] feat: integrate Google Analytics and update environment variable prefix to PUBLIC_ --- .env.example | 3 +-- src/components/BaseHead.astro | 24 ++++++++++++++++++++---- src/lib/analytics.ts | 4 +++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/.env.example b/.env.example index dc761b2..d8fa78e 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1 @@ -# .env -VITE_GA_MEASUREMENT_ID=G-XXXXXXXXXX +PUBLIC_GA_MEASUREMENT_ID=G-XXXXXXXXXX diff --git a/src/components/BaseHead.astro b/src/components/BaseHead.astro index bcf9b3c..3a5bafe 100644 --- a/src/components/BaseHead.astro +++ b/src/components/BaseHead.astro @@ -55,7 +55,23 @@ const esURL = lang === 'es' ? canonicalURL.href : (alternateUrl ?? new URL('/es/ - + +{ + import.meta.env.PUBLIC_GA_MEASUREMENT_ID && ( + <> + + + ) +} diff --git a/src/lib/analytics.ts b/src/lib/analytics.ts index 8040eb1..94e64e0 100644 --- a/src/lib/analytics.ts +++ b/src/lib/analytics.ts @@ -1,7 +1,7 @@ // src/lib/analytics.ts // Utility for Google Analytics event tracking -const GA_MEASUREMENT_ID = import.meta.env.VITE_GA_MEASUREMENT_ID; +const GA_MEASUREMENT_ID = import.meta.env.PUBLIC_GA_MEASUREMENT_ID; export function trackEvent({ action, @@ -20,6 +20,8 @@ export function trackEvent({ event_label: label, value: value, }); + } else if (import.meta.env.DEV) { + console.log('[Analytics]', { action, category, label, value }); } }