Skip to content

Use VueRouter Lifecycle Hook for Page Calls Instead #12

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

Open
wants to merge 4 commits 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
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,41 @@ export default {
</script>
```

Or betterstill just hook into VueRouter afterEach Lifecycle, and negate the need to set the page on each component


```js
import Vue from 'vue'
import VueRouter from 'vue-router'

import Dashboard from '@/Dashboard.vue';
import About from '@/About.vue';

Vue.use(VueRouter)

const router = new VueRouter({
routes: [
{
path: '/dashboard',
name: 'Dashboard',
component: Dashboard
},
{
path: '/about',
name: 'about',
component: About
}
]
});

router.afterEach((to) => {
//window.analytics.page(to.name) // if you prefer Name
window.analytics.page(to.fullPath)
});

export default router;
```

## 🔍 Step 3: Identify Users
The `identify` method is how you tell Segment who the current user is. It includes a unique User ID and any optional traits you can pass on about them. You can read more about this in the [identify reference](https://segment.com/docs/sources/website/analytics.js/#identify?utm_source=github&utm_medium=click&utm_campaign=protos_vue).

Expand Down
5 changes: 1 addition & 4 deletions src/components/About.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

<script>
export default {
name: 'About',
mounted () {
window.analytics.page('About')
}
name: 'About'
}
</script>

Expand Down
5 changes: 1 addition & 4 deletions src/components/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

<script>
export default {
name: 'Home',
mounted () {
window.analytics.page('Home')
}
name: 'Home'
}
</script>
8 changes: 7 additions & 1 deletion src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import About from '@/components/About'

Vue.use(Router)

export default new Router({
const router = new Router({
routes: [
{
path: '/',
Expand All @@ -19,3 +19,9 @@ export default new Router({
}
]
})

router.afterEach((to) => {
window.analytics.page(to.fullPath)
});

export default router;