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

this.$segment.track does not work from vuex action #14

Open
sandoche opened this issue Jan 28, 2021 · 2 comments
Open

this.$segment.track does not work from vuex action #14

sandoche opened this issue Jan 28, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@sandoche
Copy link

Hi again,

I have a pretty strange issue.

When I call this.$segment.track() from a method of my vue it works and I see the event in my segment dashboard.

When I call the exact same this.$segment.track() in an action from vuex it does not do any error but I don't see it in the dashboard of Segment, and this.$segment does exist, when I console log it I see an array of events that I wanted to track but that were not sent to Segment backend.

@sandoche sandoche reopened this Jan 28, 2021
@jedrula
Copy link

jedrula commented Feb 3, 2021

I also encountered this.

For now what I do in my vuex store module is this:

import Vue from 'vue'

Vue.$segment.track('Some info'); // this is somewhere in my vuex action

@gaelreyrol gaelreyrol added the bug Something isn't working label Feb 10, 2021
@nmackey
Copy link

nmackey commented Mar 7, 2022

For anyone who comes here in the future, I ran into this issue as well when implementing my own nuxt segment module (and vue plugin) modeled after this one. From what I could tell the cause is that when segment loads it replaces the window.analytics stubs that were created with the actual segment functions. However somehow the object reference is broken for uses within the nuxt context and nuxt still calls a copy of the stubs so the segment calls never get made.

To fix this I created a proxy object in the vue segment plugin instead of using https://github.com/dansmaculotte/vue-segment/blob/master/src/index.js#L82
I replaced it with:

const proxy = {};
analytics.methods.forEach(method => {
  proxy[method] = (...args) => window.analytics[method](...args);
});
Vue.$segment = Vue.prototype.$segment = proxy;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

4 participants