@walkeros/server-destination-datamanager 0.3.1 → 0.4.1

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
@@ -23,7 +23,7 @@ npm install @walkeros/server-destination-datamanager
23
23
 
24
24
  ## Quick Start
25
25
 
26
- ### Minimal Configuration
26
+ ### Minimal Configuration (AWS Lambda / Serverless)
27
27
 
28
28
  ```typescript
29
29
  import { destinationDataManager } from '@walkeros/server-destination-datamanager';
@@ -35,8 +35,11 @@ const { collector, elb } = await startFlow({
35
35
  ...destinationDataManager,
36
36
  config: {
37
37
  settings: {
38
- // OAuth 2.0 access token with datamanager scope
39
- accessToken: 'ya29.c.xxx',
38
+ // Service account credentials from environment variables
39
+ credentials: {
40
+ client_email: process.env.GOOGLE_CLIENT_EMAIL!,
41
+ private_key: process.env.GOOGLE_PRIVATE_KEY!.replace(/\\n/g, '\n'),
42
+ },
40
43
 
41
44
  // Destination accounts
42
45
  destinations: [
@@ -62,7 +65,36 @@ await elb('order complete', {
62
65
  });
63
66
  ```
64
67
 
65
- ### Complete Configuration
68
+ ### GCP Cloud Functions (Application Default Credentials)
69
+
70
+ ```typescript
71
+ import { destinationDataManager } from '@walkeros/server-destination-datamanager';
72
+ import { startFlow } from '@walkeros/collector';
73
+
74
+ // No auth config needed - ADC works automatically on GCP!
75
+ const { collector, elb } = await startFlow({
76
+ destinations: {
77
+ datamanager: {
78
+ ...destinationDataManager,
79
+ config: {
80
+ settings: {
81
+ destinations: [
82
+ {
83
+ operatingAccount: {
84
+ accountId: '123-456-7890',
85
+ accountType: 'GOOGLE_ADS',
86
+ },
87
+ productDestinationId: 'AW-CONVERSION-123',
88
+ },
89
+ ],
90
+ },
91
+ },
92
+ },
93
+ },
94
+ });
95
+ ```
96
+
97
+ ### Local Development
66
98
 
