@stacksjs/ts-cloud-core 0.1.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/LICENSE.md +21 -0
- package/README.md +321 -0
- package/package.json +31 -0
- package/src/advanced-features.test.ts +465 -0
- package/src/aws/cloudformation.ts +421 -0
- package/src/aws/cloudfront.ts +158 -0
- package/src/aws/credentials.test.ts +132 -0
- package/src/aws/credentials.ts +545 -0
- package/src/aws/index.ts +87 -0
- package/src/aws/s3.test.ts +188 -0
- package/src/aws/s3.ts +1088 -0
- package/src/aws/signature.test.ts +670 -0
- package/src/aws/signature.ts +1155 -0
- package/src/backup/disaster-recovery.test.ts +726 -0
- package/src/backup/disaster-recovery.ts +500 -0
- package/src/backup/index.ts +34 -0
- package/src/backup/manager.test.ts +498 -0
- package/src/backup/manager.ts +432 -0
- package/src/cicd/circleci.ts +430 -0
- package/src/cicd/github-actions.ts +424 -0
- package/src/cicd/gitlab-ci.ts +255 -0
- package/src/cicd/index.ts +8 -0
- package/src/cli/history.ts +396 -0
- package/src/cli/index.ts +10 -0
- package/src/cli/progress.ts +458 -0
- package/src/cli/repl.ts +454 -0
- package/src/cli/suggestions.ts +327 -0
- package/src/cli/table.test.ts +319 -0
- package/src/cli/table.ts +332 -0
- package/src/cloudformation/builder.test.ts +327 -0
- package/src/cloudformation/builder.ts +378 -0
- package/src/cloudformation/builders/api-gateway.ts +449 -0
- package/src/cloudformation/builders/cache.ts +334 -0
- package/src/cloudformation/builders/cdn.ts +278 -0
- package/src/cloudformation/builders/compute.ts +485 -0
- package/src/cloudformation/builders/database.ts +392 -0
- package/src/cloudformation/builders/functions.ts +343 -0
- package/src/cloudformation/builders/messaging.ts +140 -0
- package/src/cloudformation/builders/monitoring.ts +300 -0
- package/src/cloudformation/builders/network.ts +264 -0
- package/src/cloudformation/builders/queue.ts +147 -0
- package/src/cloudformation/builders/security.ts +399 -0
- package/src/cloudformation/builders/storage.ts +285 -0
- package/src/cloudformation/index.ts +30 -0
- package/src/cloudformation/types.ts +173 -0
- package/src/compliance/aws-config.ts +543 -0
- package/src/compliance/cloudtrail.ts +376 -0
- package/src/compliance/compliance.test.ts +423 -0
- package/src/compliance/guardduty.ts +446 -0
- package/src/compliance/index.ts +66 -0
- package/src/compliance/security-hub.ts +456 -0
- package/src/containers/build-optimization.ts +416 -0
- package/src/containers/containers.test.ts +508 -0
- package/src/containers/image-scanning.ts +360 -0
- package/src/containers/index.ts +9 -0
- package/src/containers/registry.ts +293 -0
- package/src/containers/service-mesh.ts +520 -0
- package/src/database/database.test.ts +762 -0
- package/src/database/index.ts +9 -0
- package/src/database/migrations.ts +444 -0
- package/src/database/performance.ts +528 -0
- package/src/database/replicas.ts +534 -0
- package/src/database/users.ts +494 -0
- package/src/dependency-graph.ts +143 -0
- package/src/deployment/ab-testing.ts +582 -0
- package/src/deployment/blue-green.ts +452 -0
- package/src/deployment/canary.ts +500 -0
- package/src/deployment/deployment.test.ts +526 -0
- package/src/deployment/index.ts +61 -0
- package/src/deployment/progressive.ts +62 -0
- package/src/dns/dns.test.ts +641 -0
- package/src/dns/dnssec.ts +315 -0
- package/src/dns/index.ts +8 -0
- package/src/dns/resolver.ts +496 -0
- package/src/dns/routing.ts +593 -0
- package/src/email/advanced/analytics.ts +445 -0
- package/src/email/advanced/index.ts +11 -0
- package/src/email/advanced/rules.ts +465 -0
- package/src/email/advanced/scheduling.ts +352 -0
- package/src/email/advanced/search.ts +412 -0
- package/src/email/advanced/shared-mailboxes.ts +404 -0
- package/src/email/advanced/templates.ts +455 -0
- package/src/email/advanced/threading.ts +281 -0
- package/src/email/analytics.ts +467 -0
- package/src/email/bounce-handling.ts +425 -0
- package/src/email/email.test.ts +431 -0
- package/src/email/handlers/__tests__/inbound.test.ts +38 -0
- package/src/email/handlers/__tests__/outbound.test.ts +37 -0
- package/src/email/handlers/converter.ts +227 -0
- package/src/email/handlers/feedback.ts +228 -0
- package/src/email/handlers/inbound.ts +169 -0
- package/src/email/handlers/outbound.ts +178 -0
- package/src/email/index.ts +15 -0
- package/src/email/reputation.ts +303 -0
- package/src/email/templates.ts +352 -0
- package/src/errors/index.test.ts +434 -0
- package/src/errors/index.ts +416 -0
- package/src/health-checks/index.ts +40 -0
- package/src/index.ts +360 -0
- package/src/intrinsic-functions.ts +118 -0
- package/src/lambda/concurrency.ts +330 -0
- package/src/lambda/destinations.ts +345 -0
- package/src/lambda/dlq.ts +425 -0
- package/src/lambda/index.ts +11 -0
- package/src/lambda/lambda.test.ts +840 -0
- package/src/lambda/layers.ts +263 -0
- package/src/lambda/versions.ts +376 -0
- package/src/lambda/vpc.ts +399 -0
- package/src/local/config.ts +114 -0
- package/src/local/index.ts +6 -0
- package/src/local/mock-aws.ts +351 -0
- package/src/modules/ai.ts +340 -0
- package/src/modules/api.ts +478 -0
- package/src/modules/auth.ts +805 -0
- package/src/modules/cache.ts +417 -0
- package/src/modules/cdn.ts +1062 -0
- package/src/modules/communication.ts +1094 -0
- package/src/modules/compute.ts +3348 -0
- package/src/modules/database.ts +554 -0
- package/src/modules/deployment.ts +1079 -0
- package/src/modules/dns.ts +337 -0
- package/src/modules/email.ts +1538 -0
- package/src/modules/filesystem.ts +515 -0
- package/src/modules/index.ts +32 -0
- package/src/modules/messaging.ts +486 -0
- package/src/modules/monitoring.ts +2086 -0
- package/src/modules/network.ts +664 -0
- package/src/modules/parameter-store.ts +325 -0
- package/src/modules/permissions.ts +1081 -0
- package/src/modules/phone.ts +494 -0
- package/src/modules/queue.ts +1260 -0
- package/src/modules/redirects.ts +464 -0
- package/src/modules/registry.ts +699 -0
- package/src/modules/search.ts +401 -0
- package/src/modules/secrets.ts +416 -0
- package/src/modules/security.ts +731 -0
- package/src/modules/sms.ts +389 -0
- package/src/modules/storage.ts +1120 -0
- package/src/modules/workflow.ts +680 -0
- package/src/multi-account/config.ts +521 -0
- package/src/multi-account/index.ts +7 -0
- package/src/multi-account/manager.ts +427 -0
- package/src/multi-region/cross-region.ts +410 -0
- package/src/multi-region/index.ts +8 -0
- package/src/multi-region/manager.ts +483 -0
- package/src/multi-region/regions.ts +435 -0
- package/src/network-security/index.ts +48 -0
- package/src/observability/index.ts +9 -0
- package/src/observability/logs.ts +522 -0
- package/src/observability/metrics.ts +460 -0
- package/src/observability/observability.test.ts +782 -0
- package/src/observability/synthetics.ts +568 -0
- package/src/observability/xray.ts +358 -0
- package/src/phone/advanced/analytics.ts +349 -0
- package/src/phone/advanced/callbacks.ts +428 -0
- package/src/phone/advanced/index.ts +8 -0
- package/src/phone/advanced/ivr-builder.ts +504 -0
- package/src/phone/advanced/recording.ts +310 -0
- package/src/phone/handlers/__tests__/incoming-call.test.ts +40 -0
- package/src/phone/handlers/incoming-call.ts +117 -0
- package/src/phone/handlers/missed-call.ts +116 -0
- package/src/phone/handlers/voicemail.ts +179 -0
- package/src/phone/index.ts +9 -0
- package/src/presets/api-backend.ts +134 -0
- package/src/presets/data-pipeline.ts +204 -0
- package/src/presets/extend.test.ts +295 -0
- package/src/presets/extend.ts +297 -0
- package/src/presets/fullstack-app.ts +144 -0
- package/src/presets/index.ts +27 -0
- package/src/presets/jamstack.ts +135 -0
- package/src/presets/microservices.ts +167 -0
- package/src/presets/ml-api.ts +208 -0
- package/src/presets/nodejs-server.ts +104 -0
- package/src/presets/nodejs-serverless.ts +114 -0
- package/src/presets/realtime-app.ts +184 -0
- package/src/presets/static-site.ts +64 -0
- package/src/presets/traditional-web-app.ts +339 -0
- package/src/presets/wordpress.ts +138 -0
- package/src/preview/github.test.ts +249 -0
- package/src/preview/github.ts +297 -0
- package/src/preview/index.ts +37 -0
- package/src/preview/manager.test.ts +440 -0
- package/src/preview/manager.ts +326 -0
- package/src/preview/notifications.test.ts +582 -0
- package/src/preview/notifications.ts +341 -0
- package/src/queue/batch-processing.ts +402 -0
- package/src/queue/dlq-monitoring.ts +402 -0
- package/src/queue/fifo.ts +342 -0
- package/src/queue/index.ts +9 -0
- package/src/queue/management.ts +428 -0
- package/src/queue/queue.test.ts +429 -0
- package/src/resource-mgmt/index.ts +39 -0
- package/src/resource-naming.ts +62 -0
- package/src/s3/index.ts +523 -0
- package/src/schema/cloud-config.schema.json +554 -0
- package/src/schema/index.ts +68 -0
- package/src/security/certificate-manager.ts +492 -0
- package/src/security/index.ts +9 -0
- package/src/security/scanning.ts +545 -0
- package/src/security/secrets-manager.ts +476 -0
- package/src/security/secrets-rotation.ts +456 -0
- package/src/security/security.test.ts +738 -0
- package/src/sms/advanced/ab-testing.ts +389 -0
- package/src/sms/advanced/analytics.ts +336 -0
- package/src/sms/advanced/campaigns.ts +523 -0
- package/src/sms/advanced/chatbot.ts +224 -0
- package/src/sms/advanced/index.ts +10 -0
- package/src/sms/advanced/link-tracking.ts +248 -0
- package/src/sms/advanced/mms.ts +308 -0
- package/src/sms/handlers/__tests__/send.test.ts +40 -0
- package/src/sms/handlers/delivery-status.ts +133 -0
- package/src/sms/handlers/receive.ts +162 -0
- package/src/sms/handlers/send.ts +174 -0
- package/src/sms/index.ts +9 -0
- package/src/stack-diff.ts +389 -0
- package/src/static-site/index.ts +85 -0
- package/src/template-builder.ts +110 -0
- package/src/template-validator.ts +574 -0
- package/src/utils/cache.ts +291 -0
- package/src/utils/diff.ts +269 -0
- package/src/utils/hash.ts +227 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/parallel.ts +294 -0
- package/src/validators/credentials.test.ts +274 -0
- package/src/validators/credentials.ts +233 -0
- package/src/validators/quotas.test.ts +434 -0
- package/src/validators/quotas.ts +217 -0
- package/test/ai.test.ts +327 -0
- package/test/api.test.ts +511 -0
- package/test/auth.test.ts +632 -0
- package/test/cache.test.ts +406 -0
- package/test/cdn.test.ts +247 -0
- package/test/compute.test.ts +861 -0
- package/test/database.test.ts +523 -0
- package/test/deployment.test.ts +499 -0
- package/test/dns.test.ts +270 -0
- package/test/email.test.ts +439 -0
- package/test/filesystem.test.ts +382 -0
- package/test/integration.test.ts +350 -0
- package/test/messaging.test.ts +514 -0
- package/test/monitoring.test.ts +634 -0
- package/test/network.test.ts +425 -0
- package/test/permissions.test.ts +488 -0
- package/test/queue.test.ts +484 -0
- package/test/registry.test.ts +306 -0
- package/test/security.test.ts +462 -0
- package/test/storage.test.ts +463 -0
- package/test/template-validator.test.ts +559 -0
- package/test/workflow.test.ts +592 -0
- package/tsconfig.json +16 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Email Analytics
|
|
3
|
+
*
|
|
4
|
+
* Provides open tracking, click tracking, and email metrics
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export interface EmailAnalytics {
|
|
8
|
+
messageId: string
|
|
9
|
+
sent: boolean
|
|
10
|
+
sentAt?: string
|
|
11
|
+
delivered: boolean
|
|
12
|
+
deliveredAt?: string
|
|
13
|
+
opened: boolean
|
|
14
|
+
openedAt?: string
|
|
15
|
+
openCount: number
|
|
16
|
+
clicked: boolean
|
|
17
|
+
clickedAt?: string
|
|
18
|
+
clickCount: number
|
|
19
|
+
clicks: ClickEvent[]
|
|
20
|
+
bounced: boolean
|
|
21
|
+
bouncedAt?: string
|
|
22
|
+
bounceType?: string
|
|
23
|
+
complained: boolean
|
|
24
|
+
complainedAt?: string
|
|
25
|
+
unsubscribed: boolean
|
|
26
|
+
unsubscribedAt?: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ClickEvent {
|
|
30
|
+
url: string
|
|
31
|
+
clickedAt: string
|
|
32
|
+
userAgent?: string
|
|
33
|
+
ipAddress?: string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface EmailMetrics {
|
|
37
|
+
period: string
|
|
38
|
+
sent: number
|
|
39
|
+
delivered: number
|
|
40
|
+
opened: number
|
|
41
|
+
clicked: number
|
|
42
|
+
bounced: number
|
|
43
|
+
complained: number
|
|
44
|
+
unsubscribed: number
|
|
45
|
+
deliveryRate: number
|
|
46
|
+
openRate: number
|
|
47
|
+
clickRate: number
|
|
48
|
+
bounceRate: number
|
|
49
|
+
complaintRate: number
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Email Analytics Module
|
|
54
|
+
*/
|
|
55
|
+
export class EmailAnalyticsModule {
|
|
56
|
+
/**
|
|
57
|
+
* Lambda code for tracking pixel (open tracking)
|
|
58
|
+
*/
|
|
59
|
+
static TrackingPixelLambdaCode = `
|
|
60
|
+
const { DynamoDBClient, UpdateItemCommand, GetItemCommand } = require('@aws-sdk/client-dynamodb');
|
|
61
|
+
|
|
62
|
+
const dynamodb = new DynamoDBClient({});
|
|
63
|
+
const ANALYTICS_TABLE = process.env.ANALYTICS_TABLE;
|
|
64
|
+
|
|
65
|
+
// 1x1 transparent GIF
|
|
66
|
+
const TRACKING_PIXEL = Buffer.from('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', 'base64');
|
|
67
|
+
|
|
68
|
+
exports.handler = async (event) => {
|
|
69
|
+
console.log('Tracking pixel request:', JSON.stringify(event, null, 2));
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
const messageId = event.pathParameters?.messageId || event.queryStringParameters?.mid;
|
|
73
|
+
|
|
74
|
+
if (messageId && ANALYTICS_TABLE) {
|
|
75
|
+
const now = new Date().toISOString();
|
|
76
|
+
|
|
77
|
+
// Update open tracking
|
|
78
|
+
await dynamodb.send(new UpdateItemCommand({
|
|
79
|
+
TableName: ANALYTICS_TABLE,
|
|
80
|
+
Key: { messageId: { S: messageId } },
|
|
81
|
+
UpdateExpression: 'SET opened = :true, openedAt = if_not_exists(openedAt, :now), openCount = if_not_exists(openCount, :zero) + :one',
|
|
82
|
+
ExpressionAttributeValues: {
|
|
83
|
+
':true': { BOOL: true },
|
|
84
|
+
':now': { S: now },
|
|
85
|
+
':zero': { N: '0' },
|
|
86
|
+
':one': { N: '1' },
|
|
87
|
+
},
|
|
88
|
+
}));
|
|
89
|
+
|
|
90
|
+
console.log(\`Tracked open for: \${messageId}\`);
|
|
91
|
+
}
|
|
92
|
+
} catch (error) {
|
|
93
|
+
console.error('Error tracking open:', error);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
statusCode: 200,
|
|
98
|
+
headers: {
|
|
99
|
+
'Content-Type': 'image/gif',
|
|
100
|
+
'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate',
|
|
101
|
+
'Pragma': 'no-cache',
|
|
102
|
+
'Expires': '0',
|
|
103
|
+
},
|
|
104
|
+
body: TRACKING_PIXEL.toString('base64'),
|
|
105
|
+
isBase64Encoded: true,
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
`
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Lambda code for click tracking
|
|
112
|
+
*/
|
|
113
|
+
static ClickTrackingLambdaCode = `
|
|
114
|
+
const { DynamoDBClient, UpdateItemCommand } = require('@aws-sdk/client-dynamodb');
|
|
115
|
+
|
|
116
|
+
const dynamodb = new DynamoDBClient({});
|
|
117
|
+
const ANALYTICS_TABLE = process.env.ANALYTICS_TABLE;
|
|
118
|
+
|
|
119
|
+
exports.handler = async (event) => {
|
|
120
|
+
console.log('Click tracking request:', JSON.stringify(event, null, 2));
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
const messageId = event.pathParameters?.messageId || event.queryStringParameters?.mid;
|
|
124
|
+
const url = event.queryStringParameters?.url;
|
|
125
|
+
|
|
126
|
+
if (!url) {
|
|
127
|
+
return {
|
|
128
|
+
statusCode: 400,
|
|
129
|
+
body: JSON.stringify({ error: 'Missing URL parameter' }),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const decodedUrl = decodeURIComponent(url);
|
|
134
|
+
|
|
135
|
+
if (messageId && ANALYTICS_TABLE) {
|
|
136
|
+
const now = new Date().toISOString();
|
|
137
|
+
const userAgent = event.headers?.['user-agent'] || '';
|
|
138
|
+
const ipAddress = event.requestContext?.identity?.sourceIp || '';
|
|
139
|
+
|
|
140
|
+
// Update click tracking
|
|
141
|
+
await dynamodb.send(new UpdateItemCommand({
|
|
142
|
+
TableName: ANALYTICS_TABLE,
|
|
143
|
+
Key: { messageId: { S: messageId } },
|
|
144
|
+
UpdateExpression: 'SET clicked = :true, clickedAt = if_not_exists(clickedAt, :now), clickCount = if_not_exists(clickCount, :zero) + :one, clicks = list_append(if_not_exists(clicks, :empty), :click)',
|
|
145
|
+
ExpressionAttributeValues: {
|
|
146
|
+
':true': { BOOL: true },
|
|
147
|
+
':now': { S: now },
|
|
148
|
+
':zero': { N: '0' },
|
|
149
|
+
':one': { N: '1' },
|
|
150
|
+
':empty': { L: [] },
|
|
151
|
+
':click': { L: [{ M: {
|
|
152
|
+
url: { S: decodedUrl },
|
|
153
|
+
clickedAt: { S: now },
|
|
154
|
+
userAgent: { S: userAgent },
|
|
155
|
+
ipAddress: { S: ipAddress },
|
|
156
|
+
}}]},
|
|
157
|
+
},
|
|
158
|
+
}));
|
|
159
|
+
|
|
160
|
+
console.log(\`Tracked click for: \${messageId} -> \${decodedUrl}\`);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Redirect to actual URL
|
|
164
|
+
return {
|
|
165
|
+
statusCode: 302,
|
|
166
|
+
headers: {
|
|
167
|
+
'Location': decodedUrl,
|
|
168
|
+
'Cache-Control': 'no-store',
|
|
169
|
+
},
|
|
170
|
+
body: '',
|
|
171
|
+
};
|
|
172
|
+
} catch (error) {
|
|
173
|
+
console.error('Error tracking click:', error);
|
|
174
|
+
return {
|
|
175
|
+
statusCode: 500,
|
|
176
|
+
body: JSON.stringify({ error: 'Internal server error' }),
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
`
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Lambda code for processing SES events
|
|
184
|
+
*/
|
|
185
|
+
static SesEventProcessorCode = `
|
|
186
|
+
const { DynamoDBClient, UpdateItemCommand } = require('@aws-sdk/client-dynamodb');
|
|
187
|
+
|
|
188
|
+
const dynamodb = new DynamoDBClient({});
|
|
189
|
+
const ANALYTICS_TABLE = process.env.ANALYTICS_TABLE;
|
|
190
|
+
|
|
191
|
+
exports.handler = async (event) => {
|
|
192
|
+
console.log('SES event:', JSON.stringify(event, null, 2));
|
|
193
|
+
|
|
194
|
+
for (const record of event.Records) {
|
|
195
|
+
try {
|
|
196
|
+
const message = JSON.parse(record.Sns?.Message || record.body || '{}');
|
|
197
|
+
const eventType = message.eventType || message.notificationType;
|
|
198
|
+
const mail = message.mail || {};
|
|
199
|
+
const messageId = mail.messageId;
|
|
200
|
+
|
|
201
|
+
if (!messageId || !ANALYTICS_TABLE) continue;
|
|
202
|
+
|
|
203
|
+
const now = new Date().toISOString();
|
|
204
|
+
let updateExpression = '';
|
|
205
|
+
const expressionValues = {};
|
|
206
|
+
|
|
207
|
+
switch (eventType) {
|
|
208
|
+
case 'Send':
|
|
209
|
+
updateExpression = 'SET sent = :true, sentAt = :now';
|
|
210
|
+
expressionValues[':true'] = { BOOL: true };
|
|
211
|
+
expressionValues[':now'] = { S: now };
|
|
212
|
+
break;
|
|
213
|
+
|
|
214
|
+
case 'Delivery':
|
|
215
|
+
updateExpression = 'SET delivered = :true, deliveredAt = :now';
|
|
216
|
+
expressionValues[':true'] = { BOOL: true };
|
|
217
|
+
expressionValues[':now'] = { S: now };
|
|
218
|
+
break;
|
|
219
|
+
|
|
220
|
+
case 'Bounce':
|
|
221
|
+
updateExpression = 'SET bounced = :true, bouncedAt = :now, bounceType = :type';
|
|
222
|
+
expressionValues[':true'] = { BOOL: true };
|
|
223
|
+
expressionValues[':now'] = { S: now };
|
|
224
|
+
expressionValues[':type'] = { S: message.bounce?.bounceType || 'Unknown' };
|
|
225
|
+
break;
|
|
226
|
+
|
|
227
|
+
case 'Complaint':
|
|
228
|
+
updateExpression = 'SET complained = :true, complainedAt = :now';
|
|
229
|
+
expressionValues[':true'] = { BOOL: true };
|
|
230
|
+
expressionValues[':now'] = { S: now };
|
|
231
|
+
break;
|
|
232
|
+
|
|
233
|
+
case 'Open':
|
|
234
|
+
updateExpression = 'SET opened = :true, openedAt = if_not_exists(openedAt, :now), openCount = if_not_exists(openCount, :zero) + :one';
|
|
235
|
+
expressionValues[':true'] = { BOOL: true };
|
|
236
|
+
expressionValues[':now'] = { S: now };
|
|
237
|
+
expressionValues[':zero'] = { N: '0' };
|
|
238
|
+
expressionValues[':one'] = { N: '1' };
|
|
239
|
+
break;
|
|
240
|
+
|
|
241
|
+
case 'Click':
|
|
242
|
+
updateExpression = 'SET clicked = :true, clickedAt = if_not_exists(clickedAt, :now), clickCount = if_not_exists(clickCount, :zero) + :one';
|
|
243
|
+
expressionValues[':true'] = { BOOL: true };
|
|
244
|
+
expressionValues[':now'] = { S: now };
|
|
245
|
+
expressionValues[':zero'] = { N: '0' };
|
|
246
|
+
expressionValues[':one'] = { N: '1' };
|
|
247
|
+
break;
|
|
248
|
+
|
|
249
|
+
default:
|
|
250
|
+
console.log(\`Unknown event type: \${eventType}\`);
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
await dynamodb.send(new UpdateItemCommand({
|
|
255
|
+
TableName: ANALYTICS_TABLE,
|
|
256
|
+
Key: { messageId: { S: messageId } },
|
|
257
|
+
UpdateExpression: updateExpression,
|
|
258
|
+
ExpressionAttributeValues: expressionValues,
|
|
259
|
+
}));
|
|
260
|
+
|
|
261
|
+
console.log(\`Processed \${eventType} for: \${messageId}\`);
|
|
262
|
+
} catch (error) {
|
|
263
|
+
console.error('Error processing SES event:', error);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return { statusCode: 200 };
|
|
268
|
+
};
|
|
269
|
+
`
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Create analytics DynamoDB table
|
|
273
|
+
*/
|
|
274
|
+
static createAnalyticsTable(config: { slug: string }): Record<string, any> {
|
|
275
|
+
return {
|
|
276
|
+
[`${config.slug}EmailAnalyticsTable`]: {
|
|
277
|
+
Type: 'AWS::DynamoDB::Table',
|
|
278
|
+
Properties: {
|
|
279
|
+
TableName: `${config.slug}-email-analytics`,
|
|
280
|
+
BillingMode: 'PAY_PER_REQUEST',
|
|
281
|
+
AttributeDefinitions: [
|
|
282
|
+
{ AttributeName: 'messageId', AttributeType: 'S' },
|
|
283
|
+
],
|
|
284
|
+
KeySchema: [
|
|
285
|
+
{ AttributeName: 'messageId', KeyType: 'HASH' },
|
|
286
|
+
],
|
|
287
|
+
TimeToLiveSpecification: {
|
|
288
|
+
AttributeName: 'ttl',
|
|
289
|
+
Enabled: true,
|
|
290
|
+
},
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Create tracking pixel Lambda
|
|
298
|
+
*/
|
|
299
|
+
static createTrackingPixelLambda(config: {
|
|
300
|
+
slug: string
|
|
301
|
+
roleArn: string
|
|
302
|
+
analyticsTable: string
|
|
303
|
+
}): Record<string, any> {
|
|
304
|
+
return {
|
|
305
|
+
[`${config.slug}TrackingPixelLambda`]: {
|
|
306
|
+
Type: 'AWS::Lambda::Function',
|
|
307
|
+
Properties: {
|
|
308
|
+
FunctionName: `${config.slug}-tracking-pixel`,
|
|
309
|
+
Runtime: 'nodejs20.x',
|
|
310
|
+
Handler: 'index.handler',
|
|
311
|
+
Role: config.roleArn,
|
|
312
|
+
Timeout: 10,
|
|
313
|
+
MemorySize: 128,
|
|
314
|
+
Code: {
|
|
315
|
+
ZipFile: EmailAnalyticsModule.TrackingPixelLambdaCode,
|
|
316
|
+
},
|
|
317
|
+
Environment: {
|
|
318
|
+
Variables: {
|
|
319
|
+
ANALYTICS_TABLE: config.analyticsTable,
|
|
320
|
+
},
|
|
321
|
+
},
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Create click tracking Lambda
|
|
329
|
+
*/
|
|
330
|
+
static createClickTrackingLambda(config: {
|
|
331
|
+
slug: string
|
|
332
|
+
roleArn: string
|
|
333
|
+
analyticsTable: string
|
|
334
|
+
}): Record<string, any> {
|
|
335
|
+
return {
|
|
336
|
+
[`${config.slug}ClickTrackingLambda`]: {
|
|
337
|
+
Type: 'AWS::Lambda::Function',
|
|
338
|
+
Properties: {
|
|
339
|
+
FunctionName: `${config.slug}-click-tracking`,
|
|
340
|
+
Runtime: 'nodejs20.x',
|
|
341
|
+
Handler: 'index.handler',
|
|
342
|
+
Role: config.roleArn,
|
|
343
|
+
Timeout: 10,
|
|
344
|
+
MemorySize: 128,
|
|
345
|
+
Code: {
|
|
346
|
+
ZipFile: EmailAnalyticsModule.ClickTrackingLambdaCode,
|
|
347
|
+
},
|
|
348
|
+
Environment: {
|
|
349
|
+
Variables: {
|
|
350
|
+
ANALYTICS_TABLE: config.analyticsTable,
|
|
351
|
+
},
|
|
352
|
+
},
|
|
353
|
+
},
|
|
354
|
+
},
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Create SES event processor Lambda
|
|
360
|
+
*/
|
|
361
|
+
static createSesEventProcessorLambda(config: {
|
|
362
|
+
slug: string
|
|
363
|
+
roleArn: string
|
|
364
|
+
analyticsTable: string
|
|
365
|
+
}): Record<string, any> {
|
|
366
|
+
return {
|
|
367
|
+
[`${config.slug}SesEventProcessorLambda`]: {
|
|
368
|
+
Type: 'AWS::Lambda::Function',
|
|
369
|
+
Properties: {
|
|
370
|
+
FunctionName: `${config.slug}-ses-event-processor`,
|
|
371
|
+
Runtime: 'nodejs20.x',
|
|
372
|
+
Handler: 'index.handler',
|
|
373
|
+
Role: config.roleArn,
|
|
374
|
+
Timeout: 30,
|
|
375
|
+
MemorySize: 256,
|
|
376
|
+
Code: {
|
|
377
|
+
ZipFile: EmailAnalyticsModule.SesEventProcessorCode,
|
|
378
|
+
},
|
|
379
|
+
Environment: {
|
|
380
|
+
Variables: {
|
|
381
|
+
ANALYTICS_TABLE: config.analyticsTable,
|
|
382
|
+
},
|
|
383
|
+
},
|
|
384
|
+
},
|
|
385
|
+
},
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Inject tracking into email HTML
|
|
391
|
+
*/
|
|
392
|
+
static injectTracking(params: {
|
|
393
|
+
html: string
|
|
394
|
+
messageId: string
|
|
395
|
+
trackingDomain: string
|
|
396
|
+
}): string {
|
|
397
|
+
const { html, messageId, trackingDomain } = params
|
|
398
|
+
|
|
399
|
+
// Inject tracking pixel before </body>
|
|
400
|
+
const trackingPixel = `<img src="https://${trackingDomain}/track/open/${messageId}" width="1" height="1" style="display:none" alt="" />`
|
|
401
|
+
let trackedHtml = html.replace('</body>', `${trackingPixel}</body>`)
|
|
402
|
+
|
|
403
|
+
// Replace links with tracking links
|
|
404
|
+
trackedHtml = trackedHtml.replace(
|
|
405
|
+
/href="(https?:\/\/[^"]+)"/g,
|
|
406
|
+
(match, url) => {
|
|
407
|
+
const encodedUrl = encodeURIComponent(url)
|
|
408
|
+
return `href="https://${trackingDomain}/track/click/${messageId}?url=${encodedUrl}"`
|
|
409
|
+
}
|
|
410
|
+
)
|
|
411
|
+
|
|
412
|
+
return trackedHtml
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Calculate email metrics for a period
|
|
417
|
+
*/
|
|
418
|
+
static calculateMetrics(analytics: EmailAnalytics[]): EmailMetrics {
|
|
419
|
+
const sent = analytics.filter(a => a.sent).length
|
|
420
|
+
const delivered = analytics.filter(a => a.delivered).length
|
|
421
|
+
const opened = analytics.filter(a => a.opened).length
|
|
422
|
+
const clicked = analytics.filter(a => a.clicked).length
|
|
423
|
+
const bounced = analytics.filter(a => a.bounced).length
|
|
424
|
+
const complained = analytics.filter(a => a.complained).length
|
|
425
|
+
const unsubscribed = analytics.filter(a => a.unsubscribed).length
|
|
426
|
+
|
|
427
|
+
return {
|
|
428
|
+
period: new Date().toISOString(),
|
|
429
|
+
sent,
|
|
430
|
+
delivered,
|
|
431
|
+
opened,
|
|
432
|
+
clicked,
|
|
433
|
+
bounced,
|
|
434
|
+
complained,
|
|
435
|
+
unsubscribed,
|
|
436
|
+
deliveryRate: sent > 0 ? (delivered / sent) * 100 : 0,
|
|
437
|
+
openRate: delivered > 0 ? (opened / delivered) * 100 : 0,
|
|
438
|
+
clickRate: opened > 0 ? (clicked / opened) * 100 : 0,
|
|
439
|
+
bounceRate: sent > 0 ? (bounced / sent) * 100 : 0,
|
|
440
|
+
complaintRate: delivered > 0 ? (complained / delivered) * 100 : 0,
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
export default EmailAnalyticsModule
|