A collection of analytics helper functions.
- Plausible
- Ahoy
Make sure the API object is available at window.plausible. It usually is if the snippet was inserted via <script>.
Make sure the API object is available at window.ahoy. If you use it as NPM module you can expose the object like this:
importahoyfrom"ahoy.js"window.ahoy=ahoy- Install
$yarnadd @oddcamp/analytics- Enjoy
import{enableAutoEventAnalytics}from"@oddcamp/analytics"enableAutoEventAnalytics()HTML
<ahref="/" data-event-analytics='{"name": "Click", "props": {"trigger": "anchor"}}'>
Link
</a>ERB
<%= link_to 'Link', root_path, data: {event_analytics: {name: 'Click', props: {trigger: 'anchor'}}} %>JSX
<ahref="/"data-event-analytics={JSON.stringify({name: 'Click',props: {trigger: 'anchor'}})}>
Link
</a>- Create
.envand set variables of analytics services you prefer to test:PLAUSIBLE_DOMAIN=something.siteAHOY_SCRIPT_URL=https://unpkg.com/ahoy.js@0.4.0/dist/ahoy.js
$ yarn install$ yarn dev- localhost:1234
Enable automatic event analytics for element clicks and form submissions.
Defaults:
enableAutoEventAnalytics({
attributeName =`event-analytics`,
sourceNode =document,
services =[`plausible`,`ahoy`],
debug =false,})Returns: function which disables automatic event analytics if executed, e.g.:
constdisableAutoEventAnalytics=enableAutoEventAnalytics()// ...disableAutoEventAnalytics()The value of attributeName attribute must be a JSON, e.g.:
{"name": "",// name; mandatory"props": {},// custom properties"options": {"allFieldsAsProps": false,// sets all form fields as props on form submissions}}Get all form field name and value pairs as props using boolean allFieldsAsProps
option or specify field(s) manually by setting data-event-analytics-field
attribute, e.g.
<formdata-event-analytics="{...}"><inputtype="search" name="query" data-event-analytics-field></form>Note: the attribute name may differ if you did set a custom value for
attributeName parameter (${attributeName}-field).
Analyticizes an event.
Defaults:
analyticizeEvent({ data, services =[`plausible`,`ahoy`],
debug =false})Example:
analyticizeEvent({data: {name: `Event name`,props: {key: `value`,},},})