Implements segment.io tracking to @thrivehive projects
npm install @thrivehive/segment@thrivehive/segment

Implements Segment.io identify, page, and track methods, and adds a setup function to inject the segment script into your page with your write key.
Also removes the need to paste the segment JS snippet into .
A Vue directive is also included to make event tracking easier.
``bash`
npm install @thrivehive/segment
Inject the official segment.js script into the page with your write key:
`js
import { setup } from '@thrivehive/segment';
setup('
`
Wrapper for analytics.identity:
`js
// Foo.vue
import { identify } from '@thrivehive/segment';
const user = {
name: 'Jane Doe',
active: true
}
export default {
mounted() {
identify(user.name, {
active: user.active
});
}
}
`
Wrapper for analytics.track:
`js
// Foo.vue
import { trackEvent } from '@thrivehive/segment';
export default {
methods: {
foo() {
trackEvent('Button clicked', properties);
}
}
}
`
Wrapper for analytics.page:
`js
// router.js
import Vue from 'vue';
import Router from 'vue-router';
import { trackPageView } from '@thrivehive/segment';
Vue.use(Router);
const router = new Router({
mode: 'history',
routes: [
{
path: '/',
name: 'search',
component: Search
}
]
});
router.afterEach((to) => {
trackPageView(to.name, to.query);
});
`
@thrivehive/segment also includes a Vue plugin. The plugin will handle the setup function for you, and adds a Vue directive for easy event tracking. More info about Vue directives.
Setup the plugin:
`js
import Vue from 'vue';
import { VueSegment } from '@thrivehive/segment';
Vue.use(VueSegment, {
key: '
});
`
The directive accepts a single argument, the eventName to listen for. By default, this is a Vue event listener, not native.
Example using button:
`html`
The expression passed to the directive may one of the following:
-
- description: Message to send to segment.io
-
Example usage with message only:
`html`
Example usage with verbose syntax:
`html
v-segment:submit="{
message: 'User submitted form',
data: trackingInfo
}"
>
`
It is also possible to listen to a native event instead of a Vue event. Just like with normal Vue event listeners, you can use the .native modifier to use the native event listener:
`html``