67
99
  ```typescript
68
100
  import { destinationDataManager } from '@walkeros/server-destination-datamanager';
@@ -71,7 +103,8 @@ const config = {
71
103
  ...destinationDataManager,
72
104
  config: {
73
105
  settings: {
74
- accessToken: 'ya29.c.xxx',
106
+ // Service account JSON file
107
+ keyFilename: './service-account.json',
75
108
 
76
109
  // Multiple destinations (max 10)
77
110
  destinations: [
@@ -136,34 +169,141 @@ const config = {
136
169
 
137
170
  ## Authentication
138
171
 
139
- ### OAuth 2.0 Setup
172
+ The Data Manager destination uses Google Cloud service accounts with automatic
173
+ token refresh. The library handles token management automatically - you just
174
+ provide credentials and tokens are cached and refreshed as needed.
175
+
176
+ ### Authentication Methods
177
+
178
+ The destination supports three authentication methods (in priority order):
179
+
180
+ 1. **Inline Credentials** - Best for serverless (AWS Lambda, Docker, K8s)
181
+ 2. **Service Account File** - Best for local development
182
+ 3. **Application Default Credentials (ADC)** - Automatic on GCP
183
+
184
+ ### 1. Inline Credentials (Recommended for Serverless)
185
+
186
+ Best for AWS Lambda, Docker, Kubernetes, and other containerized environments
187
+ where you manage secrets via environment variables.
188
+
189
+ ```typescript
190
+ import { destinationDataManager } from '@walkeros/server-destination-datamanager';
191
+
192
+ const config = {
193
+ ...destinationDataManager,
194
+ config: {
195
+ settings: {
196
+ credentials: {
197
+ client_email: process.env.GOOGLE_CLIENT_EMAIL!,
198
+ private_key: process.env.GOOGLE_PRIVATE_KEY!.replace(/\\n/g, '\n'),
199
+ },
200
+ destinations: [
201
+ /* ... */
202
+ ],
203
+ },
204
+ },
205
+ };
206
+ ```
207
+
208
+ **Environment Variables:**
209
+
210
+ ```bash
211
+ GOOGLE_CLIENT_EMAIL=service-account@project.iam.gserviceaccount.com
212
+ GOOGLE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nMIIE...\n-----END PRIVATE KEY-----\n"
213
+ ```
214
+
215
+ **Note:** Use `.replace(/\\n/g, '\n')` to convert escaped newlines from
216
+ environment variables.
217
+
218
+ ### 2. Service Account File (Local Development)
219
+
220
+ Best for local development with filesystem access.
221
+
222
+ ```typescript
223
+ const config = {
224
+ ...destinationDataManager,
225
+ config: {
226
+ settings: {
227
+ keyFilename: './service-account.json',
228
+ destinations: [
229
+ /* ... */
230
+ ],
231
+ },
232
+ },
233
+ };
234
+ ```
235
+
236
+ ### 3. Application Default Credentials (GCP)
237
+
238
+ Automatic on Google Cloud Platform - no configuration needed!
140
239
 
141
- 1. **Create a Google Cloud Project**
142
- - Go to [Google Cloud Console](https://console.cloud.google.com/)
143
- - Create a new project or select existing
240
+ ```typescript
241
+ const config = {
242
+ ...destinationDataManager,
243
+ config: {
244
+ settings: {
245
+ // No auth config needed - ADC used automatically!
246
+ destinations: [
247
+ /* ... */
248
+ ],
249
+ },
250
+ },
251
+ };
252
+ ```
253
+
254
+ **Works automatically on:**
144
255
 
145
- 2. **Enable Data Manager API**
146
- - Navigate to APIs & Services > Library
147
- - Search for "Google Data Manager API"
148
- - Click Enable
256
+ - Google Cloud Functions
257
+ - Google Cloud Run
258
+ - Google Compute Engine
259
+ - Google Kubernetes Engine
260
+ - Any GCP environment with default service account
149
261
 
150
- 3. **Create Service Account**
151
- - Go to APIs & Services > Credentials
152
- - Click "Create Credentials" > "Service Account"
153
- - Grant necessary permissions
154
- - Download JSON key file
262
+ **Also works with:**
155
263
 
156
- 4. **Get Access Token**
157
- ```bash
158
- gcloud auth application-default print-access-token --scopes=https://www.googleapis.com/auth/datamanager
159
- ```
264
+ - `GOOGLE_APPLICATION_CREDENTIALS` environment variable
265
+ - gcloud CLI: `gcloud auth application-default login`
160
266
 
161
- ### Required Scope
267
+ ### Creating a Service Account
268
+
269
+ 1. Go to [Google Cloud Console](https://console.cloud.google.com/)
270
+ 2. Navigate to **IAM & Admin > Service Accounts**
271
+ 3. Click **Create Service Account**
272
+ 4. Name: `walkeros-datamanager` (or your choice)
273
+ 5. Grant role: **Data Manager Admin** or custom role with datamanager
274
+ permissions
275
+ 6. Click **Keys > Add Key > Create New Key**
276
+ 7. Select **JSON** format and download
277
+
278
+ **For inline credentials:**
279
+
280
+ - Extract `client_email` and `private_key` from the JSON
281
+ - Store in environment variables or secrets manager
282
+
283
+ **For keyFilename:**
284
+
285
+ - Set `keyFilename: '/path/to/downloaded-key.json'`
286
+
287
+ **For ADC:**
288
+
289
+ - Set `GOOGLE_APPLICATION_CREDENTIALS=/path/to/downloaded-key.json`
290
+ - Or deploy to GCP where ADC works automatically
291
+
292
+ ### Required OAuth Scope
162
293
 
163
294
  ```
164
295
  https://www.googleapis.com/auth/datamanager
165
296
  ```
166
297
 
298
+ This scope is automatically applied - no configuration needed.
299
+
300
+ ### Token Management
301
+
302
+ - **Lifetime:** 1 hour (3600 seconds)
303
+ - **Refresh:** Automatic - library handles expiration
304
+ - **Caching:** Tokens are cached and reused until expiration
305
+ - **Performance:** ~10-50ms overhead per request for token validation
306
+
167
307
  ## Guided Mapping Helpers
168
308
 
169
309
  Define common fields once in Settings instead of repeating them in every event
@@ -172,7 +312,7 @@ mapping:
172
312
  ```typescript
