Skip to content

Commit

Permalink
feat: ahoy (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Osvaldas Valutis authored Jan 23, 2023
1 parent ea9a5b9 commit 4dca9ed
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ A collection of analytics helper functions.
## Supported analytics services

- Plausible
- Ahoy

### Plausible

Make sure the API object is available at `window.plausible`. It usually is if the snippet was inserted via `<script>`.

### Plausible

Make sure the API object is available at `window.ahoy`. If you use it as NPM module you can expose the object like this:

```js
import ahoy from "ahoy.js"

window.ahoy = ahoy
```


###

## Usage

Expand Down Expand Up @@ -47,7 +65,8 @@ JSX
## Development

1. Create `.env` and set variables of analytics services you prefer to test:
- `PLAUSIBLE_DOMAIN`
- `PLAUSIBLE_DOMAIN=something.site`
- `AHOY_SCRIPT_URL=https://unpkg.com/[email protected]/dist/ahoy.js`
2. `$ yarn install`
3. `$ yarn dev`
3. [localhost:1234](http://localhost:1234)
Expand All @@ -64,7 +83,7 @@ _Defaults:_
enableAutoEventAnalytics({
attributeName = `event-analytics`,
sourceNode = document,
services = [`plausible`],
services = [`plausible`, `ahoy`],
debug = false,
})
```
Expand Down Expand Up @@ -111,7 +130,7 @@ _Defaults:_
```js
analyticizeEvent({
data,
services = [`plausible`],
services = [`plausible`, `ahoy`],
debug = false
})
```
Expand Down
4 changes: 4 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<if condition="plausible_domain">
<script defer data-domain="{{ plausible_domain }}" src="https://plausible.io/js/script.local.js"></script>
</if>

<if condition="ahoy_script_url">
<script defer src="{{ ahoy_script_url }}"></script>
</if>
</head>

<body>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@oddcamp/analytics",
"description": "Odd Camp analytics helpers library",
"version": "0.0.6",
"version": "0.0.7",
"author": "Odd Camp <[email protected]>",
"license": "MIT",
"main": "src/index.js",
Expand Down
5 changes: 4 additions & 1 deletion posthtml.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ require(`dotenv`).config({ path: `.env` })
module.exports = {
plugins: {
"posthtml-expressions": {
locals: { plausible_domain: process.env.PLAUSIBLE_DOMAIN },
locals: {
plausible_domain: process.env.PLAUSIBLE_DOMAIN,
ahoy_script_url: process.env.AHOY_SCRIPT_URL,
},
},
},
}
22 changes: 21 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isPlainObject, get } from "lodash"

const SERVICES = [`plausible`]
const SERVICES = [`plausible`, `ahoy`]

function enableAutoEventAnalytics({
attributeName = `event-analytics`,
Expand Down Expand Up @@ -196,6 +196,22 @@ function analyticizeEvent({ data, services = SERVICES, debug = false } = {}) {
}
break
}

case `ahoy`: {
if (window && window.ahoy) {
debugLog(debug, `Ahoy event analyzation has been requested`, data)

window.ahoy.track(data.name, { ...(data.props || {}) })

debugLog(debug, `Ahoy event has been analyzed`, data)
} else {
debugLog(
debug,
`Ahoy event analysation requested, but service is not available`
)
}
break
}
}
})
}
Expand All @@ -206,6 +222,10 @@ function anyServicesAvailable(services) {
case `plausible`: {
return !!window.plausible
}

case `ahoy`: {
return !!window.ahoy
}
}

return false
Expand Down

0 comments on commit 4dca9ed

Please sign in to comment.