UNPKG

strapi-google-analytics-dashboard

Version:

A plug-and-play Google Analytics 4 dashboard for Strapi 5.x. No code required — just install, configure your credentials, and instantly start tracking GA metrics directly in your admin panel.

98 lines (97 loc) 3.6 kB
import { jsx, jsxs } from "react/jsx-runtime"; import { useState, useEffect } from "react"; import { Box, Flex, Typography, Field, TextInput, Textarea, Button } from "@strapi/design-system"; import { C as CenteredLoader } from "./CenterLoader-BiaLjoNX.mjs"; const Settings = () => { const [propertyId, setPropertyId] = useState(""); const [measurementId, setMeasurementId] = useState(""); const [credentials, setCredentials] = useState(""); const [loading, setLoading] = useState(true); useEffect(() => { fetch("/api/strapi-google-analytics-dashboard/settings").then((res) => res.json()).then((data) => { if (data) { setPropertyId(data.propertyId || ""); setMeasurementId(data.measurementId || ""); setCredentials(JSON.stringify(data.credentials || {}, null, 2)); } }).catch(console.error).finally(() => setLoading(false)); }, []); const saveSettings = async () => { try { await fetch("/api/strapi-google-analytics-dashboard/settings", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ propertyId, measurementId, credentials: JSON.parse(credentials) }) }); alert("Settings saved successfully!"); } catch (error) { alert("Error saving settings. Check your JSON credentials."); } }; if (loading) return /* @__PURE__ */ jsx(CenteredLoader, {}); return /* @__PURE__ */ jsx(Box, { padding: 8, background: "neutral0", shadow: "tableShadow", hasRadius: true, children: /* @__PURE__ */ jsxs(Flex, { direction: "column", alignItems: "stretch", gap: 4, maxWidth: "600px", children: [ /* @__PURE__ */ jsx(Typography, { variant: "beta", as: "h2", children: "Google Analytics Settings" }), /* @__PURE__ */ jsxs( Field.Root, { id: "propertyId", hint: "Enter your Google Analytics Property ID.", children: [ /* @__PURE__ */ jsx(Field.Label, { children: "GA Property ID" }), /* @__PURE__ */ jsx( TextInput, { label: "Property ID", name: "propertyId", onChange: (e) => setPropertyId(e.target.value), value: propertyId } ), /* @__PURE__ */ jsx(Field.Hint, {}) ] } ), /* @__PURE__ */ jsxs( Field.Root, { id: "measurementId", hint: "Measurement ID is optional but recommended for enhanced tracking.", children: [ /* @__PURE__ */ jsx(Field.Label, { children: "GA Measurement ID" }), /* @__PURE__ */ jsx( TextInput, { label: "Measurement ID", name: "measurementId", onChange: (e) => setMeasurementId(e.target.value), value: measurementId } ), /* @__PURE__ */ jsx(Field.Hint, {}) ] } ), /* @__PURE__ */ jsxs(Field.Root, { id: "credentials", hint: "Paste your Google Analytics Service Account JSON here.", children: [ /* @__PURE__ */ jsx(Field.Label, { children: "GA Service Account JSON" }), /* @__PURE__ */ jsx( Textarea, { label: "GA Service Account JSON", name: "credentials", onChange: (e) => setCredentials(e.target.value), value: credentials, rows: 10 } ), /* @__PURE__ */ jsx(Field.Hint, {}) ] }), /* @__PURE__ */ jsx(Button, { onClick: saveSettings, children: "Save" }) ] }) }); }; export { Settings as default };