@happyvertical/smrt-ads 0.30.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/AGENTS.md ADDED
@@ -0,0 +1,47 @@
1
+ # @happyvertical/smrt-ads
2
+
3
+ Ad delivery with priority waterfall, weighted A/B testing, and immutable event tracking.
4
+
5
+ ## Models
6
+
7
+ - **AdFormat**: IAB standard dimensions (width, height, type: banner/native/video)
8
+ - **AdDeliveryTier**: priority levels — Sponsorship (1, FIXED pricing) > Standard (2, CPM) > House (3, fallback)
9
+ - **AdGroup**: campaign with targeting, budget, zone restrictions
10
+ - **AdVariation** (STI): creative variant with weighted selection for A/B. `weight=2` is 2x more likely than `weight=1`. Denormalized `impressions`/`clicks` fields.
11
+ - **AdEvent** (STI, immutable): impression/click/conversion tracking. **Create-only** — no update/delete in API/MCP. `cli: false` (high-volume).
12
+
13
+ ## Cross-Package References (all plain string IDs)
14
+
15
+ `contractId` → smrt-commerce, `zoneId`/`siteId` → smrt-properties, `assetId` → smrt-assets, `verticalSlug` → smrt-tags
16
+
17
+ ## Tenancy
18
+
19
+ Optional tenancy via `@TenantScoped({ mode: 'optional' })` is applied to the
20
+ three transactional models — `AdGroup`, `AdVariation`, and `AdEvent` — which
21
+ participate in per-tenant ad serving and event tracking.
22
+
23
+ `AdFormat` and `AdDeliveryTier` are deliberately **NOT** tenant-scoped: they
24
+ are shared catalog/lookup tables.
25
+
26
+ - `AdFormat` rows describe IAB standard ad dimensions (e.g. `728x90`
27
+ Leaderboard, `300x250` Medium Rectangle). The dimensions are an industry
28
+ standard, not a tenant-specific configuration.
29
+ - `AdDeliveryTier` rows describe the priority waterfall for ad selection
30
+ (Sponsorship → Standard → House). Tiers are part of the package's
31
+ ad-serving contract; per-tenant tier definitions would fragment the
32
+ selection algorithm without a clear use case.
33
+
34
+ Either model can still be filtered by tenant manually if a deployment ever
35
+ needs tenant-specific overrides, but the default is global. This mirrors the
36
+ documented exception pattern in `packages/secrets/AGENTS.md` (`TenantKey`).
37
+ Each `@smrt(...)` block on these two classes carries an inline comment
38
+ pointing back here.
39
+
40
+ ## Gotchas
41
+
42
+ - **Priority waterfall**: lower priority number = higher priority in selection
43
+ - **Weight is relative integer**: not percentage — probability calculated from total weight across variations
44
+ - **Denormalized counts need async refresh**: impressions/clicks on AdVariation are eventually consistent
45
+ - **No frequency capping or budget enforcement built-in**: external responsibility
46
+ - **Targeting rules stored as JSON string**: no schema validation
47
+ - **Optional tenancy** on `AdGroup`, `AdVariation`, `AdEvent` only — see Tenancy section above for `AdFormat`/`AdDeliveryTier` rationale
package/CLAUDE.md ADDED
@@ -0,0 +1 @@
1
+ @AGENTS.md
package/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright <2025> <Happy Vertical Corporation>
2
+
3
+ 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:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ 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 ADDED
@@ -0,0 +1,112 @@
1
+ # @happyvertical/smrt-ads
2
+
3
+ Advertising delivery and tracking models for the SMRT framework. Supports priority-based ad group delivery with a tier waterfall, IAB ad formats, weighted A/B variation testing, and immutable event tracking.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @happyvertical/smrt-ads
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import {
15
+ AdDeliveryTier, AdDeliveryTierCollection,
16
+ AdGroup, AdGroupCollection,
17
+ AdVariation, AdVariationCollection,
18
+ AdEvent, AdEventCollection,
19
+ AdEventType, PricingModel, AdGroupStatus
20
+ } from '@happyvertical/smrt-ads';
21
+
22
+ // Define delivery tiers (lower priority number = served first)
23
+ const tiers = new AdDeliveryTierCollection(db);
24
+ const sponsorship = await tiers.create({
25
+ name: 'Sponsorship',
26
+ priority: 1,
27
+ pricingModel: PricingModel.FIXED,
28
+ });
29
+
30
+ const standard = await tiers.create({
31
+ name: 'Standard',
32
+ priority: 2,
33
+ pricingModel: PricingModel.CPM,
34
+ });
35
+
36
+ // Create an ad group with targeting and budget
37
+ const groups = new AdGroupCollection(db);
38
+ const group = await groups.create({
39
+ name: 'Holiday Campaign',
40
+ tierId: sponsorship.id,
41
+ contractId: 'contract-uuid',
42
+ status: AdGroupStatus.ACTIVE,
43
+ dailyBudget: 100.00,
44
+ totalBudget: 3000.00,
45
+ startDate: new Date('2024-06-01'),
46
+ endDate: new Date('2024-08-31'),
47
+ });
48
+ group.setTargeting({ device: 'desktop', geo: 'US' });
49
+ group.setZoneIds(['zone-1', 'zone-2']);
50
+ await group.save();
51
+
52
+ // Add variations with weights for A/B testing
53
+ // weight=2 is selected 2x more often than weight=1
54
+ const variations = new AdVariationCollection(db);
55
+ const varA = await variations.create({
56
+ groupId: group.id,
57
+ name: 'Version A - Blue CTA',
58
+ weight: 2,
59
+ status: 'active',
60
+ });
61
+
62
+ // Track an immutable event (create-only, no update/delete)
63
+ const events = new AdEventCollection(db);
64
+ await events.create({
65
+ variationId: varA.id,
66
+ zoneId: 'zone-uuid',
67
+ siteId: 'site-uuid',
68
+ eventType: AdEventType.IMPRESSION,
69
+ });
70
+ ```
71
+
72
+ ### Delivery Tier Waterfall
73
+
74
+ Ads are selected by tier priority (lower number = higher priority):
75
+
76
+ 1. **Sponsorship** (priority 1) -- guaranteed premium placements, FIXED pricing
77
+ 2. **Standard** (priority 2) -- regular programmatic ads, CPM pricing
78
+ 3. **House** (priority 3) -- self-promotional fallback ads
79
+
80
+ Within a tier, variations are selected by relative weight. A variation with `weight: 2` is twice as likely to be chosen as one with `weight: 1`.
81
+
82
+ ## API
83
+
84
+ ### Models
85
+
86
+ | Export | Description |
87
+ |--------|------------|
88
+ | `AdDeliveryTier` | Priority tier for waterfall delivery (name, priority, pricingModel) |
89
+ | `AdFormat` | IAB standard ad dimensions (width, height, formatType: banner/native/video) |
90
+ | `AdGroup` | Campaign with targeting rules, budget, zone restrictions, and date range |
91
+ | `AdVariation` | Creative variant within a group with weight for A/B distribution. Denormalized `impressions`/`clicks` counts |
92
+ | `AdEvent` | Immutable tracking event (impression/click/conversion). Create-only -- no update/delete. `cli: false` (high-volume) |
93
+
94
+ ### Collections
95
+
96
+ `AdDeliveryTierCollection`, `AdFormatCollection`, `AdGroupCollection`, `AdVariationCollection`, `AdEventCollection`
97
+
98
+ ### Enums
99
+
100
+ | Export | Values |
101
+ |--------|--------|
102
+ | `AdEventType` | `impression`, `click`, `conversion` |
103
+ | `AdFormatType` | `banner`, `native`, `video` |
104
+ | `AdGroupStatus` | `draft`, `active`, `paused`, `completed` |
105
+ | `AdVariationStatus` | `draft`, `active`, `paused` |
106
+ | `PricingModel` | `fixed`, `cpm`, `cpc`, `cpa` |
107
+
108
+ ## Dependencies
109
+
110
+ - `@happyvertical/smrt-core` -- ORM and code generation
111
+ - `@happyvertical/smrt-tenancy` -- multi-tenant scoping (optional on AdGroup, AdVariation, AdEvent)
112
+ - Peer: `@happyvertical/smrt-assets`, `@happyvertical/smrt-commerce`, `@happyvertical/smrt-properties`, `@happyvertical/smrt-tags`