serge-pro-dev

analytics

by serge-pro-dev
payload-plugins
Analytics
v4
^4.0.0-beta.12
MIT

Overview

Summary generated from the README

Adds an Analytics Settings global to Payload 4 that injects analytics into a Next.js frontend without code changes. Editors manage a Google tag, custom scripts and pixels, and UTM attribution cookies from the admin. You render three server components in the root layout. Requires Payload 4 beta and Next.js.

Features

  • Single field for Google tag IDs: GTM, GA4, Google tag or Google Ads
  • Custom script snippets placed in the head or before the closing body tag
  • Noscript fallbacks and tracking pixels rendered at the end of the body
  • UTM first-touch and last-touch cookies with 1 to 365 day lifetime
  • Consent categories per group: not required, analytics or marketing
  • Server-side rendering with cache revalidation on save and skipped in draft mode

Use cases

  • Add Google Tag Manager or GA4 to a Next.js site without editing code
  • Let editors paste marketing pixels and gate them behind visitor consent
  • Capture UTM attribution cookies for use by CRM or messaging plugins

From the author

Analytics for Payload CMS 4 and Next.js: Google tag, custom scripts and pixels, UTM attribution cookies, with consent categories.

Installation

npm i @serge-pro-dev/payload-plugin-analytics

0

Stars

0

Forks

0

Open Issues

Yesterday

Last Updated

627

Weekly Downloads

v0.1.0-beta.2

npm Version

91.1 KB

Unpacked Size

2

Dependencies

Repository stars

00 since Nov 29

Show data
Repository stars by day (UTC)
DateStars
Dec 6, 20260
Dec 5, 20260
Dec 4, 20260
Dec 3, 20260
Dec 2, 20260
Dec 1, 20260
Nov 30, 20260
Nov 29, 20260

Shared by 6 plugins in serge-pro-dev/payload-plugins.

npm weekly downloads

Trends appear after 7 days of data. Collecting since Dec 4 — 3 daily snapshots so far.

Each point is npm's rolling 7-day download count.

Health Score

77/100Excellent

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1479182369-analytics.svg)](https://payloaddirectory.dev/plugins/1479182369-analytics)
HTML
<a href="https://payloaddirectory.dev/plugins/1479182369-analytics"><img src="https://payloaddirectory.dev/badge/1479182369-analytics.svg" alt="Payload Directory"></a>
Updated Dec 4, 2026Created Dec 1, 2026Published Dec 4, 2026

README

@serge-pro-dev/payload-plugin-analytics

Русская версия

A Payload 4 global (Analytics Settings) that adds analytics to a Next.js site without code changes: Google tags, your own scripts and pixels, and UTM attribution cookies. Every group of scripts has a consent category that the consent plugin respects.

Analytics Settings

Install

pnpm add @serge-pro-dev/payload-plugin-analytics
// payload.config.ts
import { analyticsPlugin } from '@serge-pro-dev/payload-plugin-analytics'

plugins: [analyticsPlugin()]
// src/app/(frontend)/layout.tsx  (the folder may be named (app) in some templates)
import config from '@payload-config'
import { Analytics, AnalyticsBody, AnalyticsHead } from '@serge-pro-dev/payload-plugin-analytics/rsc'
import { draftMode } from 'next/headers'

export default async function RootLayout({ children }: { children: React.ReactNode }) {
  const { isEnabled } = await draftMode()

  return (
    <html>
      <head>{!isEnabled && <AnalyticsHead config={config} />}</head>
      <body>
        <Providers>
          {!isEnabled && <Analytics config={config} />}
          {children}
        </Providers>
        {!isEnabled && <AnalyticsBody config={config} />}
      </body>
    </html>
  )
}

isEnabled is Next.js draft mode (Payload live preview); the scripts are skipped there. Render {children} only once. Then run pnpm payload generate:types.

ComponentRenders
AnalyticsHeadcustom <script> tags placed in the head
AnalyticsGoogle tag and the UTM writer
AnalyticsBodycustom <script> tags placed before </body> and all <noscript> blocks

Features

  • Google tag in one field: GTM-… (Tag Manager), G-… (GA4), GT-… (Google tag) or AW-… (Google Ads). Use one ID; if GA4 already lives in Tag Manager, enter only the container.
  • Custom JavaScript: paste provider snippets. <script> tags (inline and external, data-* attributes kept) go to <head> or before </body>; <noscript> fallbacks and tracking pixels always go to the end of <body>. Other markup is ignored.
  • UTM tracking: writes the utm_first_touch / utm_last_touch cookies that the Telegram and Bitrix24 plugins read. Lifetime 1–365 days.
  • Consent categories: each group (Google tag, custom scripts, UTM) can be not required, analytics or marketing. They wait for consent when the consent plugin runs in Notice or Opt-in mode; with the consent plugin missing or Off, scripts run as usual.

Scripts are rendered on the server and cached; saving the global revalidates the cache (revalidateTag('global_analytics')). Nothing is rendered in Next.js draft mode.

Security notes

  • Custom JavaScript runs on every page for every visitor. Only users who can edit the global (any logged-in user) can change it, so give admin access carefully.
  • The global is publicly readable (read: () => true) because the site reads it.
  • Snippets are parsed with parse5 on the server; max size 50 000 characters.

Admin language

Labels and descriptions are available in English and Russian. Enable both in the project: i18n: { supportedLanguages: { en, ru } }.

Database

New global analytics. Development: schema push. Production: pnpm payload migrate:create and migrate.

MIT

Similar plugins