Skip to content

Commit a4efa5a

Browse files
committed
feat: from vue2 to vue3
1 parent 8038ebe commit a4efa5a

File tree

3 files changed

+47
-53
lines changed

3 files changed

+47
-53
lines changed

README.md

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,56 @@ For testing purpose, don't forget to [turn it off yourself](https://support.goog
1515
### Browser
1616
```
1717
<script src="https://unpkg.com/vue/dist/vue.js"></script>
18-
<script src="https://unpkg.com/vue-mixpanel@1.0.7/index.js"></script>
18+
<script src="https://unpkg.com/vue-mixpanel@2.0.0/index.js"></script>
1919
```
2020
### Package Managers
2121
```
2222
npm install vue-mixpanel --save
2323
yarn add vue-mixpanel --save
24-
25-
// require
26-
var Vue = require('vue')
27-
Vue.use(require('vue-mixpanel'), {
28-
token: YOUR_TOKEN
29-
})
30-
31-
// import
32-
import Vue from 'vue'
33-
import VueMixpanel from 'vue-mixpanel'
34-
Vue.use(VueMixpanel, {
35-
token: YOUR_TOKEN
36-
})
3724
```
3825

3926
### How does it work?
4027

41-
- Initialize it by using the token given by your [Mixpanel](https://mixpanel.com/) account in the `Vue.use()` inside you `main.js`
42-
- Start using their public API through `this.$mixpanel` in your components.
28+
- Initialize it by using the token given by your [Mixpanel](https://mixpanel.com/) account in the `app.use()` inside you `main.js`
29+
- Start using their public API with `inject` through `this.mixpanel` in your components.
4330

4431
## Example Usage
4532

46-
#### Initialize with config
33+
>If you're using Vue 2 or below, please check the version [1.1.0](https://github.com/Loschcode/vue-mixpanel/releases/tag/1.1.0)
34+
35+
#### Initialize with configuration
4736
```
48-
Vue.use(VueMixpanel, {
37+
import App from './App.vue'
38+
import VueMixpanel from 'vue-mixpanel'
39+
40+
const app = createApp(App)
41+
app.use(VueMixpanel, {
4942
token: YOUR_TOKEN,
5043
config: {
5144
debug: true
5245
}
5346
})
47+
app.mount('#app')
48+
```
49+
50+
Then, you can use it in your components like so
51+
52+
```
53+
<script>
54+
export default {
55+
inject: ['mixpanel'],
56+
name: 'App',
57+
58+
created () {
59+
this.mixpanel.track('test')
60+
}
61+
}
62+
</script>
5463
```
5564

5665
#### Track an event
5766
```
58-
this.$mixpanel.track('event name', {
67+
this.mixpanel.track('event name', {
5968
distinct_id: 'unique client id',
6069
property_1: 'value 1',
6170
property_2: 'value 2',
@@ -65,12 +74,12 @@ this.$mixpanel.track('event name', {
6574

6675
#### Create an alias
6776
```
68-
this.$mixpanel.alias('distinct_id', 'your_alias');
77+
this.mixpanel.alias('distinct_id', 'your_alias');
6978
```
7079

7180
#### Increment a numeric property
7281
```
73-
this.$mixpanel.people.increment('13793', 'games_played');
82+
this.mixpanel.people.increment('13793', 'games_played');
7483
```
7584

7685
## License

index.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
/**
3-
* Vue Mixpanel v1.1.0
3+
* Vue Mixpanel v2.0.0
44
* https://github.com/Loschcode/vue-mixpanel
55
*
66
* Copyright 2020-2021, cmp-cc
@@ -11,16 +11,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1111
};
1212
Object.defineProperty(exports, "__esModule", { value: true });
1313
var mixpanel_browser_1 = __importDefault(require("mixpanel-browser"));
14-
var VueMixpanel = {
15-
install: function () { }
14+
exports.default = {
15+
install: function (app, _a) {
16+
var _b = _a.config, config = _b === void 0 ? {} : _b, token = _a.token;
17+
var defaultConfig = {};
18+
var endConfig = Object.assign(config, defaultConfig);
19+
mixpanel_browser_1.default.init(token, endConfig);
20+
app.provide('mixpanel', mixpanel_browser_1.default);
21+
}
1622
};
17-
VueMixpanel.install = function (Vue, _a) {
18-
var config = _a.config, token = _a.token;
19-
if (typeof config !== 'object')
20-
config = {};
21-
Vue.prototype.$mixpanel = mixpanel_browser_1.default;
22-
var defaultConfig = {};
23-
var endConfig = Object.assign(config, defaultConfig);
24-
Vue.prototype.$mixpanel.init(token, endConfig);
25-
};
26-
exports.default = VueMixpanel;

index.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Vue Mixpanel v1.1.0
2+
* Vue Mixpanel v2.0.0
33
* https://github.com/Loschcode/vue-mixpanel
44
*
55
* Copyright 2020-2021, cmp-cc
@@ -8,23 +8,12 @@
88

99
import mixpanel from 'mixpanel-browser'
1010

11-
interface VueMixpanelType {
12-
install: object
13-
}
11+
export default {
12+
install: (app: any, { config = {}, token }: { config: object; token: string;}) => {
13+
const defaultConfig = {}
14+
const endConfig = Object.assign(config, defaultConfig)
1415

15-
let VueMixpanel: VueMixpanelType = {
16-
install: () => {}
16+
mixpanel.init(token, endConfig)
17+
app.provide('mixpanel', mixpanel)
18+
}
1719
}
18-
19-
VueMixpanel.install = function (Vue: any, { config, token }: { config: object; token: string; }) {
20-
if (typeof config !== 'object') config = {}
21-
22-
Vue.prototype.$mixpanel = mixpanel
23-
24-
const defaultConfig = {}
25-
const endConfig = Object.assign(config, defaultConfig)
26-
27-
Vue.prototype.$mixpanel.init(token, endConfig)
28-
}
29-
30-
export default VueMixpanel

0 commit comments

Comments
 (0)