brease-client 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -107,6 +107,39 @@ export default {
107
107
  }
108
108
  ```
109
109
 
110
+ ### Google Analytics (GA4)
111
+
112
+ When GA4 is linked in Brease (Connections → Google) with "Add the tag to the site" on, the site response carries
113
+ its measurement id. Render it once in the root layout; turning it off in Brease removes it without a deploy.
114
+
115
+ ```tsx
116
+ // app/layout.tsx
117
+ import Script from 'next/script'
118
+ import { ga4Tag } from 'brease-client'
119
+ import { brease } from '@/lib/brease'
120
+
121
+ export default async function RootLayout({ children }: { children: React.ReactNode }) {
122
+ const ga = ga4Tag(await brease.getSite())
123
+ return (
124
+ <html>
125
+ <body>
126
+ {children}
127
+ {ga && (
128
+ <>
129
+ <Script src={ga.src} strategy="afterInteractive" />
130
+ <Script id="brease-ga4" strategy="afterInteractive">{ga.inline}</Script>
131
+ </>
132
+ )}
133
+ </body>
134
+ </html>
135
+ )
136
+ }
137
+ ```
138
+
139
+ Consent mode starts denied: nothing is stored until your cookie banner calls `grantAnalytics()` (and
140
+ `grantAnalytics(false)` when consent is withdrawn). Without a banner, GA4 only receives cookieless pings. For
141
+ Astro or plain HTML, put `ga4Html(site)` in the `<head>`.
142
+
110
143
  ### Recommended structure (optional)
111
144
 
112
145
  Brease works with any routing: `getPage()` and friends don't care how a site is built. This structure is what Brease recommends, and what its editor moves a site towards when the site follows Brease conventions. It makes locales a setting: adding one, or changing how URLs are prefixed, needs no code change.
package/dist/astro.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { HeadOptions } from './html.cjs';
2
2
  export { HeadTag, headTagList, headTags } from './html.cjs';
3
- import { R as ResolvedSeo } from './types-V0ELFvwy.cjs';
3
+ import { R as ResolvedSeo } from './types-ecfDXrLa.cjs';
4
4
 
5
5
  declare function breaseHead(seo: ResolvedSeo, opts?: HeadOptions): string;
6
6
 
package/dist/astro.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { HeadOptions } from './html.js';
2
2
  export { HeadTag, headTagList, headTags } from './html.js';
3
- import { R as ResolvedSeo } from './types-V0ELFvwy.js';
3
+ import { R as ResolvedSeo } from './types-ecfDXrLa.js';
4
4
 
5
5
  declare function breaseHead(seo: ResolvedSeo, opts?: HeadOptions): string;
6
6
 
@@ -73,6 +73,29 @@ function resolvePath(settings, pathname) {
73
73
  return { slug: clean, locale: settings.defaultLocale };
74
74
  }
75
75
 
76
+ // src/analytics.ts
77
+ var MEASUREMENT_ID = /^G-[A-Z0-9]+$/;
78
+ function ga4Tag(site) {
79
+ const id = site.analytics?.ga4;
80
+ if (!id || !MEASUREMENT_ID.test(id)) return null;
81
+ const inline = [
82
+ "window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}",
83
+ "gtag('consent','default',{ad_storage:'denied',ad_user_data:'denied',ad_personalization:'denied',analytics_storage:'denied',wait_for_update:500});",
84
+ "gtag('js',new Date());",
85
+ `gtag('config','${id}');`
86
+ ].join("");
87
+ return { id, src: `https://www.googletagmanager.com/gtag/js?id=${id}`, inline };
88
+ }
89
+ function ga4Html(site) {
90
+ const tag = ga4Tag(site);
91
+ return tag ? `<script async src="${tag.src}"></script>
92
+ <script>${tag.inline}</script>` : "";
93
+ }
94
+ function grantAnalytics(granted = true) {
95
+ const gtag = globalThis.gtag;
96
+ gtag?.("consent", "update", { analytics_storage: granted ? "granted" : "denied" });
97
+ }
98
+
76
99
  // src/locales.ts
77
100
  function localeLinks(context) {
78
101
  const { settings, alternates, homes, locale: current } = context;
@@ -219,4 +242,4 @@ function pathOf(url) {
219
242
  }
220
243
  }
221
244
 
222
- export { BreaseError, buildPath, createBrease, env, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath };
245
+ export { BreaseError, buildPath, createBrease, env, ga4Html, ga4Tag, grantAnalytics, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath };
package/dist/html.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { R as ResolvedSeo } from './types-V0ELFvwy.cjs';
1
+ import { R as ResolvedSeo } from './types-ecfDXrLa.cjs';
2
2
 
