@traffical/react 0.4.0 → 0.4.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.
Files changed (2) hide show
  1. package/README.md +105 -0
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -1037,6 +1037,111 @@ Yes! That's the point. Parameters are resolved from Traffical's config bundle, w
1037
1037
  ---
1038
1038
 
1039
1039
 
1040
+ ## Type-Safe Event Tracking
1041
+
1042
+ The `useTraffical` hook supports a `TEvents` generic that enforces event names and property shapes at compile time. Combined with `@traffical/cli generate-types`, you get full type safety on every `track()` call.
1043
+
1044
+ ### 1. Generate types from your config
1045
+
1046
+ ```bash
1047
+ bunx @traffical/cli generate-types
1048
+ # → creates .traffical/traffical.generated.ts
1049
+ ```
1050
+
1051
+ This generates interfaces for each event's properties and a `TrafficalEventProperties` map:
1052
+
1053
+ ```typescript
1054
+ // traffical.generated.ts (auto-generated, do not edit)
1055
+ export interface PurchaseProperties {
1056
+ order_total: number;
1057
+ payment_method: "visa" | "mastercard" | "paypal";
1058
+ item_count?: number;
1059
+ }
1060
+
1061
+ export interface TrafficalEventProperties {
1062
+ "purchase": PurchaseProperties;
1063
+ "add_to_cart": AddToCartProperties;
1064
+ // ...
1065
+ }
1066
+ ```
1067
+
1068
+ ### 2. Create a typed wrapper
1069
+
1070
+ ```typescript
1071
+ // lib/traffical.ts
1072
+ import type { TrafficalEventProperties } from './traffical.generated';
1073
+ import { useTraffical as useTrafficalBase, type UseTrafficalOptions, type ParameterValue } from '@traffical/react';
1074
+
1075
+ // Strict track — only allows events and properties defined in the schema
1076
+ export type TrafficalTrack = <E extends Extract<keyof TrafficalEventProperties, string>>(
1077
+ event: E,
1078
+ properties?: TrafficalEventProperties[E],
1079
+ options?: { decisionId?: string; unitKey?: string }
1080
+ ) => void;
1081
+
1082
+ export function useTraffical<T extends Record<string, ParameterValue>>(
1083
+ options: UseTrafficalOptions<T>
1084
+ ) {
1085
+ const result = useTrafficalBase<T>(options);
1086
+ return { ...result, track: result.track as unknown as TrafficalTrack };
1087
+ }
1088
+ ```
1089
+
1090
+ ### 3. Use it — TypeScript catches mistakes
1091
+
1092
+ ```tsx
1093
+ import { useTraffical } from '@/lib/traffical';
1094
+
1095
+ function CheckoutPage() {
1096
+ const { params, track } = useTraffical({
1097
+ defaults: { 'checkout.ctaText': 'Buy Now' },
1098
+ });
1099
+
1100
+ track('purchase', {
1101
+ order_total: 99.99,
1102
+ payment_method: 'visa',
1103
+ }); // ✅ compiles
1104
+
1105
+ track('purchase', {
1106
+ order_total: 99.99,
1107
+ payment_method: 'bitcoin', // ❌ Type error: not in "visa" | "mastercard" | "paypal"
1108
+ });
1109
+
1110
+ track('purchase', {
1111
+ order_total: 99.99,
1112
+ payment_method: 'visa',
1113
+ random_field: true, // ❌ Type error: not in PurchaseProperties
1114
+ });
1115
+
1116
+ track('nonexistent_event'); // ❌ Type error: not in TrafficalEventProperties
1117
+ }
1118
+ ```
1119
+
1120
+ ### Typing component props
1121
+
1122
+ Use `TrafficalTrack` when passing `track` as a prop to child components:
1123
+
1124
+ ```tsx
1125
+ import type { TrafficalTrack } from '@/lib/traffical';
1126
+
1127
+ interface ProductCardProps {
1128
+ product: Product;
1129
+ track?: TrafficalTrack;
1130
+ }
1131
+
1132
+ function ProductCard({ product, track }: ProductCardProps) {
1133
+ const handleAdd = () => {
1134
+ track?.('add_to_cart', {
1135
+ product_id: product.id,
1136
+ quantity: 1,
1137
+ });
1138
+ };
1139
+ // ...
1140
+ }
1141
+ ```
1142
+
1143
+ ---
1144
+
1040
1145
  ## Migration from Deprecated Hooks
1041
1146
 
1042
1147
  The `useTrafficalParams` and `useTrafficalDecision` hooks are deprecated but still available for backward compatibility.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@traffical/react",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "Traffical SDK for React - Provider and hooks for parameter resolution",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -22,8 +22,8 @@
22
22
  "typecheck": "tsc --noEmit"
23
23
  },
24
24
  "dependencies": {
25
- "@traffical/core": "0.6.0",
26
- "@traffical/js-client": "0.11.0"
25
+ "@traffical/core": "0.8.0",
26
+ "@traffical/js-client": "0.12.0"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@types/bun": "latest",