-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
include mailing_version in posthog analytics (#401)
* implement the comment, not sure why this was never implemented? * scripts/prepare-release sed's in MAILING_VERSION * send mailing_version with events to posthog * try it here * include mailing version and mailing core version in server side events * use 0.9.5-next.0 in cli and web * Revert "use 0.9.5-next.0 in cli and web" This reverts commit 08fe115. * update tests and ignore undefined * read the versions from package.json * refactor capture and test core * cli actually does expect to send in dev, oops * test vercel deploy * revert * return early if POSTHOG_API_KEY to avoid throwing page errors & 401's if POSTHOG_API_KEY is undefined * test api/sendMail * Revert "test api/sendMail" This reverts commit acfa8da. * load consts in next components with project paths so they load from node_modules in an installed app (where they've been replaced with string literals) * export MAILING_VERSION and POSTHOG_API_KEY from packages/cli/src/index.ts and import those from "mailing" in the frontend tsx * move cli code from index.ts -> cli.ts * Revert "move cli code from index.ts -> cli.ts" This reverts commit 6a4096f. * Revert "export MAILING_VERSION and POSTHOG_API_KEY from packages/cli/src/index.ts and import those from "mailing" in the frontend tsx" This reverts commit ab7eb75. * Revert "load consts in next components with project paths so they load from node_modules in an installed app (where they've been replaced with string literals)" This reverts commit 6922bca. * search and replace in release script (and then undo after release) * wwooops * mailing-core should more properly add mailing_core_version * fix test * try to fix error when using mailing-core in production: mailing calling capture with {"event":"mail sent","distinctId":"unknown","properties":{"mailing_version":"0.9.6-next.0","recipientCount":1,"analyticsEnabled":false}} mailing posthog capture error TypeError: Cannot read properties of undefined (reading 'reInit') at PostHog.capture (/var/task/node_modules/posthog-node/lib/index.cjs.js:2043:10) at _callee$ (/var/task/.mailing/.next/server/chunks/3414.js:727:13) at tryCatch (/var/task/.mailing/.next/server/chunks/3414.js:167:17) at Generator._invoke (/var/task/.mailing/.next/server/chunks/3414.js:150:24) at Generator.next (/var/task/.mailing/.next/server/chunks/3414.js:192:21) at asyncGeneratorStep (/var/task/.mailing/.next/server/chunks/3414.js:457:24) at _next (/var/task/.mailing/.next/server/chunks/3414.js:476:9) at /var/task/.mailing/.next/server/chunks/3414.js:481:7 at new Promise (<anonymous>) at /var/task/.mailing/.next/server/chunks/3414.js:473:12 * better type * changeset * now that we are searching and replacing in src, it's unneccessary to search and replace in dist. this is the same and simpler / less code * update snapshots
- Loading branch information
1 parent
37c6a82
commit 164940f
Showing
13 changed files
with
197 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"mailing": patch | ||
"mailing-core": patch | ||
"web": patch | ||
--- | ||
|
||
To better help us improve mailing, anonymous analytics now includes the version of mailing and mailing-core that you are using. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const MAILING_VERSION = process.env.MAILING_VERSION; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/cli/src/util/postHog/__test__/indexMockMailingVersion.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { capture } from ".."; | ||
import * as postHogClient from "../client"; | ||
import { PostHog } from "posthog-node"; | ||
|
||
jest.mock("../../../const/mailingVersion", () => ({ | ||
MAILING_VERSION: "mock-mailing-version", | ||
})); | ||
|
||
describe("posthog", () => { | ||
const MM_DEV_OG = process.env.MM_DEV; | ||
const MM_DEV_E2E_OG = process.env.MM_E2E; | ||
|
||
let mockPostHogClient: PostHog; | ||
beforeEach(() => { | ||
mockPostHogClient = { capture: jest.fn() } as unknown as PostHog; | ||
jest.restoreAllMocks(); | ||
delete process.env.MM_DEV; | ||
delete process.env.MM_E2E; | ||
}); | ||
|
||
afterEach(() => { | ||
process.env.MM_DEV = MM_DEV_OG; | ||
process.env.MM_E2E = MM_DEV_E2E_OG; | ||
}); | ||
|
||
it("should include MAILING_VERSION because it is defined", () => { | ||
jest | ||
.spyOn(postHogClient, "postHogClient") | ||
.mockImplementation(() => mockPostHogClient); | ||
|
||
capture({ | ||
distinctId: "abc", | ||
event: "ate pizza", | ||
}); | ||
|
||
expect(mockPostHogClient.capture).toHaveBeenCalledWith({ | ||
distinctId: "abc", | ||
event: "ate pizza", | ||
properties: { | ||
mailing_version: "mock-mailing-version", | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const MAILING_CORE_VERSION = process.env.MAILING_CORE_VERSION; |
44 changes: 44 additions & 0 deletions
44
packages/core/src/util/postHog/__test__/indexMockMailingCoreVersion.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { capture } from ".."; | ||
import * as postHogClient from "../client"; | ||
import { PostHog } from "posthog-node"; | ||
|
||
jest.mock("../../../const/mailingCoreVersion", () => ({ | ||
MAILING_CORE_VERSION: "mock-mailing-core-version", | ||
})); | ||
|
||
describe("posthog", () => { | ||
const MM_DEV_OG = process.env.MM_DEV; | ||
const MM_DEV_E2E_OG = process.env.MM_E2E; | ||
|
||
let mockPostHogClient: PostHog; | ||
beforeEach(() => { | ||
mockPostHogClient = { capture: jest.fn() } as unknown as PostHog; | ||
jest.restoreAllMocks(); | ||
delete process.env.MM_DEV; | ||
delete process.env.MM_E2E; | ||
}); | ||
|
||
afterEach(() => { | ||
process.env.MM_DEV = MM_DEV_OG; | ||
process.env.MM_E2E = MM_DEV_E2E_OG; | ||
}); | ||
|
||
it("should include MAILING_CORE_VERSION because it is defined", async () => { | ||
jest | ||
.spyOn(postHogClient, "postHogClient") | ||
.mockImplementation(() => mockPostHogClient); | ||
|
||
await capture({ | ||
distinctId: "abc", | ||
event: "ate pizza", | ||
}); | ||
|
||
expect(mockPostHogClient.capture).toHaveBeenCalledWith({ | ||
distinctId: "abc", | ||
event: "ate pizza", | ||
properties: { | ||
mailing_core_version: "mock-mailing-core-version", | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env bash | ||
|
||
# load .env file | ||
if [[ -f .env ]]; then | ||
source .env | ||
fi | ||
|
||
# throw an error if $POSTHOG_API_KEY is not set | ||
if [[ -z "$POSTHOG_API_KEY" ]]; then | ||
echo "Must provide POSTHOG_API_KEY in environment" 1>&2 | ||
exit 1 | ||
fi | ||
|
||
# set variables MAILING_VERSION and MAILING_CORE_VERSION to the value of the "version" attribute from each package.json | ||
MAILING_VERSION=$(awk -F'"' '/"version": ".+"/{ print $4; exit; }' packages/cli/package.json) | ||
MAILING_CORE_VERSION=$(awk -F'"' '/"version": ".+"/{ print $4; exit; }' packages/core/package.json) | ||
|
||
# throw an error if $MAILING_VERSION is empty | ||
if [[ -z "$MAILING_VERSION" ]]; then | ||
echo "Failed to set MAILING_VERSION" 1>&2 | ||
exit 1 | ||
fi | ||
|
||
# throw an error if $MAILING_CORE_VERSION is empty | ||
if [[ -z "$MAILING_CORE_VERSION" ]]; then | ||
echo "Must provide MAILING_CORE_VERSION in environment" 1>&2 | ||
exit 1 | ||
fi | ||
|
||
## | ||
# Search and replace in src directories -- these will be undone in the post-release script | ||
|
||
# search cli and core src and replace "process.env.POSTHOG_API_KEY" with "\"${process.env.POSTHOG_API_KEY}\"" | ||
find 'packages/cli/src' 'packages/core/src' -type f \( -name '*.ts' -o -name '*.tsx' \) -exec sed -i '' "s/process\.env\.POSTHOG_API_KEY/\"$POSTHOG_API_KEY\"/g" {} + | ||
|
||
# search cli src and replace "process.env.MAILING_VERSION" with "\"${process.env.MAILING_VERSION}\"" | ||
find 'packages/cli/src' -type f \( -name '*.ts' -o -name '*.tsx' \) -exec sed -i '' "s/MAILING_VERSION = process\.env\.MAILING_VERSION/MAILING_VERSION = \"$MAILING_VERSION\"/g" {} + | ||
|
||
# search core src and replace "process.env.MAILING_CORE_VERSION" with "\"${process.env.MAILING_CORE_VERSION}\"" | ||
find 'packages/core/src' -type f \( -name '*.ts' -o -name '*.tsx' \) -exec sed -i '' "s/MAILING_CORE_VERSION = process\.env\.MAILING_CORE_VERSION/MAILING_CORE_VERSION = \"$MAILING_CORE_VERSION\"/g" {} + | ||
|
||
## build packages | ||
yarn build | ||
yarn changeset publish | ||
|
||
## | ||
# Undo search and replace in src directories from above | ||
|
||
# search cli and core src and replace "process.env.POSTHOG_API_KEY" with "\"${process.env.POSTHOG_API_KEY}\"" | ||
find 'packages/cli/src' 'packages/core/src' -type f \( -name '*.ts' -o -name '*.tsx' \) -exec sed -i '' "s/\"$POSTHOG_API_KEY\"/process\.env\.POSTHOG_API_KEY/g" {} + | ||
|
||
# search cli src and replace "process.env.MAILING_VERSION" with "\"${process.env.MAILING_VERSION}\"" | ||
find 'packages/cli/src' -type f \( -name '*.ts' -o -name '*.tsx' \) -exec sed -i '' "s/MAILING_VERSION = \"$MAILING_VERSION\"/MAILING_VERSION = process\.env\.MAILING_VERSION/g" {} + | ||
|
||
# search core src and replace "process.env.MAILING_CORE_VERSION" with "\"${process.env.MAILING_CORE_VERSION}\"" | ||
find 'packages/core/src' -type f \( -name '*.ts' -o -name '*.tsx' \) -exec sed -i '' "s/MAILING_CORE_VERSION = \"$MAILING_CORE_VERSION\"/MAILING_CORE_VERSION = process\.env\.MAILING_CORE_VERSION/g" {} + |
164940f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
mailing-dynamic-demo – ./
mailing-dynamic-demo.vercel.app
mailing-dynamic-demo-git-main-sofn.vercel.app
mailing-dynamic-demo-sofn.vercel.app
demo.mailing.run