Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/plausible/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const analytics = Analytics({
plausiblePlugin({
domain: "example.com", // By default window.location.hostname
hashMode: true, // By default 'false'
trackLocalhost: true, // By default 'false'
apiHost: "https://my-plausible-api.domain.com/", // By default 'https://plausible.io'
}),
],
Expand All @@ -29,11 +28,10 @@ const analytics = Analytics({

### Options

`plausiblePlugin()` accepts some [options](https://plausible-tracker.netlify.app/globals.html#plausibleinitoptions) that you may want to provide:
`plausiblePlugin()` accepts some options that you may want to provide:

| Option | Type | Description | Default |
| -------------- | -------- | ----------------------------------------------------------------- | ------------------------ |
| domain | `string` | Your site's domain, as declared by you in Plausible's settings | `location.hostname` |
| hashMode | `bool` | Enables tracking based on URL hash changes. | `false` |
| trackLocalhost | `bool` | Enables tracking on *localhost*. | `false` |
| apiHost | `string` | Plausible's API host to use. Change this if you are self-hosting. | `'https://plausible.io'` |
2 changes: 1 addition & 1 deletion packages/plausible/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
},
"dependencies": {
"analytics": "^0.7.5",
"plausible-tracker": "^0.3.1"
"plausible-client": "^1.1.0"
}
}
31 changes: 9 additions & 22 deletions packages/plausible/src/plausible.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AnalyticsInstance } from "analytics"
import Plausible from "plausible-tracker"
import { Plausible } from "plausible-client"

type IPluginProps = {
instance: AnalyticsInstance
Expand All @@ -10,7 +10,6 @@ type IPluginConfig = {
apiHost?: string,
domain?: string,
hashMode?: boolean,
trackLocalhost?: boolean,
}

interface IProps {
Expand All @@ -25,7 +24,7 @@ type IPayload = {
}

type IPluginContext = {
plausible: ReturnType<typeof Plausible> | null
plausible: Plausible | null
}

const defaultConfig = {
Expand All @@ -48,26 +47,12 @@ export default function plausiblePlugin(pluginConfig: IPluginConfig) {
const {
apiHost,
domain,
hashMode,
trackLocalhost,
} = config

const plausibleConfig = {
apiHost,
domain,
hashMode,
trackLocalhost,
}

context.plausible = Plausible(
Object.keys(plausibleConfig).reduce((acc, key) => {
if (typeof plausibleConfig[key] !== "undefined") {
return { ...acc, [key]: plausibleConfig[key] }
}

return acc
}, {})
)
context.plausible = new Plausible({
domain: domain || (typeof window !== 'undefined' ? window.location.hostname : ''),
apiHost: apiHost || 'https://plausible.io',
})
},

loaded: () => {
Expand All @@ -78,7 +63,9 @@ export default function plausiblePlugin(pluginConfig: IPluginConfig) {
if (!context.plausible) return

const { properties } = payload
context.plausible.trackPageview({}, { props: properties })
context.plausible.sendEvent('pageview', {
props: properties
})
},

// Set parameter scope at event level with 'event' method
Expand Down
8 changes: 4 additions & 4 deletions packages/plausible/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ dlv@^1.1.3:
resolved "https://registry.yarnpkg.com/dlv/-/dlv-1.1.3.tgz#5c198a8a11453596e751494d49874bc7732f2e79"
integrity sha512-+HlytyjlPKnIG8XuRG8WvmBP8xs8P71y+SKKS6ZXWoEgLuePxtDoUEiH7WkdePWrQ5JBpE6aoVqfZfJUQkjXwA==

plausible-tracker@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/plausible-tracker/-/plausible-tracker-0.3.1.tgz#dd4e1f59cafc7bf1b00e30890567287b3f204efd"
integrity sha512-oXnVyfTF2gzkfK3iOZCRZEnf8sH4gBASaJhY+clwWW1orIybfQpfbgg4U5Bwj5dW/4iNstM4tx4jMCVUfMCUbg==
plausible-client@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/plausible-client/-/plausible-client-1.1.0.tgz#acee082705133c68f3da8470a82a830799bb5b90"
integrity sha512-iP2Y31w3qgWvI1B+CIwq9jrkPyoU1t/IEiy+zWfK1wFTfUTwDbbugPC+EzxFsDwJaRceGZ1kame4Ug0u2SAhYw==

typescript@^4.2.4:
version "4.2.4"
Expand Down