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

File tree

15 files changed

+134
-60
lines changed

15 files changed

+134
-60
lines changed

.changeset/modern-planes-end.md

Lines changed: 5 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 21 additions & 6 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 16 additions & 2 deletions
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

Lines changed: 16 additions & 9 deletions
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

Lines changed: 8 additions & 2 deletions
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
}

0 commit comments

Comments
 (0)