Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add analytics handler (mixpanel) #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ Vue.use(VueAB, {
Note: The expiry date is refreshed with every page visit. The entry only
expires, if the user doesn't come back in the specified time.

### Analytics options

You can set an ***analytics handler***, on initialization. The only internally supported option currently is mixpanel, that will set a super property on the user like 'AB [test name]' equal to the wining slot name

``` js
Vue.use(VueAB, {
analytics: 'mixpanel'
})
```

If you want to create your own ***analytics handler*** you can simply pass a function

``` js
Vue.use(VueAB, {
analytics: (name, winner) => console.log('AB Test', name, winner)
})
```

### Component Name

By default `<split-test>` is the component that wraps a new test. This name can be overwritten on initialization:
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"build": "cross-env NODE_ENV=production rollup -c",
"dev": "cross-env NODE_ENV=development rollup -cw"
},
"dependencies": {
},
"dependencies": {},
"devDependencies": {
"cross-env": "^5.0.0",
"rollup": "^0.41.4",
Expand Down
15 changes: 15 additions & 0 deletions src/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

function mixpanel (name, winner, countdown=100) {
if (window.mixpanel) {
var super_property = {}
super_property['A/B ' + name] = winner
window.mixpanel.register_once(super_property)
} else {
// loop until mixpanel is loaded
setTimeout(() => mixpanel(name, winner, countdown - 1), 1000)
}
}

export default {
mixpanel
}
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { storage, selectAB } from './persistence'
import { randomCandidate } from './toolbox'

import analytics from './analytics.js'
const VueAB = {
abtest: selectAB,
randomCandidate,
Expand Down Expand Up @@ -28,6 +28,12 @@ const VueAB = {
|| randomCandidate(ctx.children)

storage.entry = {name, winner}

if (analytics[options.analytics]) {
analytics[options.analytics](name, winner)
} else if (typeof options.analytics === "function"){
options.analytics(name, winner)
}
return variations[winner]
}
})
Expand Down