@amplifyup/sdk 0.1.5 → 0.1.7

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
@@ -1,127 +1,86 @@
1
1
  # @amplifyup/sdk
2
2
 
3
- AmplifyUp Tracking SDK - CDP-agnostic event tracking library.
3
+ Official JavaScript SDK for [AmplifyUp](https://amplifyup.ai) — event tracking, page layouts, personalization, and Composer preview on your site.
4
4
 
5
- ## Overview
6
-
7
- This SDK provides event tracking and personalization against AmplifyUp backends.
8
- Configure a `trackingId`, and optionally a `target` for which environment to use.
9
-
10
- ## Environment targets
11
-
12
- **Default: production (live).** Hosting your site on `localhost` does not switch environments.
13
-
14
- Opt into `local` only when developing inside the AmplifyUp monorepo:
15
-
16
- | Target | When | Layouts / config | Events + personalize |
17
- |--------|------|------------------|----------------------|
18
- | `production` (default) | Customer sites, staging, production | `https://amplifyup.ai` | `https://api.amplifyup.ai` |
19
- | `local` (explicit) | AmplifyUp monorepo dev | `NEXT_PUBLIC_ORCHESTRATOR_URL` | `https://api-dev.amplifyup.ai` |
20
-
21
- ```bash
22
- # AmplifyUp monorepo only (apps/demo, apps/orchestrator locally)
23
- NEXT_PUBLIC_ORCHESTRATOR_URL=http://localhost:5001
24
- NEXT_PUBLIC_AMPLIFYUP_TARGET=local
25
-
26
- # Customer sites: omit NEXT_PUBLIC_AMPLIFYUP_TARGET — always live
27
- ```
28
-
29
- Check active URLs in the browser: `AmplifyUp.getStatus()`
30
-
31
- ## Installation
5
+ ## Install
32
6
 
33
7
  ```bash
34
8
  npm install @amplifyup/sdk
35
9
  ```
36
10
 
37
- ## Usage
11
+ ## Quick start
38
12
 
39
- ### Basic Setup
13
+ ### React / Next.js
40
14
 
41
- ```typescript
42
- import { init } from '@amplifyup/sdk';
15
+ Wrap your app with the provider and pass your site **tracking ID** from the AmplifyUp dashboard:
16
+
17
+ ```tsx
18
+ import { AmplifyUpProvider } from '@amplifyup/sdk/react';
43
19
 
44
- await init({
45
- trackingId: 'your-tracking-id',
46
- debug: true,
47
- });
20
+ export default function RootLayout({ children }) {
21
+ return (
22
+ <AmplifyUpProvider config={{ trackingId: 'your-tracking-id' }}>
23
+ {children}
24
+ </AmplifyUpProvider>
25
+ );
26
+ }
48
27
  ```
49
28
 
50
- ### Track Events
29
+ ### Track events
51
30
 
52
31
  ```typescript
53
- import {
54
- page,
55
- track,
56
- trackClick,
57
- trackFormSubmit,
58
- trackContentView,
59
- identify
60
- } from '@amplifyup/sdk';
61
-
62
- // Track page view
63
- page('/home');
32
+ import { init, page, track, identify } from '@amplifyup/sdk';
64
33
 
65
- // Track custom event
66
- track('Product Viewed', {
67
- productId: '123',
68
- productName: 'Widget',
69
- price: 29.99
70
- });
71
-
72
- // Track button click
73
- trackClick('add-to-cart', 'Add to Cart', {
74
- productId: '123'
75
- });
76
-
77
- // Track form submission
78
- trackFormSubmit('newsletter-signup', {
79
- email: 'user@example.com'
80
- });
81
-
82
- // Track content view
83
- trackContentView('product-123', 'product', {
84
- productName: 'Widget',
85
- category: 'Electronics'
86
- });
87
-
88
- // Identify user
89
- identify('user-123', {
90
- email: 'user@example.com',
91
- name: 'John Doe'
92
- });
93
- ```
34
+ await init({ trackingId: 'your-tracking-id' });
94
35
 
95
- ### Get Instance ID
36
+ page('/home');
96
37
 
97
- ```typescript
98
- import { getInstanceId } from '@amplifyup/sdk';
38
+ track('Product Viewed', { productId: '123' });
99
39
 
100
- const instanceId = getInstanceId();
40
+ identify('user-123', { email: 'user@example.com' });
101
41
  ```
102
42
 
103
- ## Architecture
104
-
105
- The SDK sends events directly to AWS API Gateway:
106
- - Events are sent to `/v1/track` or `/v1/page` endpoints
107
- - All URLs are hardcoded in the SDK
108
- - No write keys or configuration needed
109
- - Personalization requests go to AWS Decision API Gateway
43
+ ## Composer and hybrid pages
44
+
45
+ If a page has its own native content **and** can be edited in Composer, use `AmplifyPageContent`:
46
+
47
+ - **Visitors** see your AmplifyUp layout, or your native content when no layout is configured yet.
48
+ - **Composer preview** shows only the layout canvas — native fallback content is hidden.
49
+
50
+ ```tsx
51
+ import { isPreviewRequest } from '@amplifyup/sdk';
52
+ import { AmplifyPageContent } from '@amplifyup/sdk/react';
53
+
54
+ export default async function Page({ searchParams }) {
55
+ const params = await searchParams;
56
+
57
+ if (isPreviewRequest(params)) {
58
+ return (
59
+ <Layout>
60
+ <AmplifyPageContent renderComponent={renderAmplifyComponent} />
61
+ </Layout>
62
+ );
63
+ }
64
+
65
+ return (
66
+ <Layout>
67
+ <AmplifyPageContent
68
+ fallback={<YourNativePage />}
69
+ renderComponent={renderAmplifyComponent}
70
+ />
71
+ </Layout>
72
+ );
73
+ }
74
+ ```
110
75
 
111
- ## Development
76
+ ## Debugging
112
77
 
113
- ```bash
114
- # Build
115
- npm run build
78
+ In the browser console (on your site):
116
79
 
117
- # Watch mode
118
- npm run dev
119
-
120
- # Lint
121
- npm run lint
80
+ ```js
81
+ AmplifyUp.getStatus()
122
82
  ```
123
83
 
124
84
  ## License
125
85
 
126
86
  MIT
127
-
@@ -407,6 +407,7 @@ var AmplifyUpSDK = class {
407
407
  */
408
408
  async init(config) {
409
409
  if (this.initialized && this.adapter?.isInitialized()) {
410
+ this.ensureWindowBridge();
410
411
  return;
411
412
  }
412
413
  if (this.initPromise) {
@@ -419,6 +420,32 @@ var AmplifyUpSDK = class {
419
420
  this.initPromise = null;
420
421
  }
421
422
  }
423
+ /** Attach console helpers to window (idempotent; survives Fast Refresh). */
424
+ ensureWindowBridge() {
425
+ if (typeof window === "undefined") return;
426
+ window.AmplifyUp = {
427
+ getInstanceId: () => this.getInstanceId(),
428
+ getUserId: () => this.getUserId(),
429
+ getTrackingId: () => this.getTrackingId(),
430
+ getTarget: () => this.getTarget(),
431
+ getDiagnostics: () => this.getDiagnostics(),
432
+ getStatus: () => {
433
+ console.log(this.getStatus());
434
+ return this.getDiagnostics();
435
+ },
436
+ testTracking: () => this.testTracking(),
437
+ verifyAWSConnection: () => {
438
+ const result = this.verifyAWSConnection();
439
+ console.log("AWS Connection Verification:", result.message);
440
+ console.log("Full details:", result);
441
+ return result;
442
+ },
443
+ isInitialized: () => this.isInitialized(),
444
+ page: (url, properties) => this.page(url, properties),
445
+ track: (eventName, properties) => this.track(eventName, properties),
446
+ identify: (userId, traits) => this.identify(userId, traits)
447
+ };
448
+ }
422
449
  async performInit(config) {
423
450
  if (!this.adapter) {
424
451
  this.adapter = new AWSAdapter();
@@ -457,34 +484,10 @@ var AmplifyUpSDK = class {
457
484
  };
458
485
  await this.adapter.init(adapterConfig);
459
486
  this.initialized = this.adapter.isInitialized();
460
- if (typeof window !== "undefined") {
461
- window.AmplifyUp = {
462
- getInstanceId: () => this.getInstanceId(),
463
- getUserId: () => this.getUserId(),
464
- getTrackingId: () => this.getTrackingId(),
465
- getTarget: () => this.getTarget(),
466
- getDiagnostics: () => this.getDiagnostics(),
467
- getStatus: () => {
468
- console.log(this.getStatus());
469
- return this.getDiagnostics();
470
- },
471
- testTracking: () => this.testTracking(),
472
- verifyAWSConnection: () => {
473
- const result = this.verifyAWSConnection();
474
- console.log("AWS Connection Verification:", result.message);
475
- console.log("Full details:", result);
476
- return result;
477
- },
478
- isInitialized: () => this.isInitialized(),
479
- // Re-export main tracking methods for convenience
480
- page: (url, properties) => this.page(url, properties),
481
- track: (eventName, properties) => this.track(eventName, properties),
482
- identify: (userId, traits) => this.identify(userId, traits)
483
- };
484
- if (this.config?.debug) {
485
- console.log("AmplifyUp SDK: Diagnostic methods available on window.AmplifyUp");
486
- console.log("Try: AmplifyUp.getStatus() or AmplifyUp.testTracking()");
487
- }
487
+ this.ensureWindowBridge();
488
+ if (this.config?.debug) {
489
+ console.log("AmplifyUp SDK: Diagnostic methods available on window.AmplifyUp");
490
+ console.log("Try: AmplifyUp.getStatus() or AmplifyUp.testTracking()");
488
491
  }
489
492
  this.clearTrackingIntervals();
490
493
  if (this.isPreviewMode()) {
@@ -1495,10 +1498,17 @@ Available Console Commands:
1495
1498
  }
1496
1499
  };
1497
1500
  var sdkInstance = null;
1501
+ if (typeof window !== "undefined" && window.__AMPLIFYUP_SDK__) {
1502
+ sdkInstance = window.__AMPLIFYUP_SDK__;
1503
+ }
1498
1504
  function getSDK(adapter) {
1499
1505
  if (!sdkInstance) {
1500
1506
  sdkInstance = new AmplifyUpSDK(adapter);
1507
+ if (typeof window !== "undefined") {
1508
+ window.__AMPLIFYUP_SDK__ = sdkInstance;
1509
+ }
1501
1510
  }
1511
+ sdkInstance.ensureWindowBridge();
1502
1512
  return sdkInstance;
1503
1513
  }
1504
1514
  var trackContentView = (contentId, contentType, properties) => getSDK().trackContentView(contentId, contentType, properties);