-
-
Notifications
You must be signed in to change notification settings - Fork 15
Add anonymous usage telemetry to Email SDK and CLI #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@opencoredev/email-sdk": minor | ||
| --- | ||
|
|
||
| Add anonymous usage telemetry to the SDK and CLI via PostHog. The client now reports `client created` (configured adapter names), `email sent` (adapter, success/failure, error code, duration, recipient count), and the CLI reports `cli command run` (command, adapter, success). No email content, addresses, headers, or credentials are ever collected, and custom adapter names are masked as `custom`. A one-time notice with opt-out instructions is printed on first use. Opt out with `EMAIL_SDK_TELEMETRY=0`, `DO_NOT_TRACK=1`, or `createEmailClient({ telemetry: false })`; telemetry is disabled automatically when `NODE_ENV=test`. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,8 @@ import type { | |
| SendBatchResult, | ||
| SendOptions, | ||
| } from "./types.js"; | ||
| import { assertMessage, toProviderError } from "./utils.js"; | ||
| import { getTelemetry, normalizeAdapterName } from "./telemetry.js"; | ||
| import { arrayify, assertMessage, toProviderError } from "./utils.js"; | ||
|
|
||
| const defaultDelay = (attempt: number) => Math.min(100 * 2 ** (attempt - 1), 2_000); | ||
|
|
||
|
|
@@ -83,6 +84,15 @@ export function createEmailClient< | |
| throw new EmailProviderNotFoundError(defaultProvider); | ||
| } | ||
|
|
||
| const telemetry = options.telemetry === false ? undefined : getTelemetry(); | ||
|
|
||
| void telemetry?.capture("client created", { | ||
| adapters: [...adapters.keys()].map(normalizeAdapterName), | ||
| adapter_count: adapters.size, | ||
| plugin_count: options.plugins?.length ?? 0, | ||
| default_adapter: normalizeAdapterName(defaultProvider), | ||
| }); | ||
|
|
||
| const hooks = [...pluginHooks, ...(options.hooks ? [options.hooks] : [])]; | ||
| const client: EmailClient = { | ||
| adapters, | ||
|
|
@@ -102,18 +112,48 @@ export function createEmailClient< | |
| return client.adapter<TProvider>(name); | ||
| }, | ||
| async send(message, sendOptions) { | ||
| return sendWithAdapters({ | ||
| adapters, | ||
| message, | ||
| options: { | ||
| hookList: hooks, | ||
| middleware, | ||
| retry: options.retry, | ||
| defaultProvider, | ||
| fallback: options.fallback, | ||
| }, | ||
| sendOptions, | ||
| }); | ||
| const startedAt = Date.now(); | ||
| const messageFacts = { | ||
| recipients: | ||
| arrayify(message.to).length + arrayify(message.cc).length + arrayify(message.bcc).length, | ||
| has_attachments: (message.attachments?.length ?? 0) > 0, | ||
|
Comment on lines
+116
to
+119
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| }; | ||
|
|
||
| try { | ||
| const response = await sendWithAdapters({ | ||
| adapters, | ||
| message, | ||
| options: { | ||
| hookList: hooks, | ||
| middleware, | ||
| retry: options.retry, | ||
| defaultProvider, | ||
| fallback: options.fallback, | ||
| }, | ||
| sendOptions, | ||
| }); | ||
|
|
||
| void telemetry?.capture("email sent", { | ||
| ...messageFacts, | ||
| adapter: normalizeAdapterName(response.provider), | ||
| success: true, | ||
| duration_ms: Date.now() - startedAt, | ||
| }); | ||
|
|
||
| return response; | ||
| } catch (error) { | ||
| void telemetry?.capture("email sent", { | ||
| ...messageFacts, | ||
| adapter: normalizeAdapterName( | ||
| sendOptions?.adapter ?? sendOptions?.provider ?? defaultProvider, | ||
| ), | ||
| success: false, | ||
| duration_ms: Date.now() - startedAt, | ||
| error_code: error instanceof EmailSdkError ? error.code : "unknown", | ||
| }); | ||
|
|
||
| throw error; | ||
| } | ||
| }, | ||
| async sendBatch(messages, sendOptions) { | ||
| const results: SendBatchResult[] = []; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.