@ampless/plugin-analytics-ga4 0.1.1-alpha.0 → 0.2.0-alpha.2

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/dist/index.d.ts CHANGED
@@ -2,13 +2,19 @@ import { AmplessPlugin } from 'ampless';
2
2
 
3
3
  interface AnalyticsGa4Options {
4
4
  /**
5
- * Google Analytics 4 measurement ID, e.g. `"G-XXXXXXXX"`. An empty
6
- * string disables the plugin (descriptors return `[]`), which is
7
- * the recommended way to keep the dependency wired up while
8
- * temporarily turning analytics off for example in staging or
9
- * before consent is granted.
5
+ * Optional fallback Google Analytics 4 measurement ID, e.g.
6
+ * `"G-XXXXXXXX"`. Used only when no admin-stored value exists in
7
+ * `pk='siteconfig', sk='plugins.<instanceId>.measurementId'`. An
8
+ * empty string disables the plugin (descriptors return `[]`),
9
+ * which is the recommended way to keep the dependency wired up
10
+ * while temporarily turning analytics off — for example in
11
+ * staging or before consent is granted.
12
+ *
13
+ * The constructor argument will be removed in Phase 3. New
14
+ * deployments should leave it empty and configure the value from
15
+ * the admin UI.
10
16
  */
11
- measurementId: string;
17
+ measurementId?: string;
12
18
  /**
13
19
  * Optional namespace for this instance. Defaults to
14
20
  * `'analytics-ga4'`. Set distinct values when registering the
@@ -23,6 +29,6 @@ interface AnalyticsGa4Options {
23
29
  * it does not need any AWS data permissions because everything
24
30
  * happens inside the public Next.js process at render time.
25
31
  */
26
- declare function analyticsGa4Plugin(options: AnalyticsGa4Options): AmplessPlugin;
32
+ declare function analyticsGa4Plugin(options?: AnalyticsGa4Options): AmplessPlugin;
27
33
 
28
34
  export { type AnalyticsGa4Options, analyticsGa4Plugin as default };
package/dist/index.js CHANGED
@@ -1,22 +1,46 @@
1
1
  // src/index.ts
2
2
  import { definePlugin } from "ampless";
3
- function analyticsGa4Plugin(options) {
4
- const { measurementId, instanceId = "analytics-ga4" } = options;
3
+ function analyticsGa4Plugin(options = {}) {
4
+ const { measurementId = "", instanceId = "analytics-ga4" } = options;
5
5
  return definePlugin({
6
6
  name: "analytics-ga4",
7
7
  instanceId,
8
8
  displayName: { en: "Google Analytics 4", ja: "Google Analytics 4" },
9
9
  apiVersion: 1,
10
10
  trust_level: "untrusted",
11
- capabilities: ["publicHead"],
12
- publicHead() {
13
- if (!measurementId) return [];
11
+ capabilities: ["publicHead", "adminSettings"],
12
+ settings: {
13
+ public: [
14
+ {
15
+ type: "text",
16
+ key: "measurementId",
17
+ label: {
18
+ en: "Measurement ID",
19
+ ja: "\u6E2C\u5B9A ID"
20
+ },
21
+ description: {
22
+ en: "GA4 measurement ID (e.g. G-XXXXXXXX). Leave blank to disable the plugin without removing it from cms.config.",
23
+ ja: "GA4 \u306E\u6E2C\u5B9A ID (\u4F8B: G-XXXXXXXX)\u3002\u7A7A\u306B\u3059\u308B\u3068 cms.config \u304B\u3089\u524A\u9664\u305B\u305A\u306B\u30D7\u30E9\u30B0\u30A4\u30F3\u3092\u7121\u52B9\u5316\u3067\u304D\u307E\u3059\u3002"
24
+ },
25
+ // Accept either empty (disable) or a well-formed GA4
26
+ // measurement id. The runtime's `publicHead` reads the
27
+ // resolved value through ctx.setting and skips emitting
28
+ // descriptors when it's empty.
29
+ pattern: "^$|^G-[A-Z0-9]+$",
30
+ placeholder: "G-XXXXXXXX",
31
+ default: measurementId
32
+ }
33
+ ]
34
+ },
35
+ publicHead(ctx) {
36
+ const id = (ctx.setting("measurementId") ?? "").trim();
37
+ if (!id) return [];
14
38
  return [
15
39
  {
16
40
  type: "script",
17
41
  id: `ga4-loader-${instanceId}`,
18
42
  src: `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(
19
- measurementId
43
+ id
20
44
  )}`,
21
45
  strategy: "afterInteractive"
22
46
  },
@@ -32,7 +56,7 @@ function analyticsGa4Plugin(options) {
32
56
  "window.dataLayer = window.dataLayer || [];",
33
57
  "function gtag(){dataLayer.push(arguments);}",
34
58
  "gtag('js', new Date());",
35
- `gtag('config', ${JSON.stringify(measurementId)});`
59
+ `gtag('config', ${JSON.stringify(id)});`
36
60
  ].join("\n")
37
61
  }
38
62
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ampless/plugin-analytics-ga4",
3
- "version": "0.1.1-alpha.0",
3
+ "version": "0.2.0-alpha.2",
4
4
  "description": "Google Analytics 4 plugin for ampless",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -25,7 +25,7 @@
25
25
  "homepage": "https://github.com/heavymoons/ampless/tree/main/packages/plugin-analytics-ga4#readme",
26
26
  "bugs": "https://github.com/heavymoons/ampless/issues",
27
27
  "dependencies": {
28
- "ampless": "1.0.0-alpha.17"
28
+ "ampless": "1.0.0-alpha.19"
29
29
  },
30
30
  "keywords": [
31
31
  "ampless",