-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Product analytics * Product analytics * Product analytics --------- Co-authored-by: Paweł Chyła <chyla1988@gmail.com>
- Loading branch information
1 parent
cbd9c46
commit fea94f6
Showing
11 changed files
with
170 additions
and
2 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,5 @@ | ||
--- | ||
"saleor-dashboard": patch | ||
--- | ||
|
||
Add product analytics on cloud |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,55 @@ | ||
import { render, screen } from "@testing-library/react"; | ||
import React from "react"; | ||
|
||
import { ProductAnalytics } from "."; | ||
|
||
jest.mock("posthog-js/react", () => ({ | ||
PostHogProvider: ({ children }: { children: React.ReactNode }) => ( | ||
<>{children} with posthog</> | ||
), | ||
})); | ||
|
||
describe("ProductAnalytics", () => { | ||
const ENV_COPY = process.env; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
process.env = { ...ENV_COPY }; // Make a copy | ||
}); | ||
|
||
afterAll(() => { | ||
process.env = ENV_COPY; // Restore old environment | ||
}); | ||
|
||
it("renders analytics when it is cloud instance and config was provided", async () => { | ||
// Arrange | ||
process.env.IS_CLOUD_INSTANCE = "true"; | ||
process.env.POSTHOG_KEY = "key"; | ||
process.env.POSTHOG_HOST = "host"; | ||
|
||
// Act | ||
render(<ProductAnalytics>Test</ProductAnalytics>); | ||
|
||
// Assert | ||
await screen.findByText("Test with posthog"); | ||
}); | ||
|
||
it("does not renders analytics when there is no config", async () => { | ||
// Arrange | ||
process.env.IS_CLOUD_INSTANCE = "true"; | ||
|
||
// Act | ||
render(<ProductAnalytics>Test</ProductAnalytics>); | ||
|
||
// Assert | ||
await screen.findByText("Test"); | ||
}); | ||
|
||
it("does not renders analytics it is not cloud instance", async () => { | ||
// Act | ||
render(<ProductAnalytics>Test</ProductAnalytics>); | ||
|
||
// Assert | ||
await screen.findByText("Test"); | ||
}); | ||
}); |
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,47 @@ | ||
import { PostHogProvider } from "posthog-js/react"; | ||
import React from "react"; | ||
|
||
const useConfig = () => { | ||
const options = { | ||
api_host: process.env.POSTHOG_HOST, | ||
}; | ||
const apiKey = process.env.POSTHOG_KEY; | ||
const isCloudInstance = process.env.IS_CLOUD_INSTANCE; | ||
const canRenderAnalytics = () => { | ||
if (!isCloudInstance) { | ||
return false; | ||
} | ||
|
||
if (!options.api_host || !apiKey) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
return { | ||
config: { | ||
options, | ||
apiKey, | ||
}, | ||
canRenderAnalytics, | ||
}; | ||
}; | ||
|
||
interface ProductAnalyticsProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
export const ProductAnalytics = ({ children }: ProductAnalyticsProps) => { | ||
const { canRenderAnalytics, config } = useConfig(); | ||
|
||
if (!canRenderAnalytics()) { | ||
return <>{children}</>; | ||
} | ||
|
||
return ( | ||
<PostHogProvider apiKey={config.apiKey} options={config.options}> | ||
{children} | ||
</PostHogProvider> | ||
); | ||
}; |
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