173
313
  {
174
314
  settings: {
175
- accessToken: 'ya29.c.xxx',
315
+ credentials: { /* ... */ },
176
316
  destinations: [...],
177
317
 
178
318
  // Guided helpers (apply to all events)
@@ -616,23 +756,28 @@ be rejected.
616
756
 
617
757
  ### Settings
618
758
 
619
- | Property | Type | Required | Description |
620
- | -------------------------- | -------------- | -------- | ------------------------------------------- |
621
- | `accessToken` | string | | OAuth 2.0 access token |
622
- | `destinations` | Destination[] | | Array of destination accounts (max 10) |
623
- | `eventSource` | EventSource | | Default event source (WEB, APP, etc.) |
624
- | `batchSize` | number | | Max events per batch (max 2000) |
625
- | `batchInterval` | number | | Batch flush interval in ms |
626
- | `validateOnly` | boolean | | Validate without ingestion |
627
- | `url` | string | | Override API endpoint |
628
- | `consent` | Consent | | Request-level consent |
629
- | `testEventCode` | string | | Test event code for debugging |
630
- | `userData` | object | | Guided helper: User data mapping |
631
- | `userId` | string | | Guided helper: First-party user ID |
632
- | `clientId` | string | | Guided helper: GA4 client ID |
633
- | `sessionAttributes` | string | | Guided helper: Privacy-safe attribution |
634
- | `consentAdUserData` | string/boolean | | Consent mapping: Field name or static value |
635
- | `consentAdPersonalization` | string/boolean | | Consent mapping: Field name or static value |
759
+ | Property | Type | Required | Description |
760
+ | -------------------------- | -------------- | -------- | -------------------------------------------- |
761
+ | `credentials` | object | \* | Service account (client_email + private_key) |
762
+ | `keyFilename` | string | \* | Path to service account JSON file |
763
+ | `scopes` | string[] | | OAuth scopes (default: datamanager scope) |
764
+ | `destinations` | Destination[] || Array of destination accounts (max 10) |
765
+ | `eventSource` | EventSource | | Default event source (WEB, APP, etc.) |
766
+ | `batchSize` | number | | Max events per batch (max 2000) |
767
+ | `batchInterval` | number | | Batch flush interval in ms |
768
+ | `validateOnly` | boolean | | Validate without ingestion |
769
+ | `url` | string | | Override API endpoint |
770
+ | `consent` | Consent | | Request-level consent |
771
+ | `testEventCode` | string | | Test event code for debugging |
772
+ | `userData` | object | | Guided helper: User data mapping |
773
+ | `userId` | string | | Guided helper: First-party user ID |
774
+ | `clientId` | string | | Guided helper: GA4 client ID |
775
+ | `sessionAttributes` | string | | Guided helper: Privacy-safe attribution |
776
+ | `consentAdUserData` | string/boolean | | Consent mapping: Field name or static value |
777
+ | `consentAdPersonalization` | string/boolean | | Consent mapping: Field name or static value |
778
+
779
+ **\* One of `credentials`, `keyFilename`, or ADC (automatic) required for
780
+ authentication**
636
781
 
637
782
  ### Event Fields
638
783
 
package/dist/dev.d.mts ADDED
@@ -0,0 +1,235 @@
1
+ import { JSONSchema } from '@walkeros/core/dev';
2
+ import { Mapping as Mapping$1, Destination as Destination$1 } from '@walkeros/core';
3
+ import { DestinationServer } from '@walkeros/server-core';
4
+ import { OAuth2Client } from 'google-auth-library';
5
+
6
+ declare const schemas$1: Record<string, JSONSchema>;
7
+
8
+ declare namespace schemas {
9
+ export { schemas$1 as schemas };
10
+ }
11
+
12
+ type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'none';
13
+
14
+ interface Settings {
15
+ /**
16
+ * Service account credentials (client_email + private_key)
17
+ * Recommended for serverless environments (AWS Lambda, Docker, etc.)
18
+ */
19
+ credentials?: {
20
+ client_email: string;
21
+ private_key: string;
22
+ };
23
+ /**
24
+ * Path to service account JSON file
25
+ * For local development or environments with filesystem access
26
+ */
27
+ keyFilename?: string;
28
+ /**
29
+ * OAuth scopes for Data Manager API
30
+ * @default ['https://www.googleapis.com/auth/datamanager']
31
+ */
32
+ scopes?: string[];
33
+ /** Array of destination accounts and conversion actions/user lists */
34
+ destinations: Destination[];
35
+ /** Default event source if not specified per event */
36
+ eventSource?: EventSource;
37
+ /** Maximum number of events to batch before sending (max 2000) */
38
+ batchSize?: number;
39
+ /** Time in milliseconds to wait before auto-flushing batch */
40
+ batchInterval?: number;
41
+ /** If true, validate request without ingestion (testing mode) */
42
+ validateOnly?: boolean;
43
+ /** Override API endpoint (for testing) */
44
+ url?: string;
45
+ /** Request-level consent for all events */
46
+ consent?: Consent;
47
+ /** Test event code for debugging (optional) */
48
+ testEventCode?: string;
49
+ /** Log level for debugging (optional) */
50
+ logLevel?: LogLevel;
51
+ /** Guided helpers: User data mapping (applies to all events) */
52
+ userData?: Mapping$1.Map;
53
+ /** Guided helper: First-party user ID */
54
+ userId?: Mapping$1.Value;
55
+ /** Guided helper: GA4 client ID */
56
+ clientId?: Mapping$1.Value;
57
+ /** Guided helper: Privacy-safe attribution (Google's sessionAttributes) */
58
+ sessionAttributes?: Mapping$1.Value;
59
+ /** Consent mapping: Map consent field to adUserData (string = field name, boolean = static value) */
60
+ consentAdUserData?: string | boolean;
61
+ /** Consent mapping: Map consent field to adPersonalization (string = field name, boolean = static value) */
62
+ consentAdPersonalization?: string | boolean;
63
+ }
64
+ interface Mapping {
65
+ gclid?: Mapping$1.Value;
66
+ gbraid?: Mapping$1.Value;
67
+ wbraid?: Mapping$1.Value;
68
+ sessionAttributes?: Mapping$1.Value;
69
+ }
70
+ interface Env extends DestinationServer.Env {
71
+ fetch?: typeof fetch;
72
+ authClient?: OAuth2Client | null;
73
+ }
74
+ type InitSettings = Partial<Settings>;
75
+ type Types = Destination$1.Types<Settings, Mapping, Env, InitSettings>;
76
+ type Config = {
77
+ settings: Settings;
78
+ } & DestinationServer.Config<Types>;
79
+ type Rule = Mapping$1.Rule<Mapping>;
80
+ /**
81
+ * Destination account and product identifier
82
+ * https://developers.google.com/data-manager/api/reference/rest/v1/Destination
83
+ */
84
+ interface Destination {
85
+ /** Reference identifier for this destination */
86
+ reference?: string;
87
+ /** Login account (account initiating the request) */
88
+ loginAccount?: ProductAccount;
89
+ /** Linked account (child account linked to login account) */
90
+ linkedAccount?: ProductAccount;
91
+ /** Operating account (account where data is sent) */
92
+ operatingAccount?: ProductAccount;
93
+ /** Product-specific destination ID (conversion action or user list) */
94
+ productDestinationId?: string;
95
+ }
96
+ /**
97
+ * Product account information
98
+ */
99
+ interface ProductAccount {
100
+ /** Account ID (e.g., "123-456-7890" for Google Ads) */
101
+ accountId: string;
102
+ /** Type of account */
103
+ accountType: AccountType;
104
+ }
105
+ type AccountType = 'ACCOUNT_TYPE_UNSPECIFIED' | 'GOOGLE_ADS' | 'DISPLAY_VIDEO_ADVERTISER' | 'DISPLAY_VIDEO_PARTNER' | 'GOOGLE_ANALYTICS_PROPERTY' | 'DATA_PARTNER';
106
+ type EventSource = 'WEB' | 'APP' | 'IN_STORE' | 'PHONE' | 'OTHER';
107
+ /**
108
+ * Consent for Digital Markets Act (DMA) compliance
109
+ * https://developers.google.com/data-manager/api/devguides/concepts/dma
110
+ */
111
+ interface Consent {
112
+ /** Consent for data collection and use */
113
+ adUserData?: ConsentStatus;
114
+ /** Consent for ad personalization */
115
+ adPersonalization?: ConsentStatus;
116
+ }
117
+ type ConsentStatus = 'CONSENT_GRANTED' | 'CONSENT_DENIED';
118
+
119
+ /**
120
+ * AWS Lambda / Serverless Configuration
121
+ * Uses inline credentials from environment variables
122
+ * Best for: AWS Lambda, Docker, Kubernetes, any serverless environment
123
+ */
124
+ declare const awsLambda: Config;
125
+ /**
126
+ * GCP Cloud Functions / Cloud Run Configuration
127
+ * Uses Application Default Credentials (ADC) - no explicit auth config needed
128
+ * Best for: Google Cloud Functions, Cloud Run, GCE, GKE
129
+ */
130
+ declare const gcpCloudFunctions: Config;
131
+ /**
132
+ * Local Development Configuration
133
+ * Uses service account JSON file
134
+ * Best for: Local development, testing
135
+ */
136
+ declare const localDevelopment: Config;
137
+ /**
138
+ * Docker / Kubernetes Configuration
139
+ * Uses ADC via GOOGLE_APPLICATION_CREDENTIALS environment variable
140
+ * Best for: Docker containers, Kubernetes pods
141
+ *
142
+ * Setup:
143
+ * 1. Mount service account JSON as secret/configmap
144
+ * 2. Set GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json
145
+ * 3. ADC will automatically use it
146
+ */
147
+ declare const dockerKubernetes: Config;
148
+ /**
149
+ * Custom Scopes Configuration
150
+ * For specific use cases requiring different OAuth scopes
151
+ */
152
+ declare const customScopes: Config;
153
+
154
+ declare const auth_awsLambda: typeof awsLambda;
155
+ declare const auth_customScopes: typeof customScopes;
156
+ declare const auth_dockerKubernetes: typeof dockerKubernetes;
157
+ declare const auth_gcpCloudFunctions: typeof gcpCloudFunctions;
158
+ declare const auth_localDevelopment: typeof localDevelopment;
159
+ declare namespace auth {
160
+ export { auth_awsLambda as awsLambda, auth_customScopes as customScopes, auth_dockerKubernetes as dockerKubernetes, auth_gcpCloudFunctions as gcpCloudFunctions, auth_localDevelopment as localDevelopment };
161
+ }
162
+
163
+ /**
164
+ * Minimal configuration for Google Data Manager with inline credentials
165
+ */
166
+ declare const minimal: Config;
167
+ /**
168
+ * Complete configuration with all options
169
+ */
170
+ declare const complete: Config;
171
+ /**
172
+ * GA4-specific configuration using Application Default Credentials
173
+ */
174
+ declare const ga4: Config;
175
+ /**
176
+ * Debug configuration with logging enabled using keyFilename
177
+ */
178
+ declare const debug: Config;
179
+
180
+ declare const basic_complete: typeof complete;
181
+ declare const basic_debug: typeof debug;
182
+ declare const basic_ga4: typeof ga4;
183
+ declare const basic_minimal: typeof minimal;
184
+ declare namespace basic {
185
+ export { basic_complete as complete, basic_debug as debug, basic_ga4 as ga4, basic_minimal as minimal };
186
+ }
187
+
188
+ /**
189
+ * Purchase event mapping for Google Ads conversion
190
+ */
191
+ declare const Purchase: Rule;
192
+ /**
193
+ * Lead event mapping
194
+ */
195
+ declare const Lead: Rule;
196
+ /**
197
+ * Page view mapping for GA4
198
+ */
199
+ declare const PageView: Rule;
200
+ /**
201
+ * Complete mapping configuration
202
+ */
203
+ declare const mapping: {
204
+ order: {
205
+ complete: Rule;
206
+ };
207
+ lead: {
208
+ submit: Rule;
209
+ };
210
+ page: {
211
+ view: Rule;
212
+ };
213
+ };
214
+ /**
215
+ * User data mapping configuration
216
+ * Maps walkerOS user properties to Data Manager user identifiers
217
+ */
218
+ declare const userDataMapping: Config;
219
+
220
+ declare const mapping$1_Lead: typeof Lead;
221
+ declare const mapping$1_PageView: typeof PageView;
222
+ declare const mapping$1_Purchase: typeof Purchase;
223
+ declare const mapping$1_mapping: typeof mapping;
224
+ declare const mapping$1_userDataMapping: typeof userDataMapping;
225
+ declare namespace mapping$1 {
226
+ export { mapping$1_Lead as Lead, mapping$1_PageView as PageView, mapping$1_Purchase as Purchase, mapping$1_mapping as mapping, mapping$1_userDataMapping as userDataMapping };
227
+ }
228
+
229
+ declare const index_auth: typeof auth;
230
+ declare const index_basic: typeof basic;
231
+ declare namespace index {
232
+ export { index_auth as auth, index_basic as basic, mapping$1 as mapping };
233
+ }
234
+
235
+ export { index as examples, schemas };