@enhanced-inc/corede-i-sdk 1.0.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 +113 -0
- package/dist/index.d.mts +18 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.global.js +7 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +7 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +45 -0
package/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# COREDE-I SDK
|
|
2
|
+
|
|
3
|
+
Google Analytics integration SDK for COREDE-I Portal clients.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install corede-i-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or load directly via `<script>` tag:
|
|
12
|
+
|
|
13
|
+
```html
|
|
14
|
+
<script src="https://your-cdn.com/corede-i-sdk/dist/index.global.js"></script>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### ES Modules / TypeScript
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { init } from "corede-i-sdk";
|
|
23
|
+
|
|
24
|
+
init({ apiKey: "ep_your_api_key_here" });
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### CommonJS
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
const { init } = require("corede-i-sdk");
|
|
31
|
+
|
|
32
|
+
init({ apiKey: "ep_your_api_key_here" });
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Script Tag (IIFE)
|
|
36
|
+
|
|
37
|
+
```html
|
|
38
|
+
<script src="https://your-cdn.com/corede-i-sdk/dist/index.global.js"></script>
|
|
39
|
+
<script>
|
|
40
|
+
CoredeISdk.init({ apiKey: "ep_your_api_key_here" });
|
|
41
|
+
</script>
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Configuration
|
|
45
|
+
|
|
46
|
+
| Option | Type | Required | Description |
|
|
47
|
+
| ---------------- | --------- | -------- | ------------------------------------------------------------------------------ |
|
|
48
|
+
| `apiKey` | `string` | Yes | Your plan's API key (from dashboard) |
|
|
49
|
+
| `baseUrl` | `string` | No | Override the portal URL (defaults to the production URL baked in at build time) |
|
|
50
|
+
| `consentBanner` | `boolean` | No | Show the built-in cookie consent banner (default: `true`) |
|
|
51
|
+
|
|
52
|
+
## Cookie Consent
|
|
53
|
+
|
|
54
|
+
The SDK requires user consent before loading Google Analytics. By default it shows a built-in consent banner (in Japanese). The user's choice is saved in `localStorage` so they are only asked once.
|
|
55
|
+
|
|
56
|
+
### Default — built-in banner
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
init({ apiKey: "ep_your_api_key_here" });
|
|
60
|
+
// Banner appears automatically if consent has not been given yet
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Manual consent — use your own UI
|
|
64
|
+
|
|
65
|
+
If your site already has a cookie consent flow, disable the built-in banner and call `acceptAnalytics()` or `declineAnalytics()` yourself:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { init, acceptAnalytics, declineAnalytics } from "corede-i-sdk";
|
|
69
|
+
|
|
70
|
+
init({ apiKey: "ep_your_api_key_here", consentBanner: false });
|
|
71
|
+
|
|
72
|
+
// Later, after the user makes a choice in your own consent UI:
|
|
73
|
+
acceptAnalytics(); // enables Google Analytics
|
|
74
|
+
// or
|
|
75
|
+
declineAnalytics(); // opts out
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
For the IIFE build:
|
|
79
|
+
|
|
80
|
+
```html
|
|
81
|
+
<script src="https://your-cdn.com/corede-i-sdk/dist/index.global.js"></script>
|
|
82
|
+
<script>
|
|
83
|
+
CoredeISdk.init({ apiKey: "ep_your_api_key_here", consentBanner: false });
|
|
84
|
+
|
|
85
|
+
// In your own consent handler:
|
|
86
|
+
CoredeISdk.acceptAnalytics();
|
|
87
|
+
// or
|
|
88
|
+
CoredeISdk.declineAnalytics();
|
|
89
|
+
</script>
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Building Locally
|
|
93
|
+
|
|
94
|
+
Set the `COREDE_I_BASE_URL` environment variable before building to point the SDK at a local portal instance:
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
COREDE_I_BASE_URL=http://localhost:5000 npm run build
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
In production/CI, omit the variable — the SDK will use the hardcoded production URL.
|
|
101
|
+
|
|
102
|
+
## How It Works
|
|
103
|
+
|
|
104
|
+
1. `init()` sends a request to the portal API with your API key
|
|
105
|
+
2. If the plan is **Active** and has Google Analytics enabled, the SDK waits for user consent before injecting gtag.js
|
|
106
|
+
3. If the plan is inactive or has no GA configured, the SDK silently does nothing
|
|
107
|
+
|
|
108
|
+
The SDK includes the following safeguards:
|
|
109
|
+
|
|
110
|
+
- **Cookie consent** — GA is never loaded without explicit user consent (built-in banner or manual API)
|
|
111
|
+
- **Double-injection guard** — gtag.js is never injected more than once
|
|
112
|
+
- **GA ID validation** — only IDs matching the `G-XXXXXXXXXX` format are accepted
|
|
113
|
+
- **Graceful degradation** — any network or configuration error is logged as a warning without breaking your site
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
interface CoredeISdkConfig {
|
|
2
|
+
apiKey: string;
|
|
3
|
+
/** Override the default portal URL. Defaults to COREDE_I_BASE_URL env var at build time, or the production URL. */
|
|
4
|
+
baseUrl?: string;
|
|
5
|
+
/** Show the built-in cookie consent banner. Defaults to true. Set to false to manage consent yourself via acceptAnalytics() / declineAnalytics(). */
|
|
6
|
+
consentBanner?: boolean;
|
|
7
|
+
}
|
|
8
|
+
interface SdkConfigResponse {
|
|
9
|
+
hasGoogleAnalytics: boolean;
|
|
10
|
+
googleAnalyticsId: string | null;
|
|
11
|
+
status: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare function acceptAnalytics(): void;
|
|
15
|
+
declare function declineAnalytics(): void;
|
|
16
|
+
declare function init(config: CoredeISdkConfig): void;
|
|
17
|
+
|
|
18
|
+
export { type CoredeISdkConfig, type SdkConfigResponse, acceptAnalytics, declineAnalytics, init };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
interface CoredeISdkConfig {
|
|
2
|
+
apiKey: string;
|
|
3
|
+
/** Override the default portal URL. Defaults to COREDE_I_BASE_URL env var at build time, or the production URL. */
|
|
4
|
+
baseUrl?: string;
|
|
5
|
+
/** Show the built-in cookie consent banner. Defaults to true. Set to false to manage consent yourself via acceptAnalytics() / declineAnalytics(). */
|
|
6
|
+
consentBanner?: boolean;
|
|
7
|
+
}
|
|
8
|
+
interface SdkConfigResponse {
|
|
9
|
+
hasGoogleAnalytics: boolean;
|
|
10
|
+
googleAnalyticsId: string | null;
|
|
11
|
+
status: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare function acceptAnalytics(): void;
|
|
15
|
+
declare function declineAnalytics(): void;
|
|
16
|
+
declare function init(config: CoredeISdkConfig): void;
|
|
17
|
+
|
|
18
|
+
export { type CoredeISdkConfig, type SdkConfigResponse, acceptAnalytics, declineAnalytics, init };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";var CoredeISdk=(()=>{var c=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var C=(e,n)=>{for(var t in n)c(e,t,{get:n[t],enumerable:!0})},y=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of m(n))!x.call(e,o)&&o!==t&&c(e,o,{get:()=>n[o],enumerable:!(r=g(n,o))||r.enumerable});return e};var E=e=>y(c({},"__esModule",{value:!0}),e);var k={};C(k,{acceptAnalytics:()=>A,declineAnalytics:()=>v,init:()=>D});var h=process.env.ENHANCED_PORTAL_BASE_URL||"enhanced-portal-web-gylronoezvgom.azurewebsites.net",p="corede-i-gtag",b=/^G-[A-Z0-9]+$/,f="corede-i-ga-consent",u="corede-i-consent-banner";async function w(e,n){try{let t=await fetch(`${n}/api/sdk/config`,{headers:{"X-Api-Key":e}});return t.ok?await t.json():(console.warn(`[COREDE-I SDK] Config fetch failed: ${t.status}`),null)}catch(t){return console.warn("[COREDE-I SDK] Config fetch error:",t),null}}function d(e){if(document.getElementById(p))return;if(!b.test(e)){console.warn(`[COREDE-I SDK] Invalid GA ID format: ${e}`);return}let n=document.createElement("script");n.id=p,n.async=!0,n.src=`https://www.googletagmanager.com/gtag/js?id=${e}`,document.head.appendChild(n);let t=document.createElement("script");t.textContent=`
|
|
2
|
+
window.dataLayer = window.dataLayer || [];
|
|
3
|
+
function gtag(){dataLayer.push(arguments);}
|
|
4
|
+
gtag('js', new Date());
|
|
5
|
+
gtag('config', '${e}');
|
|
6
|
+
`,document.head.appendChild(t)}function S(){let e=localStorage.getItem(f);return e==="true"?!0:e==="false"?!1:null}function i(e){localStorage.setItem(f,String(e))}function I(e){if(document.getElementById(u))return;let n=document.createElement("div");n.id=u,n.style.cssText=["position:fixed","bottom:0","left:0","right:0","background:#1a1a2e","color:#eee","padding:16px 20px","display:flex","align-items:center","justify-content:space-between","gap:16px","font-family:sans-serif","font-size:14px","z-index:2147483647","box-shadow:0 -2px 8px rgba(0,0,0,0.3)"].join(";");let t=document.createElement("span");t.textContent="\u5F53\u30B5\u30A4\u30C8\u3067\u306F\u3001\u30A2\u30AF\u30BB\u30B9\u89E3\u6790\u306E\u305F\u3081\u306BGoogle Analytics\u3092\u4F7F\u7528\u3057\u3066\u3044\u307E\u3059\u3002Cookie\u306E\u4F7F\u7528\u306B\u540C\u610F\u3044\u305F\u3060\u3051\u307E\u3059\u304B\uFF1F";let r=document.createElement("span");r.style.cssText="display:flex;gap:8px;flex-shrink:0";let o=document.createElement("button");o.textContent="\u540C\u610F\u3059\u308B",o.style.cssText="background:#4caf50;color:#fff;border:none;padding:8px 16px;border-radius:4px;cursor:pointer;font-size:14px";let s=document.createElement("button");s.textContent="\u62D2\u5426\u3059\u308B",s.style.cssText="background:transparent;color:#ccc;border:1px solid #555;padding:8px 16px;border-radius:4px;cursor:pointer;font-size:14px";let l=()=>n.remove();o.addEventListener("click",()=>{i(!0),l(),e()}),s.addEventListener("click",()=>{i(!1),l()}),r.appendChild(o),r.appendChild(s),n.appendChild(t),n.appendChild(r),document.body.appendChild(n)}var a=null;function A(){i(!0),a&&d(a)}function v(){i(!1),a=null}function D(e){if(!e.apiKey){console.warn("[COREDE-I SDK] Missing apiKey");return}let n=e.consentBanner!==!1,t=(e.baseUrl||h).replace(/\/+$/,"");w(e.apiKey,t).then(r=>{if(!r||!r.googleAnalyticsId)return;let o=r.googleAnalyticsId;a=o;let s=S();s===!0?d(o):s===null&&n&&I(()=>d(o))})}return E(k);})();
|
|
7
|
+
//# sourceMappingURL=index.global.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { CoredeISdkConfig, SdkConfigResponse } from \"./types\";\n\nexport type { CoredeISdkConfig, SdkConfigResponse };\n\nconst DEFAULT_BASE_URL = process.env.ENHANCED_PORTAL_BASE_URL ||\n \"enhanced-portal-web-gylronoezvgom.azurewebsites.net\";\nconst GA_SCRIPT_ID = \"corede-i-gtag\";\nconst GA_ID_PATTERN = /^G-[A-Z0-9]+$/;\nconst CONSENT_KEY = \"corede-i-ga-consent\";\nconst BANNER_ID = \"corede-i-consent-banner\";\n\nasync function fetchConfig(\n apiKey: string,\n baseUrl: string\n): Promise<SdkConfigResponse | null> {\n try {\n const res = await fetch(`${baseUrl}/api/sdk/config`, {\n headers: { \"X-Api-Key\": apiKey },\n });\n if (!res.ok) {\n console.warn(`[COREDE-I SDK] Config fetch failed: ${res.status}`);\n return null;\n }\n return (await res.json()) as SdkConfigResponse;\n } catch (err) {\n console.warn(\"[COREDE-I SDK] Config fetch error:\", err);\n return null;\n }\n}\n\nfunction injectGtag(gaId: string): void {\n if (document.getElementById(GA_SCRIPT_ID)) {\n return;\n }\n\n if (!GA_ID_PATTERN.test(gaId)) {\n console.warn(`[COREDE-I SDK] Invalid GA ID format: ${gaId}`);\n return;\n }\n\n const script = document.createElement(\"script\");\n script.id = GA_SCRIPT_ID;\n script.async = true;\n script.src = `https://www.googletagmanager.com/gtag/js?id=${gaId}`;\n document.head.appendChild(script);\n\n const initScript = document.createElement(\"script\");\n initScript.textContent = `\n window.dataLayer = window.dataLayer || [];\n function gtag(){dataLayer.push(arguments);}\n gtag('js', new Date());\n gtag('config', '${gaId}');\n `;\n document.head.appendChild(initScript);\n}\n\nfunction getConsent(): boolean | null {\n const value = localStorage.getItem(CONSENT_KEY);\n if (value === \"true\") return true;\n if (value === \"false\") return false;\n return null;\n}\n\nfunction setConsent(accepted: boolean): void {\n localStorage.setItem(CONSENT_KEY, String(accepted));\n}\n\nfunction showConsentBanner(onAccept: () => void): void {\n if (document.getElementById(BANNER_ID)) return;\n\n const banner = document.createElement(\"div\");\n banner.id = BANNER_ID;\n banner.style.cssText = [\n \"position:fixed\",\n \"bottom:0\",\n \"left:0\",\n \"right:0\",\n \"background:#1a1a2e\",\n \"color:#eee\",\n \"padding:16px 20px\",\n \"display:flex\",\n \"align-items:center\",\n \"justify-content:space-between\",\n \"gap:16px\",\n \"font-family:sans-serif\",\n \"font-size:14px\",\n \"z-index:2147483647\",\n \"box-shadow:0 -2px 8px rgba(0,0,0,0.3)\",\n ].join(\";\");\n\n const text = document.createElement(\"span\");\n text.textContent =\n \"当サイトでは、アクセス解析のためにGoogle Analyticsを使用しています。Cookieの使用に同意いただけますか?\";\n\n const btnGroup = document.createElement(\"span\");\n btnGroup.style.cssText = \"display:flex;gap:8px;flex-shrink:0\";\n\n const acceptBtn = document.createElement(\"button\");\n acceptBtn.textContent = \"同意する\";\n acceptBtn.style.cssText =\n \"background:#4caf50;color:#fff;border:none;padding:8px 16px;border-radius:4px;cursor:pointer;font-size:14px\";\n\n const declineBtn = document.createElement(\"button\");\n declineBtn.textContent = \"拒否する\";\n declineBtn.style.cssText =\n \"background:transparent;color:#ccc;border:1px solid #555;padding:8px 16px;border-radius:4px;cursor:pointer;font-size:14px\";\n\n const removeBanner = () => banner.remove();\n\n acceptBtn.addEventListener(\"click\", () => {\n setConsent(true);\n removeBanner();\n onAccept();\n });\n\n declineBtn.addEventListener(\"click\", () => {\n setConsent(false);\n removeBanner();\n });\n\n btnGroup.appendChild(acceptBtn);\n btnGroup.appendChild(declineBtn);\n banner.appendChild(text);\n banner.appendChild(btnGroup);\n document.body.appendChild(banner);\n}\n\nlet pendingGaId: string | null = null;\n\nexport function acceptAnalytics(): void {\n setConsent(true);\n if (pendingGaId) {\n injectGtag(pendingGaId);\n }\n}\n\nexport function declineAnalytics(): void {\n setConsent(false);\n pendingGaId = null;\n}\n\nexport function init(config: CoredeISdkConfig): void {\n if (!config.apiKey) {\n console.warn(\"[COREDE-I SDK] Missing apiKey\");\n return;\n }\n\n const showBanner = config.consentBanner !== false;\n const baseUrl = (config.baseUrl || DEFAULT_BASE_URL).replace(/\\/+$/, \"\");\n\n fetchConfig(config.apiKey, baseUrl).then((sdkConfig) => {\n if (!sdkConfig) return;\n if (!sdkConfig.googleAnalyticsId) return;\n\n const gaId = sdkConfig.googleAnalyticsId;\n pendingGaId = gaId;\n const consent = getConsent();\n\n if (consent === true) {\n injectGtag(gaId);\n } else if (consent === null && showBanner) {\n showConsentBanner(() => injectGtag(gaId));\n }\n // consent === false or (null + no banner): wait for manual acceptAnalytics()\n });\n}\n"],"mappings":"8bAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,qBAAAE,EAAA,qBAAAC,EAAA,SAAAC,IAIA,IAAMC,EAAmB,QAAQ,IAAI,0BACnC,sDACIC,EAAe,gBACfC,EAAgB,gBAChBC,EAAc,sBACdC,EAAY,0BAElB,eAAeC,EACbC,EACAC,EACmC,CACnC,GAAI,CACF,IAAMC,EAAM,M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","init","DEFAULT_BASE_URL","GA_SCRIPT_ID","GA_ID_PATTERN","CONSENT_KEY","BANNER_ID","fetchConfig","apiKey","baseUrl","res","err","injectGtag","gaId","script","initScript","getConsent","value","setConsent","accepted","showConsentBanner","onAccept","banner","text","btnGroup","acceptBtn","declineBtn","removeBanner","pendingGaId","config","showBanner","sdkConfig","consent"]}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";var c=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var C=(e,n)=>{for(var t in n)c(e,t,{get:n[t],enumerable:!0})},y=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of m(n))!x.call(e,o)&&o!==t&&c(e,o,{get:()=>n[o],enumerable:!(r=g(n,o))||r.enumerable});return e};var E=e=>y(c({},"__esModule",{value:!0}),e);var k={};C(k,{acceptAnalytics:()=>A,declineAnalytics:()=>v,init:()=>D});module.exports=E(k);var h=process.env.ENHANCED_PORTAL_BASE_URL||"enhanced-portal-web-gylronoezvgom.azurewebsites.net",p="corede-i-gtag",b=/^G-[A-Z0-9]+$/,f="corede-i-ga-consent",u="corede-i-consent-banner";async function w(e,n){try{let t=await fetch(`${n}/api/sdk/config`,{headers:{"X-Api-Key":e}});return t.ok?await t.json():(console.warn(`[COREDE-I SDK] Config fetch failed: ${t.status}`),null)}catch(t){return console.warn("[COREDE-I SDK] Config fetch error:",t),null}}function d(e){if(document.getElementById(p))return;if(!b.test(e)){console.warn(`[COREDE-I SDK] Invalid GA ID format: ${e}`);return}let n=document.createElement("script");n.id=p,n.async=!0,n.src=`https://www.googletagmanager.com/gtag/js?id=${e}`,document.head.appendChild(n);let t=document.createElement("script");t.textContent=`
|
|
2
|
+
window.dataLayer = window.dataLayer || [];
|
|
3
|
+
function gtag(){dataLayer.push(arguments);}
|
|
4
|
+
gtag('js', new Date());
|
|
5
|
+
gtag('config', '${e}');
|
|
6
|
+
`,document.head.appendChild(t)}function S(){let e=localStorage.getItem(f);return e==="true"?!0:e==="false"?!1:null}function i(e){localStorage.setItem(f,String(e))}function I(e){if(document.getElementById(u))return;let n=document.createElement("div");n.id=u,n.style.cssText=["position:fixed","bottom:0","left:0","right:0","background:#1a1a2e","color:#eee","padding:16px 20px","display:flex","align-items:center","justify-content:space-between","gap:16px","font-family:sans-serif","font-size:14px","z-index:2147483647","box-shadow:0 -2px 8px rgba(0,0,0,0.3)"].join(";");let t=document.createElement("span");t.textContent="\u5F53\u30B5\u30A4\u30C8\u3067\u306F\u3001\u30A2\u30AF\u30BB\u30B9\u89E3\u6790\u306E\u305F\u3081\u306BGoogle Analytics\u3092\u4F7F\u7528\u3057\u3066\u3044\u307E\u3059\u3002Cookie\u306E\u4F7F\u7528\u306B\u540C\u610F\u3044\u305F\u3060\u3051\u307E\u3059\u304B\uFF1F";let r=document.createElement("span");r.style.cssText="display:flex;gap:8px;flex-shrink:0";let o=document.createElement("button");o.textContent="\u540C\u610F\u3059\u308B",o.style.cssText="background:#4caf50;color:#fff;border:none;padding:8px 16px;border-radius:4px;cursor:pointer;font-size:14px";let s=document.createElement("button");s.textContent="\u62D2\u5426\u3059\u308B",s.style.cssText="background:transparent;color:#ccc;border:1px solid #555;padding:8px 16px;border-radius:4px;cursor:pointer;font-size:14px";let l=()=>n.remove();o.addEventListener("click",()=>{i(!0),l(),e()}),s.addEventListener("click",()=>{i(!1),l()}),r.appendChild(o),r.appendChild(s),n.appendChild(t),n.appendChild(r),document.body.appendChild(n)}var a=null;function A(){i(!0),a&&d(a)}function v(){i(!1),a=null}function D(e){if(!e.apiKey){console.warn("[COREDE-I SDK] Missing apiKey");return}let n=e.consentBanner!==!1,t=(e.baseUrl||h).replace(/\/+$/,"");w(e.apiKey,t).then(r=>{if(!r||!r.googleAnalyticsId)return;let o=r.googleAnalyticsId;a=o;let s=S();s===!0?d(o):s===null&&n&&I(()=>d(o))})}0&&(module.exports={acceptAnalytics,declineAnalytics,init});
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { CoredeISdkConfig, SdkConfigResponse } from \"./types\";\n\nexport type { CoredeISdkConfig, SdkConfigResponse };\n\nconst DEFAULT_BASE_URL = process.env.ENHANCED_PORTAL_BASE_URL ||\n \"enhanced-portal-web-gylronoezvgom.azurewebsites.net\";\nconst GA_SCRIPT_ID = \"corede-i-gtag\";\nconst GA_ID_PATTERN = /^G-[A-Z0-9]+$/;\nconst CONSENT_KEY = \"corede-i-ga-consent\";\nconst BANNER_ID = \"corede-i-consent-banner\";\n\nasync function fetchConfig(\n apiKey: string,\n baseUrl: string\n): Promise<SdkConfigResponse | null> {\n try {\n const res = await fetch(`${baseUrl}/api/sdk/config`, {\n headers: { \"X-Api-Key\": apiKey },\n });\n if (!res.ok) {\n console.warn(`[COREDE-I SDK] Config fetch failed: ${res.status}`);\n return null;\n }\n return (await res.json()) as SdkConfigResponse;\n } catch (err) {\n console.warn(\"[COREDE-I SDK] Config fetch error:\", err);\n return null;\n }\n}\n\nfunction injectGtag(gaId: string): void {\n if (document.getElementById(GA_SCRIPT_ID)) {\n return;\n }\n\n if (!GA_ID_PATTERN.test(gaId)) {\n console.warn(`[COREDE-I SDK] Invalid GA ID format: ${gaId}`);\n return;\n }\n\n const script = document.createElement(\"script\");\n script.id = GA_SCRIPT_ID;\n script.async = true;\n script.src = `https://www.googletagmanager.com/gtag/js?id=${gaId}`;\n document.head.appendChild(script);\n\n const initScript = document.createElement(\"script\");\n initScript.textContent = `\n window.dataLayer = window.dataLayer || [];\n function gtag(){dataLayer.push(arguments);}\n gtag('js', new Date());\n gtag('config', '${gaId}');\n `;\n document.head.appendChild(initScript);\n}\n\nfunction getConsent(): boolean | null {\n const value = localStorage.getItem(CONSENT_KEY);\n if (value === \"true\") return true;\n if (value === \"false\") return false;\n return null;\n}\n\nfunction setConsent(accepted: boolean): void {\n localStorage.setItem(CONSENT_KEY, String(accepted));\n}\n\nfunction showConsentBanner(onAccept: () => void): void {\n if (document.getElementById(BANNER_ID)) return;\n\n const banner = document.createElement(\"div\");\n banner.id = BANNER_ID;\n banner.style.cssText = [\n \"position:fixed\",\n \"bottom:0\",\n \"left:0\",\n \"right:0\",\n \"background:#1a1a2e\",\n \"color:#eee\",\n \"padding:16px 20px\",\n \"display:flex\",\n \"align-items:center\",\n \"justify-content:space-between\",\n \"gap:16px\",\n \"font-family:sans-serif\",\n \"font-size:14px\",\n \"z-index:2147483647\",\n \"box-shadow:0 -2px 8px rgba(0,0,0,0.3)\",\n ].join(\";\");\n\n const text = document.createElement(\"span\");\n text.textContent =\n \"当サイトでは、アクセス解析のためにGoogle Analyticsを使用しています。Cookieの使用に同意いただけますか?\";\n\n const btnGroup = document.createElement(\"span\");\n btnGroup.style.cssText = \"display:flex;gap:8px;flex-shrink:0\";\n\n const acceptBtn = document.createElement(\"button\");\n acceptBtn.textContent = \"同意する\";\n acceptBtn.style.cssText =\n \"background:#4caf50;color:#fff;border:none;padding:8px 16px;border-radius:4px;cursor:pointer;font-size:14px\";\n\n const declineBtn = document.createElement(\"button\");\n declineBtn.textContent = \"拒否する\";\n declineBtn.style.cssText =\n \"background:transparent;color:#ccc;border:1px solid #555;padding:8px 16px;border-radius:4px;cursor:pointer;font-size:14px\";\n\n const removeBanner = () => banner.remove();\n\n acceptBtn.addEventListener(\"click\", () => {\n setConsent(true);\n removeBanner();\n onAccept();\n });\n\n declineBtn.addEventListener(\"click\", () => {\n setConsent(false);\n removeBanner();\n });\n\n btnGroup.appendChild(acceptBtn);\n btnGroup.appendChild(declineBtn);\n banner.appendChild(text);\n banner.appendChild(btnGroup);\n document.body.appendChild(banner);\n}\n\nlet pendingGaId: string | null = null;\n\nexport function acceptAnalytics(): void {\n setConsent(true);\n if (pendingGaId) {\n injectGtag(pendingGaId);\n }\n}\n\nexport function declineAnalytics(): void {\n setConsent(false);\n pendingGaId = null;\n}\n\nexport function init(config: CoredeISdkConfig): void {\n if (!config.apiKey) {\n console.warn(\"[COREDE-I SDK] Missing apiKey\");\n return;\n }\n\n const showBanner = config.consentBanner !== false;\n const baseUrl = (config.baseUrl || DEFAULT_BASE_URL).replace(/\\/+$/, \"\");\n\n fetchConfig(config.apiKey, baseUrl).then((sdkConfig) => {\n if (!sdkConfig) return;\n if (!sdkConfig.googleAnalyticsId) return;\n\n const gaId = sdkConfig.googleAnalyticsId;\n pendingGaId = gaId;\n const consent = getConsent();\n\n if (consent === true) {\n injectGtag(gaId);\n } else if (consent === null && showBanner) {\n showConsentBanner(() => injectGtag(gaId));\n }\n // consent === false or (null + no banner): wait for manual acceptAnalytics()\n });\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,qBAAAE,EAAA,qBAAAC,EAAA,SAAAC,IAAA,eAAAC,EAAAL,GAIA,IAAMM,EAAmB,QAAQ,IAAI,0BACnC,sDACIC,EAAe,gBACfC,EAAgB,gBAChBC,EAAc,sBACdC,EAAY,0BAElB,eAAeC,EACbC,EACAC,EACmC,CACnC,GAAI,CACF,IAAMC,EAAM,MAAM,MAAM,GAAGD,CAAO,kBAAmB,CACnD,QAAS,CAAE,YAAaD,CAAO,CACjC,CAAC,EACD,OAAKE,EAAI,GAID,MAAMA,EAAI,KAAK,GAHrB,QAAQ,KAAK,uCAAuCA,EAAI,MAAM,EAAE,EACzD,KAGX,OAASC,EAAK,CACZ,eAAQ,KAAK,qCAAsCA,CAAG,EAC/C,IACT,CACF,CAEA,SAASC,EAAWC,EAAoB,CACtC,GAAI,SAAS,eAAeV,CAAY,EACtC,OAGF,GAAI,CAACC,EAAc,KAAKS,CAAI,EAAG,CAC7B,QAAQ,KAAK,wCAAwCA,CAAI,EAAE,EAC3D,MACF,CAEA,IAAMC,EAAS,SAAS,cAAc,QAAQ,EAC9CA,EAAO,GAAKX,EACZW,EAAO,MAAQ,GACfA,EAAO,IAAM,+CAA+CD,CAAI,GAChE,SAAS,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,KAAK,YAAYF,CAAM,CAClC,CAEA,IAAIM,EAA6B,KAE1B,SAAS9B,GAAwB,CACtCoB,EAAW,EAAI,EACXU,GACFhB,EAAWgB,CAAW,CAE1B,CAEO,SAAS7B,GAAyB,CACvCmB,EAAW,EAAK,EAChBU,EAAc,IAChB,CAEO,SAAS5B,EAAK6B,EAAgC,CACnD,GAAI,CAACA,EAAO,OAAQ,CAClB,QAAQ,KAAK,+BAA+B,EAC5C,MACF,CAEA,IAAMC,EAAaD,EAAO,gBAAkB,GACtCpB,GAAWoB,EAAO,SAAW3B,GAAkB,QAAQ,OAAQ,EAAE,EAEvEK,EAAYsB,EAAO,OAAQpB,CAAO,EAAE,KAAMsB,GAAc,CAEtD,GADI,CAACA,GACD,CAACA,EAAU,kBAAmB,OAElC,IAAMlB,EAAOkB,EAAU,kBACvBH,EAAcf,EACd,IAAMmB,EAAUhB,EAAW,EAEvBgB,IAAY,GACdpB,EAAWC,CAAI,EACNmB,IAAY,MAAQF,GAC7BV,EAAkB,IAAMR,EAAWC,CAAI,CAAC,CAG5C,CAAC,CACH","names":["index_exports","__export","acceptAnalytics","declineAnalytics","init","__toCommonJS","DEFAULT_BASE_URL","GA_SCRIPT_ID","GA_ID_PATTERN","CONSENT_KEY","BANNER_ID","fetchConfig","apiKey","baseUrl","res","err","injectGtag","gaId","script","initScript","getConsent","value","setConsent","accepted","showConsentBanner","onAccept","banner","text","btnGroup","acceptBtn","declineBtn","removeBanner","pendingGaId","config","showBanner","sdkConfig","consent"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
var f=process.env.ENHANCED_PORTAL_BASE_URL||"enhanced-portal-web-gylronoezvgom.azurewebsites.net",l="corede-i-gtag",g=/^G-[A-Z0-9]+$/,u="corede-i-ga-consent",p="corede-i-consent-banner";async function m(e,n){try{let t=await fetch(`${n}/api/sdk/config`,{headers:{"X-Api-Key":e}});return t.ok?await t.json():(console.warn(`[COREDE-I SDK] Config fetch failed: ${t.status}`),null)}catch(t){return console.warn("[COREDE-I SDK] Config fetch error:",t),null}}function c(e){if(document.getElementById(l))return;if(!g.test(e)){console.warn(`[COREDE-I SDK] Invalid GA ID format: ${e}`);return}let n=document.createElement("script");n.id=l,n.async=!0,n.src=`https://www.googletagmanager.com/gtag/js?id=${e}`,document.head.appendChild(n);let t=document.createElement("script");t.textContent=`
|
|
2
|
+
window.dataLayer = window.dataLayer || [];
|
|
3
|
+
function gtag(){dataLayer.push(arguments);}
|
|
4
|
+
gtag('js', new Date());
|
|
5
|
+
gtag('config', '${e}');
|
|
6
|
+
`,document.head.appendChild(t)}function x(){let e=localStorage.getItem(u);return e==="true"?!0:e==="false"?!1:null}function i(e){localStorage.setItem(u,String(e))}function C(e){if(document.getElementById(p))return;let n=document.createElement("div");n.id=p,n.style.cssText=["position:fixed","bottom:0","left:0","right:0","background:#1a1a2e","color:#eee","padding:16px 20px","display:flex","align-items:center","justify-content:space-between","gap:16px","font-family:sans-serif","font-size:14px","z-index:2147483647","box-shadow:0 -2px 8px rgba(0,0,0,0.3)"].join(";");let t=document.createElement("span");t.textContent="\u5F53\u30B5\u30A4\u30C8\u3067\u306F\u3001\u30A2\u30AF\u30BB\u30B9\u89E3\u6790\u306E\u305F\u3081\u306BGoogle Analytics\u3092\u4F7F\u7528\u3057\u3066\u3044\u307E\u3059\u3002Cookie\u306E\u4F7F\u7528\u306B\u540C\u610F\u3044\u305F\u3060\u3051\u307E\u3059\u304B\uFF1F";let o=document.createElement("span");o.style.cssText="display:flex;gap:8px;flex-shrink:0";let r=document.createElement("button");r.textContent="\u540C\u610F\u3059\u308B",r.style.cssText="background:#4caf50;color:#fff;border:none;padding:8px 16px;border-radius:4px;cursor:pointer;font-size:14px";let s=document.createElement("button");s.textContent="\u62D2\u5426\u3059\u308B",s.style.cssText="background:transparent;color:#ccc;border:1px solid #555;padding:8px 16px;border-radius:4px;cursor:pointer;font-size:14px";let d=()=>n.remove();r.addEventListener("click",()=>{i(!0),d(),e()}),s.addEventListener("click",()=>{i(!1),d()}),o.appendChild(r),o.appendChild(s),n.appendChild(t),n.appendChild(o),document.body.appendChild(n)}var a=null;function y(){i(!0),a&&c(a)}function E(){i(!1),a=null}function h(e){if(!e.apiKey){console.warn("[COREDE-I SDK] Missing apiKey");return}let n=e.consentBanner!==!1,t=(e.baseUrl||f).replace(/\/+$/,"");m(e.apiKey,t).then(o=>{if(!o||!o.googleAnalyticsId)return;let r=o.googleAnalyticsId;a=r;let s=x();s===!0?c(r):s===null&&n&&C(()=>c(r))})}export{y as acceptAnalytics,E as declineAnalytics,h as init};
|
|
7
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { CoredeISdkConfig, SdkConfigResponse } from \"./types\";\n\nexport type { CoredeISdkConfig, SdkConfigResponse };\n\nconst DEFAULT_BASE_URL = process.env.ENHANCED_PORTAL_BASE_URL ||\n \"enhanced-portal-web-gylronoezvgom.azurewebsites.net\";\nconst GA_SCRIPT_ID = \"corede-i-gtag\";\nconst GA_ID_PATTERN = /^G-[A-Z0-9]+$/;\nconst CONSENT_KEY = \"corede-i-ga-consent\";\nconst BANNER_ID = \"corede-i-consent-banner\";\n\nasync function fetchConfig(\n apiKey: string,\n baseUrl: string\n): Promise<SdkConfigResponse | null> {\n try {\n const res = await fetch(`${baseUrl}/api/sdk/config`, {\n headers: { \"X-Api-Key\": apiKey },\n });\n if (!res.ok) {\n console.warn(`[COREDE-I SDK] Config fetch failed: ${res.status}`);\n return null;\n }\n return (await res.json()) as SdkConfigResponse;\n } catch (err) {\n console.warn(\"[COREDE-I SDK] Config fetch error:\", err);\n return null;\n }\n}\n\nfunction injectGtag(gaId: string): void {\n if (document.getElementById(GA_SCRIPT_ID)) {\n return;\n }\n\n if (!GA_ID_PATTERN.test(gaId)) {\n console.warn(`[COREDE-I SDK] Invalid GA ID format: ${gaId}`);\n return;\n }\n\n const script = document.createElement(\"script\");\n script.id = GA_SCRIPT_ID;\n script.async = true;\n script.src = `https://www.googletagmanager.com/gtag/js?id=${gaId}`;\n document.head.appendChild(script);\n\n const initScript = document.createElement(\"script\");\n initScript.textContent = `\n window.dataLayer = window.dataLayer || [];\n function gtag(){dataLayer.push(arguments);}\n gtag('js', new Date());\n gtag('config', '${gaId}');\n `;\n document.head.appendChild(initScript);\n}\n\nfunction getConsent(): boolean | null {\n const value = localStorage.getItem(CONSENT_KEY);\n if (value === \"true\") return true;\n if (value === \"false\") return false;\n return null;\n}\n\nfunction setConsent(accepted: boolean): void {\n localStorage.setItem(CONSENT_KEY, String(accepted));\n}\n\nfunction showConsentBanner(onAccept: () => void): void {\n if (document.getElementById(BANNER_ID)) return;\n\n const banner = document.createElement(\"div\");\n banner.id = BANNER_ID;\n banner.style.cssText = [\n \"position:fixed\",\n \"bottom:0\",\n \"left:0\",\n \"right:0\",\n \"background:#1a1a2e\",\n \"color:#eee\",\n \"padding:16px 20px\",\n \"display:flex\",\n \"align-items:center\",\n \"justify-content:space-between\",\n \"gap:16px\",\n \"font-family:sans-serif\",\n \"font-size:14px\",\n \"z-index:2147483647\",\n \"box-shadow:0 -2px 8px rgba(0,0,0,0.3)\",\n ].join(\";\");\n\n const text = document.createElement(\"span\");\n text.textContent =\n \"当サイトでは、アクセス解析のためにGoogle Analyticsを使用しています。Cookieの使用に同意いただけますか?\";\n\n const btnGroup = document.createElement(\"span\");\n btnGroup.style.cssText = \"display:flex;gap:8px;flex-shrink:0\";\n\n const acceptBtn = document.createElement(\"button\");\n acceptBtn.textContent = \"同意する\";\n acceptBtn.style.cssText =\n \"background:#4caf50;color:#fff;border:none;padding:8px 16px;border-radius:4px;cursor:pointer;font-size:14px\";\n\n const declineBtn = document.createElement(\"button\");\n declineBtn.textContent = \"拒否する\";\n declineBtn.style.cssText =\n \"background:transparent;color:#ccc;border:1px solid #555;padding:8px 16px;border-radius:4px;cursor:pointer;font-size:14px\";\n\n const removeBanner = () => banner.remove();\n\n acceptBtn.addEventListener(\"click\", () => {\n setConsent(true);\n removeBanner();\n onAccept();\n });\n\n declineBtn.addEventListener(\"click\", () => {\n setConsent(false);\n removeBanner();\n });\n\n btnGroup.appendChild(acceptBtn);\n btnGroup.appendChild(declineBtn);\n banner.appendChild(text);\n banner.appendChild(btnGroup);\n document.body.appendChild(banner);\n}\n\nlet pendingGaId: string | null = null;\n\nexport function acceptAnalytics(): void {\n setConsent(true);\n if (pendingGaId) {\n injectGtag(pendingGaId);\n }\n}\n\nexport function declineAnalytics(): void {\n setConsent(false);\n pendingGaId = null;\n}\n\nexport function init(config: CoredeISdkConfig): void {\n if (!config.apiKey) {\n console.warn(\"[COREDE-I SDK] Missing apiKey\");\n return;\n }\n\n const showBanner = config.consentBanner !== false;\n const baseUrl = (config.baseUrl || DEFAULT_BASE_URL).replace(/\\/+$/, \"\");\n\n fetchConfig(config.apiKey, baseUrl).then((sdkConfig) => {\n if (!sdkConfig) return;\n if (!sdkConfig.googleAnalyticsId) return;\n\n const gaId = sdkConfig.googleAnalyticsId;\n pendingGaId = gaId;\n const consent = getConsent();\n\n if (consent === true) {\n injectGtag(gaId);\n } else if (consent === null && showBanner) {\n showConsentBanner(() => injectGtag(gaId));\n }\n // consent === false or (null + no banner): wait for manual acceptAnalytics()\n });\n}\n"],"mappings":"AAIA,IAAMA,EAAmB,QAAQ,IAAI,0BACnC,sDACIC,EAAe,gBACfC,EAAgB,gBAChBC,EAAc,sBACdC,EAAY,0BAElB,eAAeC,EACbC,EACAC,EACmC,CACnC,GAAI,CACF,IAAMC,EAAM,MAAM,MAAM,GAAGD,CAAO,kBAAmB,CACnD,QAAS,CAAE,YAAaD,CAAO,CACjC,CAAC,EACD,OAAKE,EAAI,GAID,MAAMA,EAAI,KAAK,GAHrB,QAAQ,KAAK,uCAAuCA,EAAI,MAAM,EAAE,EACzD,KAGX,OAASC,EAAK,CACZ,eAAQ,KAAK,qCAAsCA,CAAG,EAC/C,IACT,CACF,CAEA,SAASC,EAAWC,EA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AAS,IAAM,CACxCP,EAAW,EAAI,EACfS,EAAa,EACbN,EAAS,CACX,CAAC,EAEDK,EAAW,iBAAiB,QAAS,IAAM,CACzCR,EAAW,EAAK,EAChBS,EAAa,CACf,CAAC,EAEDH,EAAS,YAAYC,CAAS,EAC9BD,EAAS,YAAYE,CAAU,EAC/BJ,EAAO,YAAYC,CAAI,EACvBD,EAAO,YAAYE,CAAQ,EAC3B,SAAS,KAAK,YAAYF,CAAM,CAClC,CAEA,IAAIM,EAA6B,KAE1B,SAASC,GAAwB,CACtCX,EAAW,EAAI,EACXU,GACFhB,EAAWgB,CAAW,CAE1B,CAEO,SAASE,GAAyB,CACvCZ,EAAW,EAAK,EAChBU,EAAc,IAChB,CAEO,SAASG,EAAKC,EAAgC,CACnD,GAAI,CAACA,EAAO,OAAQ,CAClB,QAAQ,KAAK,+BAA+B,EAC5C,MACF,CAEA,IAAMC,EAAaD,EAAO,gBAAkB,GACtCvB,GAAWuB,EAAO,SAAW9B,GAAkB,QAAQ,OAAQ,EAAE,EAEvEK,EAAYyB,EAAO,OAAQvB,CAAO,EAAE,KAAMyB,GAAc,CAEtD,GADI,CAACA,GACD,CAACA,EAAU,kBAAmB,OAElC,IAAMrB,EAAOqB,EAAU,kBACvBN,EAAcf,EACd,IAAMsB,EAAUnB,EAAW,EAEvBmB,IAAY,GACdvB,EAAWC,CAAI,EACNsB,IAAY,MAAQF,GAC7Bb,EAAkB,IAAMR,EAAWC,CAAI,CAAC,CAG5C,CAAC,CACH","names":["DEFAULT_BASE_URL","GA_SCRIPT_ID","GA_ID_PATTERN","CONSENT_KEY","BANNER_ID","fetchConfig","apiKey","baseUrl","res","err","injectGtag","gaId","script","initScript","getConsent","value","setConsent","accepted","showConsentBanner","onAccept","banner","text","btnGroup","acceptBtn","declineBtn","removeBanner","pendingGaId","acceptAnalytics","declineAnalytics","init","config","showBanner","sdkConfig","consent"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@enhanced-inc/corede-i-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": "Enhanced-Inc",
|
|
5
|
+
"description": "COREDE-I SDK for Google Analytics integration",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"module": "dist/index.mjs",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./global": "./dist/index.global.js"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup",
|
|
22
|
+
"dev": "tsup --watch",
|
|
23
|
+
"test:local": "tsup && npx serve .",
|
|
24
|
+
"prepublishOnly": "npm run build"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/Enhanced-Inc/Enhanced-Portals.git",
|
|
29
|
+
"directory": "COREDE-I-SDK"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"corede-i",
|
|
33
|
+
"google-analytics",
|
|
34
|
+
"analytics",
|
|
35
|
+
"sdk",
|
|
36
|
+
"tracking"
|
|
37
|
+
],
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "^25.5.0",
|
|
40
|
+
"dotenv": "^16.4.0",
|
|
41
|
+
"tsup": "^8.4.0",
|
|
42
|
+
"typescript": "^5.7.0"
|
|
43
|
+
},
|
|
44
|
+
"license": "Apache-2.0"
|
|
45
|
+
}
|