@hermeticacomms/sdk-capacitor 0.0.0-stage → 0.4.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/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Hermetica
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,32 @@
1
- # Temporary Holding Version
1
+ # @hermeticacomms/sdk-capacitor
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Hermeticor for Capacitor and Ionic apps. The browser SDK runs unchanged in the WebView (consent gate, batching, retries with one
4
+ `Idempotency-Key` per batch); this package adds native push tokens from `@capacitor/push-notifications`, push open tracking, and
5
+ in-app messages targeted at the app's platform.
6
+
7
+ ```ts
8
+ import { Capacitor } from '@capacitor/core';
9
+ import { PushNotifications } from '@capacitor/push-notifications';
10
+ import { createCapacitorClient } from '@hermeticacomms/sdk-capacitor';
11
+
12
+ const hm = createCapacitorClient({
13
+ siteKey: 'hm_pk_...', endpoint: 'https://api.example.com',
14
+ platform: Capacitor.getPlatform() === 'ios' ? 'ios' : 'android',
15
+ push: PushNotifications, pushEnvironment: import.meta.env.DEV ? 'debug' : 'release',
16
+ onDeepLink: (link) => router.push(link),
17
+ });
18
+ hm.consent.update({ analytics: true, marketing: true });
19
+ hm.identify('user-42', { email: 'ada@example.com' });
20
+ hm.screen('Cart');
21
+ hm.track('add_to_cart', { sku: 'A1' }, { value: 1999, currency: 'USD' });
22
+ await hm.enablePush(); // asks for permission if needed, then registers the APNs (iOS) or FCM (Android) token
23
+ ```
24
+
25
+ - The token follows the profile: `identify`, `setProfile` and `reset` re-register it. `hm.unregisterPushToken()` on logout.
26
+ - Coming back from system settings re-sends the token when the notification permission changed there.
27
+ - Tapped notifications track `$opened_push` and go to `onDeepLink`; received ones track `$received_push`.
28
+ - In-app messages show on start, on `screen` and on `track` (`inApp: false` turns them off). Link buttons go to `onDeepLink`.
29
+ - Consent and suppression are enforced by the server too; opening a notification is an event and needs analytics consent.
30
+
31
+ On iOS, `@capacitor/push-notifications` hands over the APNs token; enable the Push Notifications capability. On Android it hands
32
+ over the FCM token; add `google-services.json`.
@@ -0,0 +1,38 @@
1
+ import { type AppPushToken, type HybridClient, type HybridOptions } from '@hermeticacomms/sdk-browser';
2
+ export type { AppDevice, HybridClient, ProfileIds } from '@hermeticacomms/sdk-browser';
3
+ type Permission = {
4
+ receive: string;
5
+ };
6
+ type Handle = Promise<unknown> | unknown;
7
+ /** The slice of `@capacitor/push-notifications`' `PushNotifications` used here (pass the plugin object itself). */
8
+ export interface CapacitorPush {
9
+ checkPermissions(): Promise<Permission>;
10
+ requestPermissions(): Promise<Permission>;
11
+ register(): Promise<void>;
12
+ addListener(event: 'registration', cb: (token: {
13
+ value: string;
14
+ }) => void): Handle;
15
+ addListener(event: 'registrationError', cb: (error: unknown) => void): Handle;
16
+ addListener(event: 'pushNotificationReceived', cb: (notification: unknown) => void): Handle;
17
+ addListener(event: 'pushNotificationActionPerformed', cb: (action: {
18
+ notification: unknown;
19
+ }) => void): Handle;
20
+ }
21
+ export interface CapacitorOptions extends HybridOptions {
22
+ /** `PushNotifications` from @capacitor/push-notifications. Without it, push is off (call `setPushToken` yourself). */
23
+ push?: CapacitorPush;
24
+ /** Show the notification prompt on start when the user has not decided (default false: call `enablePush()` when it fits). */
25
+ requestPushPermission?: boolean;
26
+ /** APNs environment of this build; debug for development-signed iOS builds. */
27
+ pushEnvironment?: 'debug' | 'release';
28
+ }
29
+ export type CapacitorClient = HybridClient & {
30
+ /** Asks for notification permission if needed, registers with APNs/FCM, and sends the token. Resolves the permission. */
31
+ enablePush(): Promise<AppPushToken['enablementStatus']>;
32
+ };
33
+ /**
34
+ * Hermeticor for Capacitor (and Ionic): the browser SDK runs as is in the WebView (consent gate, batching, retries with one
35
+ * Idempotency-Key per batch); this adds the native push token from @capacitor/push-notifications, push open tracking, and in-app
36
+ * messages for ios / android. Tapped notifications go to `onDeepLink`.
37
+ */
38
+ export declare function createCapacitorClient(o: CapacitorOptions): CapacitorClient;
package/dist/index.js ADDED
@@ -0,0 +1,55 @@
1
+ /// <reference lib="dom" />
2
+ import { createHybridClient } from '@hermeticacomms/sdk-browser';
3
+ const status = (p) => p.receive === 'granted' ? 'AUTHORIZED' : p.receive === 'denied' ? 'DENIED' : 'NOT_DETERMINED';
4
+ /**
5
+ * Hermeticor for Capacitor (and Ionic): the browser SDK runs as is in the WebView (consent gate, batching, retries with one
6
+ * Idempotency-Key per batch); this adds the native push token from @capacitor/push-notifications, push open tracking, and in-app
7
+ * messages for ios / android. Tapped notifications go to `onDeepLink`.
8
+ */
9
+ export function createCapacitorClient(o) {
10
+ const { push, requestPushPermission = false, pushEnvironment = 'release', ...rest } = o;
11
+ const hm = createHybridClient(rest);
12
+ const vendor = o.platform === 'ios' ? 'apns' : 'fcm';
13
+ let last = null;
14
+ let enablement = 'NOT_DETERMINED';
15
+ if (push) {
16
+ void push.addListener('registration', (t) => {
17
+ last = { token: t.value, vendor, environment: pushEnvironment, enablementStatus: enablement };
18
+ void hm.setPushToken(last);
19
+ });
20
+ void push.addListener('pushNotificationReceived', (n) => hm.handlePushReceived(n));
21
+ void push.addListener('pushNotificationActionPerformed', (a) => hm.handlePushOpened(a.notification));
22
+ // Back from system settings: re-send the token when the permission changed there.
23
+ document.addEventListener('visibilitychange', () => {
24
+ if (document.visibilityState !== 'visible' || !last)
25
+ return;
26
+ void push.checkPermissions().then((p) => {
27
+ const s = status(p);
28
+ if (!last || s === last.enablementStatus)
29
+ return;
30
+ enablement = s;
31
+ last = { ...last, enablementStatus: s };
32
+ void hm.setPushToken(last);
33
+ });
34
+ });
35
+ }
36
+ async function enablePush() {
37
+ if (!push)
38
+ return 'UNAUTHORIZED';
39
+ let p = await push.checkPermissions();
40
+ if (p.receive !== 'granted' && p.receive !== 'denied')
41
+ p = await push.requestPermissions();
42
+ enablement = status(p);
43
+ // Register even when denied: the server then knows this device exists but cannot be pushed to.
44
+ await push.register();
45
+ return enablement;
46
+ }
47
+ if (push && requestPushPermission)
48
+ void enablePush();
49
+ else if (push)
50
+ void push.checkPermissions().then((p) => { if (p.receive === 'granted') {
51
+ enablement = 'AUTHORIZED';
52
+ void push.register();
53
+ } });
54
+ return { ...hm, enablePush };
55
+ }
package/package.json CHANGED
@@ -1,6 +1,47 @@
1
1
  {
2
2
  "name": "@hermeticacomms/sdk-capacitor",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.4.0",
4
+ "description": "Hermetica for Capacitor and Ionic: events, consent, native push tokens, in-app messages",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/hermeticacomms/hermetica-capacitor.git"
10
+ },
11
+ "homepage": "https://github.com/hermeticacomms/hermetica-capacitor#readme",
12
+ "publishConfig": {
13
+ "access": "public",
14
+ "provenance": true
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "default": "./dist/index.js"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "scripts": {
26
+ "prepare": "npm run build",
27
+ "build": "tsc -p tsconfig.json",
28
+ "test": "vitest run",
29
+ "typecheck": "tsc -p tsconfig.json --noEmit"
30
+ },
31
+ "dependencies": {
32
+ "@hermeticacomms/sdk-browser": "^0.4.0"
33
+ },
34
+ "devDependencies": {
35
+ "happy-dom": "^21.0.4",
36
+ "typescript": "^7.0.2",
37
+ "vitest": "^5.0.5"
38
+ },
39
+ "peerDependencies": {
40
+ "@capacitor/push-notifications": ">=5"
41
+ },
42
+ "peerDependenciesMeta": {
43
+ "@capacitor/push-notifications": {
44
+ "optional": true
45
+ }
46
+ }
47
+ }