Skip to content

Commit 98d1866

Browse files
authored
Merge pull request #120 from timlrx/v2
Fix algolia css and refactor analytics scripts
2 parents c539a38 + 1fe56fc commit 98d1866

15 files changed

+134
-60
lines changed

.changeset/modern-planes-end.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'pliny': patch
3+
---
4+
5+
Expose option to configure host for analytics scripts

.changeset/poor-buses-remember.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'pliny': patch
3+
---
4+
5+
Fix algolia css specificity issues with tailwind

.changeset/pre.json

+2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"great-chairs-warn",
1515
"metal-coats-grab",
1616
"mighty-garlics-appear",
17+
"modern-planes-end",
1718
"neat-tables-occur",
19+
"poor-buses-remember",
1820
"pretty-worms-help",
1921
"proud-brooms-laugh",
2022
"tall-rocks-hear",

README.md

+21-6
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,31 @@ module.exports = {
6363

6464
The `Analytics` component provides an easy interface to switch between different analytics providers. It might not be as feature rich as the official analytics providers but it should be sufficient for simple use cases.
6565

66+
All components default to the hosted service, but can be configured to use a self-hosted or proxied version of the script by providing the `src` / `apiHost` props to the respective analytics component.
67+
68+
Note: As an external script will be loaded, do ensure that `script-src` in the content security policy of `next.config.js` has been configured to whitelist the domain.
69+
6670
```tsx
6771
import { Analytics, AnalyticsConfig } from 'pliny/analytics'
6872

6973
const analytics: AnalyticsConfig = {
70-
plausibleDataDomain: '', // e.g. tailwind-nextjs-starter-blog.vercel.app
71-
simpleAnalytics: false, // true or false
72-
umamiWebsiteId: '', // e.g. 123e4567-e89b-12d3-a456-426614174000
73-
posthogProjectApiKey: '', // e.g. AhnJK8392ndPOav87as450xd
74-
googleAnalyticsId: '', // e.g. UA-000000-2 or G-XXXXXXX
75-
}
74+
// If you want to use an analytics provider you have to add it to the
75+
// content security policy in the `next.config.js` file.
76+
// supports Plausible, Simple Analytics, Umami, Posthog or Google Analytics.
77+
plausibleAnalytics: {
78+
plausibleDataDomain: '', // e.g. tailwind-nextjs-starter-blog.vercel.app
79+
},
80+
simpleAnalytics: {},
81+
umamiAnalytics: {
82+
umamiWebsiteId: '', // e.g. 123e4567-e89b-12d3-a456-426614174000
83+
},
84+
posthogAnalytics: {
85+
posthogProjectApiKey: '', // e.g. 123e4567-e89b-12d3-a456-426614174000
86+
},
87+
googleAnalytics: {
88+
googleAnalyticsId: '', // e.g. G-XXXXXXX
89+
},
90+
}
7691

7792
export default function Layout() {
7893
return (

packages/pliny/CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# pliny
22

3+
## 0.1.0-beta.11
4+
5+
### Patch Changes
6+
7+
- e0aa18c: Expose option to configure host for analytics scripts
8+
- 562605b: Fix algolia css specificity issues with tailwind
9+
310
## 0.1.0-beta.10
411

512
### Patch Changes

packages/pliny/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "pliny",
33
"description": "Main entry point for pliny components",
44
"homepage": "https://github.com/timlrx/pliny",
5-
"version": "0.1.0-beta.10",
5+
"version": "0.1.0-beta.11",
66
"type": "module",
77
"exports": {
88
"./*": "./*",

packages/pliny/public/algolia.css

+2-4
Original file line numberDiff line numberDiff line change
@@ -782,10 +782,8 @@ svg.DocSearch-Hit-Select-Icon {
782782
--docsearch-logo-color: theme(colors.gray.300);
783783
}
784784

785-
.light .DocSearch-Input {
786-
box-shadow: 0 0 #0000;
787-
}
788-
785+
.light .DocSearch-Input,
789786
.dark .DocSearch-Input {
790787
box-shadow: 0 0 #0000;
788+
background: transparent;
791789
}

packages/pliny/src/analytics/Plausible.tsx

+16-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,29 @@ import Script from 'next/script.js'
22

33
export interface PlausibleProps {
44
plausibleDataDomain: string
5+
dataApi?: string
6+
src?: string
57
}
68

7-
export const Plausible = ({ plausibleDataDomain }: PlausibleProps) => {
9+
/**
10+
* Plausible analytics component.
11+
* To proxy the requests through your own domain, you can use the dataApi and src attribute.
12+
* See [Plausible docs](https://plausible.io/docs/proxy/guides/nextjs#step-2-adjust-your-deployed-script)
13+
* for more information.
14+
*
15+
*/
16+
export const Plausible = ({
17+
plausibleDataDomain,
18+
dataApi = undefined,
19+
src = 'https://plausible.io/js/plausible.js',
20+
}: PlausibleProps) => {
821
return (
922
<>
1023
<Script
1124
strategy="lazyOnload"
1225
data-domain={plausibleDataDomain}
13-
src="https://plausible.io/js/plausible.js"
26+
data-api={dataApi}
27+
src={src}
1428
/>
1529
<Script strategy="lazyOnload" id="plausible-script">
1630
{`

packages/pliny/src/analytics/Posthog.tsx

+16-9
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@ import Script from 'next/script.js'
22

33
export interface PosthogProps {
44
posthogProjectApiKey: string
5+
apiHost?: string
56
}
67

7-
export const Posthog = ({ posthogProjectApiKey }: PosthogProps) => {
8+
/**
9+
* Posthog analytics component.
10+
* See [Posthog docs](https://posthog.com/docs/libraries/js#option-1-add-javascript-snippet-to-your-html-badgerecommendedbadge) for more information.
11+
*
12+
*/
13+
export const Posthog = ({
14+
posthogProjectApiKey,
15+
apiHost = 'https://app.posthog.com',
16+
}: PosthogProps) => {
817
return (
9-
<>
10-
<Script strategy="lazyOnload" id="posthog-script">
11-
{`
12-
!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
13-
posthog.init('${posthogProjectApiKey}',{api_host:'https://app.posthog.com'})
14-
`}
15-
</Script>
16-
</>
18+
<Script strategy="lazyOnload" id="posthog-script">
19+
{`
20+
!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
21+
posthog.init('${posthogProjectApiKey}',{api_host:'${apiHost}'})
22+
`}
23+
</Script>
1724
)
1825
}

packages/pliny/src/analytics/SimpleAnalytics.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import Script from 'next/script.js'
22

3-
export const SimpleAnalytics = () => {
3+
export interface SimpleAnalyticsProps {
4+
src?: string
5+
}
6+
7+
export const SimpleAnalytics = ({
8+
src = 'https://scripts.simpleanalyticscdn.com/latest.js',
9+
}: SimpleAnalyticsProps) => {
410
return (
511
<>
612
<Script strategy="lazyOnload" id="sa-script">
713
{`
814
window.sa_event=window.sa_event||function(){var a=[].slice.call(arguments);window.sa_event.q?window.sa_event.q.push(a):window.sa_event.q=[a]};
915
`}
1016
</Script>
11-
<Script strategy="lazyOnload" src="https://scripts.simpleanalyticscdn.com/latest.js" />
17+
<Script strategy="lazyOnload" src={src} />
1218
</>
1319
)
1420
}

packages/pliny/src/analytics/Umami.tsx

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ import Script from 'next/script.js'
22

33
export interface UmamiProps {
44
umamiWebsiteId: string
5+
src?: string
56
}
67

7-
export const Umami = ({ umamiWebsiteId }: UmamiProps) => {
8+
export const Umami = ({
9+
umamiWebsiteId,
10+
src = 'https://analytics.umami.is/script.js',
11+
}: UmamiProps) => {
812
return (
9-
<>
10-
<Script
11-
async
12-
defer
13-
data-website-id={umamiWebsiteId}
14-
src="https://umami.example.com/umami.js" // Replace with your umami instance
15-
/>
16-
</>
13+
<Script
14+
async
15+
defer
16+
data-website-id={umamiWebsiteId}
17+
src={src} // Replace with your umami instance
18+
/>
1719
)
1820
}
+24-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { GA, GoogleAnalyticsProps } from './GoogleAnalytics'
33
import { Plausible, PlausibleProps } from './Plausible'
4-
import { SimpleAnalytics } from './SimpleAnalytics.js'
4+
import { SimpleAnalytics, SimpleAnalyticsProps } from './SimpleAnalytics.js'
55
import { Umami, UmamiProps } from './Umami'
66
import { Posthog, PosthogProps } from './Posthog'
77

@@ -13,12 +13,12 @@ declare global {
1313
}
1414
}
1515

16-
export interface AnalyticsConfig
17-
extends Partial<GoogleAnalyticsProps>,
18-
Partial<PlausibleProps>,
19-
Partial<PosthogProps>,
20-
Partial<UmamiProps> {
21-
simpleAnalytics?: boolean
16+
export interface AnalyticsConfig {
17+
googleAnalytics?: GoogleAnalyticsProps
18+
plausibleAnalytics?: PlausibleProps
19+
umamiAnalytics?: UmamiProps
20+
posthogAnalytics?: PosthogProps
21+
simpleAnalytics?: SimpleAnalyticsProps
2222
}
2323

2424
/**
@@ -38,32 +38,37 @@ export interface AnalyticsProps {
3838
const isProduction = process.env.NODE_ENV === 'production'
3939

4040
/**
41-
* Supports plausible, simpleAnalytics, umami or googleAnalytics.
42-
* If you want to use an analytics provider you have to add it to the
41+
* Supports Plausible, Simple Analytics, Umami, Posthog or Google Analytics.
42+
* All components default to the hosted service, but can be configured to use a self-hosted
43+
* or proxied version of the script by providing the `src` / `apiHost` props.
44+
*
45+
* Note: If you want to use an analytics provider you have to add it to the
4346
* content security policy in the `next.config.js` file.
4447
* @param {AnalyticsProps} { analytics }
4548
* @return {*}
4649
*/
4750
export const Analytics = ({ analyticsConfig }: AnalyticsProps) => {
4851
return (
4952
<>
50-
{isProduction && analyticsConfig.plausibleDataDomain && (
51-
<Plausible plausibleDataDomain={analyticsConfig.plausibleDataDomain} />
53+
{isProduction && analyticsConfig.plausibleAnalytics && (
54+
<Plausible {...analyticsConfig.plausibleAnalytics} />
5255
)}
53-
{isProduction && analyticsConfig.simpleAnalytics && <SimpleAnalytics />}
54-
{isProduction && analyticsConfig.posthogProjectApiKey && (
55-
<Posthog posthogProjectApiKey={analyticsConfig.posthogProjectApiKey} />
56+
{isProduction && analyticsConfig.simpleAnalytics && (
57+
<SimpleAnalytics {...analyticsConfig.simpleAnalytics} />
5658
)}
57-
{isProduction && analyticsConfig.umamiWebsiteId && (
58-
<Umami umamiWebsiteId={analyticsConfig.umamiWebsiteId} />
59+
{isProduction && analyticsConfig.posthogAnalytics && (
60+
<Posthog {...analyticsConfig.posthogAnalytics} />
5961
)}
60-
{isProduction && analyticsConfig.googleAnalyticsId && (
61-
<GA googleAnalyticsId={analyticsConfig.googleAnalyticsId} />
62+
{isProduction && analyticsConfig.umamiAnalytics && (
63+
<Umami {...analyticsConfig.umamiAnalytics} />
64+
)}
65+
{isProduction && analyticsConfig.googleAnalytics && (
66+
<GA {...analyticsConfig.googleAnalytics} />
6267
)}
6368
</>
6469
)
6570
}
6671

6772
export { GA, Plausible, SimpleAnalytics, Umami, Posthog }
6873

69-
export type { GoogleAnalyticsProps, PlausibleProps, UmamiProps, PosthogProps }
74+
export type { GoogleAnalyticsProps, PlausibleProps, UmamiProps, PosthogProps, SimpleAnalyticsProps }

packages/pliny/src/comments/Disqus.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const Disqus = ({ shortname, slug }: DisqusProps) => {
3333
//@ts-ignore
3434
window.DISQUS.reset({ reload: true })
3535
}
36-
}, [])
36+
}, [shortname, slug])
3737

3838
useEffect(() => {
3939
LoadComments()

packages/pliny/src/comments/Utterances.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const Utterances = ({ theme, darkTheme, repo, label, issueTerm }: Utteran
3737
const comments = document.getElementById(COMMENTS_ID)
3838
if (comments) comments.innerHTML = ''
3939
}
40-
}, [commentsTheme, issueTerm])
40+
}, [commentsTheme, issueTerm, label, repo])
4141

4242
// Reload on theme change
4343
useEffect(() => {

packages/pliny/src/config.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,20 @@ const sampleConfig: PlinyConfig = {
5555
analytics: {
5656
// If you want to use an analytics provider you have to add it to the
5757
// content security policy in the `next.config.js` file.
58-
// supports plausible, simpleAnalytics, umami or googleAnalytics
59-
plausibleDataDomain: '', // e.g. tailwind-nextjs-starter-blog.vercel.app
60-
simpleAnalytics: false, // true or false
61-
umamiWebsiteId: '', // e.g. 123e4567-e89b-12d3-a456-426614174000
62-
posthogProjectApiKey: '', // e.g. AhnJK8392ndPOav87as450xd
63-
googleAnalyticsId: '', // e.g. UA-000000-2 or G-XXXXXXX
58+
// supports Plausible, Simple Analytics, Umami, Posthog or Google Analytics.
59+
plausibleAnalytics: {
60+
plausibleDataDomain: '', // e.g. tailwind-nextjs-starter-blog.vercel.app
61+
},
62+
simpleAnalytics: {},
63+
umamiAnalytics: {
64+
umamiWebsiteId: '', // e.g. 123e4567-e89b-12d3-a456-426614174000
65+
},
66+
posthogAnalytics: {
67+
posthogProjectApiKey: '', // e.g. 123e4567-e89b-12d3-a456-426614174000
68+
},
69+
googleAnalytics: {
70+
googleAnalyticsId: '', // e.g. G-XXXXXXX
71+
},
6472
},
6573
newsletter: {
6674
// supports mailchimp, buttondown, convertkit, klaviyo, revue, emailOctopus

0 commit comments

Comments
 (0)