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.

199 lines (198 loc) 5.49 kB
"use strict"; const data = require("@google-analytics/data"); const bootstrap = ({ strapi: strapi2 }) => { }; const destroy = ({ strapi: strapi2 }) => { }; const register = ({ strapi: strapi2 }) => { }; const config = { default: {}, validator() { } }; const contentTypes = {}; function getDateRange(range) { const today = /* @__PURE__ */ new Date(); const start = new Date(today); const days = parseInt(range.replace("d", ""), 10) || 7; start.setDate(today.getDate() - days + 1); return { startDate: start.toISOString().split("T")[0], endDate: today.toISOString().split("T")[0] }; } const getChartData = async (chartType, range) => { const pluginStore = strapi.store({ type: "plugin", name: "strapi-google-analytics-dashboard" }); const settings = await pluginStore.get({ key: "settings" }); if (!settings || !settings.propertyId || !settings.credentials) { return { error: true, message: "Google Analytics plugin is not configured. Please provide propertyId and credentials in settings." }; } const { propertyId, credentials } = settings; const client = new data.BetaAnalyticsDataClient({ credentials }); const { startDate, endDate } = getDateRange(range); let request; switch (chartType) { case "overview": request = { property: `properties/${propertyId}`, dateRanges: [{ startDate, endDate }], dimensions: [{ name: "date" }], metrics: [{ name: "activeUsers" }, { name: "sessions" }, { name: "bounceRate" }, { name: "screenPageViews" }, { name: "engagementRate" }, { name: "newUsers" }] }; break; case "users-over-time": request = { property: `properties/${propertyId}`, dateRanges: [{ startDate, endDate }], dimensions: [{ name: "date" }], metrics: [{ name: "activeUsers" }] }; break; case "users-by-country": request = { property: `properties/${propertyId}`, dateRanges: [{ startDate, endDate }], dimensions: [{ name: "country" }], metrics: [{ name: "activeUsers" }], //orderBys: [{ fieldName: 'activeUsers', sortOrder: 'DESCENDING' }], limit: 10 }; break; case "users-by-device": request = { property: `properties/${propertyId}`, dateRanges: [{ startDate, endDate }], dimensions: [{ name: "deviceCategory" }], metrics: [{ name: "activeUsers" }] }; break; case "sessions-by-source": request = { property: `properties/${propertyId}`, dateRanges: [{ startDate, endDate }], dimensions: [{ name: "sessionSource" }], metrics: [{ name: "sessions" }], //orderBys: [{ fieldName: 'sessions', sortOrder: 'DESCENDING' }], limit: 10 }; break; case "pageviews-by-path": request = { property: `properties/${propertyId}`, dateRanges: [{ startDate, endDate }], dimensions: [{ name: "pagePath" }], metrics: [{ name: "screenPageViews" }], //orderBys: [{ fieldName: 'screenPageViews', sortOrder: 'DESCENDING' }], limit: 10 }; break; default: return { error: "Unknown chart type" }; } try { const [response] = await client.runReport(request); return response; } catch (err) { return { error: true, message: "Invalid Google Analytics credentials or property ID." }; } }; const analyticsService = { getChartData }; const controller = ({ strapi: strapi2 }) => ({ /*index(ctx) { ctx.body = strapi .plugin('strapi-google-analytics-dashboard') // the name of the service file & the method. .service('service') .getWelcomeMessage(); },*/ async index(ctx) { const { chartType } = ctx.params; const range = ctx.query.range || "7d"; const data2 = await analyticsService.getChartData(chartType, range); ctx.send(data2); }, async getSettings(ctx) { const pluginStore = strapi2.store({ type: "plugin", name: "strapi-google-analytics-dashboard" }); const settings = await pluginStore.get({ key: "settings" }) || {}; ctx.send(settings); }, async updateSettings(ctx) { const pluginStore = strapi2.store({ type: "plugin", name: "strapi-google-analytics-dashboard" }); await pluginStore.set({ key: "settings", value: ctx.request.body }); ctx.send({ success: true }); } }); const controllers = { controller }; const middlewares = {}; const policies = {}; const contentAPIRoutes = [ /*{ method: 'GET', path: '/', // name of the controller file & the method. handler: 'controller.index', config: { policies: [], }, },*/ { method: "GET", path: "/charts/:chartType", handler: "controller.index", config: { auth: false } }, { method: "GET", path: "/settings", handler: "controller.getSettings", config: { auth: false } }, { method: "PUT", path: "/settings", handler: "controller.updateSettings", config: { auth: false } } ]; const routes = { "content-api": { type: "content-api", routes: contentAPIRoutes } }; const service = ({ strapi: strapi2 }) => ({ getWelcomeMessage() { return "Welcome to Strapi 🚀"; } }); const services = { service }; const index = { register, bootstrap, destroy, config, controllers, routes, services, contentTypes, policies, middlewares }; module.exports = index;