3
3
  type HeadOptions = {
4
4
  xDefault?: string;
package/dist/html.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { R as ResolvedSeo } from './types-V0ELFvwy.js';
1
+ import { R as ResolvedSeo } from './types-ecfDXrLa.js';
2
2
 
3
3
  type HeadOptions = {
4
4
  xDefault?: string;
package/dist/index.cjs CHANGED
@@ -77,6 +77,29 @@ function resolvePath(settings, pathname) {
77
77
  return { slug: clean, locale: settings.defaultLocale };
78
78
  }
79
79
 
80
+ // src/analytics.ts
81
+ var MEASUREMENT_ID = /^G-[A-Z0-9]+$/;
82
+ function ga4Tag(site) {
83
+ const id = site.analytics?.ga4;
84
+ if (!id || !MEASUREMENT_ID.test(id)) return null;
85
+ const inline = [
86
+ "window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}",
87
+ "gtag('consent','default',{ad_storage:'denied',ad_user_data:'denied',ad_personalization:'denied',analytics_storage:'denied',wait_for_update:500});",
88
+ "gtag('js',new Date());",
89
+ `gtag('config','${id}');`
90
+ ].join("");
91
+ return { id, src: `https://www.googletagmanager.com/gtag/js?id=${id}`, inline };
92
+ }
93
+ function ga4Html(site) {
94
+ const tag = ga4Tag(site);
95
+ return tag ? `<script async src="${tag.src}"></script>
96
+ <script>${tag.inline}</script>` : "";
97
+ }
98
+ function grantAnalytics(granted = true) {
99
+ const gtag = globalThis.gtag;
100
+ gtag?.("consent", "update", { analytics_storage: granted ? "granted" : "denied" });
101
+ }
102
+
80
103
  // src/locales.ts
81
104
  function localeLinks(context) {
82
105
  const { settings, alternates, homes, locale: current } = context;
@@ -305,6 +328,9 @@ exports.BreaseError = BreaseError;
305
328
  exports.buildPath = buildPath;
306
329
  exports.checkSections = checkSections;
307
330
  exports.createBrease = createBrease;
331
+ exports.ga4Html = ga4Html;
332
+ exports.ga4Tag = ga4Tag;
333
+ exports.grantAnalytics = grantAnalytics;
308
334
  exports.localeHref = localeHref;
309
335
  exports.localeLinks = localeLinks;
310
336
  exports.nativeName = nativeName;
package/dist/index.d.cts CHANGED
@@ -1,5 +1,14 @@
1
- import { U as UrlSettings, S as Site, P as Page, a as PageRef, N as Navigation, b as Redirect, c as Release, d as SitemapEntry } from './types-V0ELFvwy.cjs';
2
- export { L as LocaleStrategy, e as NavigationItem, O as OpenGraph, f as PageSeo, R as ResolvedSeo, g as Robots, h as SiteSeo, T as Twitter } from './types-V0ELFvwy.cjs';
1
+ import { S as Site, U as UrlSettings, P as Page, a as PageRef, N as Navigation, b as Redirect, c as Release, d as SitemapEntry } from './types-ecfDXrLa.cjs';
2
+ export { L as LocaleStrategy, e as NavigationItem, O as OpenGraph, f as PageSeo, R as ResolvedSeo, g as Robots, h as SiteSeo, T as Twitter } from './types-ecfDXrLa.cjs';
3
+
4
+ type Ga4Tag = {
5
+ id: string;
6
+ src: string;
7
+ inline: string;
8
+ };
9
+ declare function ga4Tag(site: Pick<Site, 'analytics'>): Ga4Tag | null;
10
+ declare function ga4Html(site: Pick<Site, 'analytics'>): string;
11
+ declare function grantAnalytics(granted?: boolean): void;
3
12
 
4
13
  declare class BreaseError extends Error {
5
14
  code: string;
@@ -90,4 +99,4 @@ interface Brease {
90
99
  }
91
100
  declare function createBrease(opts?: BreaseOptions): Brease;
92
101
 
93
- export { type Brease, BreaseError, type BreaseOptions, type BreaseSection, type LocaleContext, type LocaleLink, Navigation, Page, PageRef, Redirect, Release, Site, SitemapEntry, UrlSettings, buildPath, checkSections, createBrease, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath, sectionAttrs, sectionsAttrs };
102
+ export { type Brease, BreaseError, type BreaseOptions, type BreaseSection, type Ga4Tag, type LocaleContext, type LocaleLink, Navigation, Page, PageRef, Redirect, Release, Site, SitemapEntry, UrlSettings, buildPath, checkSections, createBrease, ga4Html, ga4Tag, grantAnalytics, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath, sectionAttrs, sectionsAttrs };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,14 @@
1
- import { U as UrlSettings, S as Site, P as Page, a as PageRef, N as Navigation, b as Redirect, c as Release, d as SitemapEntry } from './types-V0ELFvwy.js';
2
- export { L as LocaleStrategy, e as NavigationItem, O as OpenGraph, f as PageSeo, R as ResolvedSeo, g as Robots, h as SiteSeo, T as Twitter } from './types-V0ELFvwy.js';
1
+ import { S as Site, U as UrlSettings, P as Page, a as PageRef, N as Navigation, b as Redirect, c as Release, d as SitemapEntry } from './types-ecfDXrLa.js';
2
+ export { L as LocaleStrategy, e as NavigationItem, O as OpenGraph, f as PageSeo, R as ResolvedSeo, g as Robots, h as SiteSeo, T as Twitter } from './types-ecfDXrLa.js';
3
+
4
+ type Ga4Tag = {
5
+ id: string;
6
+ src: string;
7
+ inline: string;
8
+ };
9
+ declare function ga4Tag(site: Pick<Site, 'analytics'>): Ga4Tag | null;
10
+ declare function ga4Html(site: Pick<Site, 'analytics'>): string;
11
+ declare function grantAnalytics(granted?: boolean): void;
3
12
 
4
13
  declare class BreaseError extends Error {
5
14
  code: string;
@@ -90,4 +99,4 @@ interface Brease {
90
99
  }
91
100
  declare function createBrease(opts?: BreaseOptions): Brease;
92
101
 
93
- export { type Brease, BreaseError, type BreaseOptions, type BreaseSection, type LocaleContext, type LocaleLink, Navigation, Page, PageRef, Redirect, Release, Site, SitemapEntry, UrlSettings, buildPath, checkSections, createBrease, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath, sectionAttrs, sectionsAttrs };
102
+ export { type Brease, BreaseError, type BreaseOptions, type BreaseSection, type Ga4Tag, type LocaleContext, type LocaleLink, Navigation, Page, PageRef, Redirect, Release, Site, SitemapEntry, UrlSettings, buildPath, checkSections, createBrease, ga4Html, ga4Tag, grantAnalytics, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath, sectionAttrs, sectionsAttrs };
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export { BreaseError, buildPath, createBrease, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath } from './chunk-OA7GXHT3.js';
1
+ export { BreaseError, buildPath, createBrease, ga4Html, ga4Tag, grantAnalytics, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath } from './chunk-7BBL2LET.js';
2
2
  export { checkSections, sectionAttrs, sectionsAttrs } from './chunk-CQZLTZGN.js';
3
3
  import './chunk-PKBMQBKP.js';
package/dist/next.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Brease } from './index.cjs';
2
- import { R as ResolvedSeo } from './types-V0ELFvwy.cjs';
2
+ import { R as ResolvedSeo } from './types-ecfDXrLa.cjs';
3
3
 
4
4
  type BreaseMetadata = {
5
5
  title: {
package/dist/next.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Brease } from './index.js';
2
- import { R as ResolvedSeo } from './types-V0ELFvwy.js';
2
+ import { R as ResolvedSeo } from './types-ecfDXrLa.js';
3
3
 
4
4
  type BreaseMetadata = {
5
5
  title: {
package/dist/next.js CHANGED
@@ -1,4 +1,4 @@
1
- import { env, createBrease } from './chunk-OA7GXHT3.js';
1
+ import { env, createBrease } from './chunk-7BBL2LET.js';
2
2
  import './chunk-CQZLTZGN.js';
3
3
  import './chunk-PKBMQBKP.js';
4
4
 
@@ -66,6 +66,9 @@ type Site = {
66
66
  localeStrategy: LocaleStrategy;
67
67
  urlTemplate: string | null;
68
68
  seo: Record<string, SiteSeo>;
69
+ analytics?: {
70
+ ga4: string;
71
+ } | null;
69
72
  };
70
73
  type Page<T = Record<string, unknown>> = {
71
74
  pageId: string;
@@ -66,6 +66,9 @@ type Site = {
66
66
  localeStrategy: LocaleStrategy;
67
67
  urlTemplate: string | null;
68
68
  seo: Record<string, SiteSeo>;
69
+ analytics?: {
70
+ ga4: string;
71
+ } | null;
69
72
  };
70
73
  type Page<T = Record<string, unknown>> = {
71
74
  pageId: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brease-client",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Brease delivery API client for Next.js, Astro and any fetch-capable runtime",
5
5
  "license": "MIT",
6
6
  "repository": {