Skip to content

Commit

Permalink
Merge pull request #157 from MilesHan/main
Browse files Browse the repository at this point in the history
add Microsoft Clarity Analytics
  • Loading branch information
timlrx authored Mar 31, 2024
2 parents a9e057d + 64a18b1 commit 00fc285
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ const analytics: AnalyticsConfig = {
googleAnalytics: {
googleAnalyticsId: '', // e.g. G-XXXXXXX
},
clarityAnalytics: {
ClarityWebsiteId: '', // e.g. abcdefjhij
},
}

export default function Layout() {
Expand Down Expand Up @@ -114,6 +117,21 @@ export default function Layout() {
}
```

#### Microsoft Clarity Analytics

```tsx
import { GA } from 'pliny/analytics/MicrosoftClarity'

const ClarityWebsiteId = '' // e.g. abcdefjhij

export default function Layout() {
return (
...
<Clarity ClarityWebsiteId={ClarityWebsiteId} />
)
}
```

#### Plausible Analytics

```tsx
Expand Down
23 changes: 23 additions & 0 deletions packages/pliny/src/analytics/MicrosoftClarity.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Script from 'next/script.js'

export interface ClarityProps {
ClarityWebsiteId: string
}

// https://clarity.microsoft.com/
// https://learn.microsoft.com/en-us/clarity/setup-and-installation/clarity-setup
export const Clarity = ({ ClarityWebsiteId }: ClarityProps) => {
return (
<>
<Script id="microsoft-clarity-init" strategy="afterInteractive">
{`
(function(c,l,a,r,i,t,y){
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "${ClarityWebsiteId}");
`}
</Script>
</>
)
}
10 changes: 8 additions & 2 deletions packages/pliny/src/analytics/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Plausible, PlausibleProps } from './Plausible'
import { SimpleAnalytics, SimpleAnalyticsProps } from './SimpleAnalytics.js'
import { Umami, UmamiProps } from './Umami'
import { Posthog, PosthogProps } from './Posthog'
import { Clarity, ClarityProps } from './MicrosoftClarity'

declare global {
interface Window {
Expand All @@ -19,6 +20,7 @@ export interface AnalyticsConfig {
umamiAnalytics?: UmamiProps
posthogAnalytics?: PosthogProps
simpleAnalytics?: SimpleAnalyticsProps
clarityAnalytics?: ClarityProps
}

/**
Expand All @@ -29,6 +31,7 @@ export interface AnalyticsConfig {
* umamiWebsiteId: '', // e.g. 123e4567-e89b-12d3-a456-426614174000
* posthogProjectApiKey: '', // e.g. AhnJK8392ndPOav87as450xd
* googleAnalyticsId: '', // e.g. UA-000000-2 or G-XXXXXXX
* ClarityWebsiteId: '', // e.g. abcdefjhij
* }
*/
export interface AnalyticsProps {
Expand Down Expand Up @@ -65,10 +68,13 @@ export const Analytics = ({ analyticsConfig }: AnalyticsProps) => {
{isProduction && analyticsConfig.googleAnalytics && (
<GA {...analyticsConfig.googleAnalytics} />
)}
{isProduction && analyticsConfig.clarityAnalytics && (
<Clarity {...analyticsConfig.clarityAnalytics} />
)}
</>
)
}

export { GA, Plausible, SimpleAnalytics, Umami, Posthog }
export { GA, Plausible, SimpleAnalytics, Umami, Posthog, Clarity }

export type { GoogleAnalyticsProps, PlausibleProps, UmamiProps, PosthogProps, SimpleAnalyticsProps }
export type { GoogleAnalyticsProps, PlausibleProps, UmamiProps, PosthogProps, SimpleAnalyticsProps, ClarityProps }
3 changes: 3 additions & 0 deletions packages/pliny/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ const sampleConfig: PlinyConfig = {
googleAnalytics: {
googleAnalyticsId: '', // e.g. G-XXXXXXX
},
clarityAnalytics: {
ClarityWebsiteId: '', // e.g. abcdefjhij
},
},
newsletter: {
// supports mailchimp, buttondown, convertkit, klaviyo, revue, emailOctopus
Expand Down

0 comments on commit 00fc285

Please sign in to comment.