From 57b6d5c8e033a90db526401eb72dcafcfe3b22bd Mon Sep 17 00:00:00 2001 From: mohamed yahia Date: Fri, 5 Jan 2024 18:01:36 +0200 Subject: [PATCH 1/4] [Analytics][l]: Add support for plausible, simpleanalytics, Google, Posthog --- components/analytics/GoogleAnalytics.tsx | 35 +++++++++++ components/analytics/Plausible.tsx | 41 ++++++++++++ components/analytics/Posthog.tsx | 25 ++++++++ components/analytics/SimpleAnalytics.tsx | 29 +++++++++ components/analytics/Umami.tsx | 20 ++++++ components/analytics/index.tsx | 80 ++++++++++++++++++++++++ pages/_app.tsx | 16 +++-- 7 files changed, 240 insertions(+), 6 deletions(-) create mode 100644 components/analytics/GoogleAnalytics.tsx create mode 100644 components/analytics/Plausible.tsx create mode 100644 components/analytics/Posthog.tsx create mode 100644 components/analytics/SimpleAnalytics.tsx create mode 100644 components/analytics/Umami.tsx create mode 100644 components/analytics/index.tsx diff --git a/components/analytics/GoogleAnalytics.tsx b/components/analytics/GoogleAnalytics.tsx new file mode 100644 index 000000000..f7ba69606 --- /dev/null +++ b/components/analytics/GoogleAnalytics.tsx @@ -0,0 +1,35 @@ +import Script from 'next/script.js' + +export interface GoogleAnalyticsProps { + googleAnalyticsId: string +} + +export const GA = ({ googleAnalyticsId }: GoogleAnalyticsProps) => { + return ( + <> + + + ) +} + +// https://developers.google.com/analytics/devguides/collection/gtagjs/events +export const logEvent = (action, category, label, value) => { + // @ts-ignore + window.gtag?.('event', action, { + event_category: category, + event_label: label, + value: value, + }) +} diff --git a/components/analytics/Plausible.tsx b/components/analytics/Plausible.tsx new file mode 100644 index 000000000..9b2361bf6 --- /dev/null +++ b/components/analytics/Plausible.tsx @@ -0,0 +1,41 @@ +import Script from 'next/script.js' + +export interface PlausibleProps { + plausibleDataDomain: string + dataApi?: string + src?: string +} + +/** + * Plausible analytics component. + * To proxy the requests through your own domain, you can use the dataApi and src attribute. + * See [Plausible docs](https://plausible.io/docs/proxy/guides/nextjs#step-2-adjust-your-deployed-script) + * for more information. + * + */ +export const Plausible = ({ + plausibleDataDomain, + dataApi = undefined, + src = 'https://plausible.io/js/plausible.js', +}: PlausibleProps) => { + return ( + <> + + + ) +} + +// https://plausible.io/docs/custom-event-goals +export const logEvent = (eventName, ...rest) => { + return window.plausible?.(eventName, ...rest) +} diff --git a/components/analytics/Posthog.tsx b/components/analytics/Posthog.tsx new file mode 100644 index 000000000..51b3066fd --- /dev/null +++ b/components/analytics/Posthog.tsx @@ -0,0 +1,25 @@ +import Script from 'next/script.js' + +export interface PosthogProps { + posthogProjectApiKey: string + apiHost?: string +} + +/** + * Posthog analytics component. + * See [Posthog docs](https://posthog.com/docs/libraries/js#option-1-add-javascript-snippet-to-your-html-badgerecommendedbadge) for more information. + * + */ +export const Posthog = ({ + posthogProjectApiKey, + apiHost = 'https://app.posthog.com', +}: PosthogProps) => { + return ( + + ) +} diff --git a/components/analytics/SimpleAnalytics.tsx b/components/analytics/SimpleAnalytics.tsx new file mode 100644 index 000000000..49096c209 --- /dev/null +++ b/components/analytics/SimpleAnalytics.tsx @@ -0,0 +1,29 @@ +import Script from 'next/script.js' + +export interface SimpleAnalyticsProps { + src?: string +} + +export const SimpleAnalytics = ({ + src = 'https://scripts.simpleanalyticscdn.com/latest.js', +}: SimpleAnalyticsProps) => { + return ( + <> + + - - ) -} - -// https://developers.google.com/analytics/devguides/collection/gtagjs/events -export const logEvent = (action, category, label, value) => { - // @ts-ignore - window.gtag?.('event', action, { - event_category: category, - event_label: label, - value: value, - }) -} diff --git a/components/analytics/Plausible.tsx b/components/analytics/Plausible.tsx deleted file mode 100644 index 9b2361bf6..000000000 --- a/components/analytics/Plausible.tsx +++ /dev/null @@ -1,41 +0,0 @@ -import Script from 'next/script.js' - -export interface PlausibleProps { - plausibleDataDomain: string - dataApi?: string - src?: string -} - -/** - * Plausible analytics component. - * To proxy the requests through your own domain, you can use the dataApi and src attribute. - * See [Plausible docs](https://plausible.io/docs/proxy/guides/nextjs#step-2-adjust-your-deployed-script) - * for more information. - * - */ -export const Plausible = ({ - plausibleDataDomain, - dataApi = undefined, - src = 'https://plausible.io/js/plausible.js', -}: PlausibleProps) => { - return ( - <> - - - ) -} - -// https://plausible.io/docs/custom-event-goals -export const logEvent = (eventName, ...rest) => { - return window.plausible?.(eventName, ...rest) -} diff --git a/components/analytics/Posthog.tsx b/components/analytics/Posthog.tsx deleted file mode 100644 index 51b3066fd..000000000 --- a/components/analytics/Posthog.tsx +++ /dev/null @@ -1,25 +0,0 @@ -import Script from 'next/script.js' - -export interface PosthogProps { - posthogProjectApiKey: string - apiHost?: string -} - -/** - * Posthog analytics component. - * See [Posthog docs](https://posthog.com/docs/libraries/js#option-1-add-javascript-snippet-to-your-html-badgerecommendedbadge) for more information. - * - */ -export const Posthog = ({ - posthogProjectApiKey, - apiHost = 'https://app.posthog.com', -}: PosthogProps) => { - return ( - - ) -} diff --git a/components/analytics/SimpleAnalytics.tsx b/components/analytics/SimpleAnalytics.tsx deleted file mode 100644 index 49096c209..000000000 --- a/components/analytics/SimpleAnalytics.tsx +++ /dev/null @@ -1,29 +0,0 @@ -import Script from 'next/script.js' - -export interface SimpleAnalyticsProps { - src?: string -} - -export const SimpleAnalytics = ({ - src = 'https://scripts.simpleanalyticscdn.com/latest.js', -}: SimpleAnalyticsProps) => { - return ( - <> - -