Skip to content

Instantly share code, notes, and snippets.

@jbwyme
Last active September 24, 2019 05:47
Show Gist options
  • Save jbwyme/5044b4ab8350d47f23bb09b2957fbdb6 to your computer and use it in GitHub Desktop.
Save jbwyme/5044b4ab8350d47f23bb09b2957fbdb6 to your computer and use it in GitHub Desktop.
import {MixpanelInstance} from 'react-native-mixpanel';
class MixpanelStub {
constructor() {
this.queue = {track: []};
}
track(event, properties) {
console.log('queuing event for tracking');
let resolve, reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
})
this.queue.track.push({args: [event, properties], resolve, reject});
return promise;
}
}
const Mixpanel = {
_instances: {},
initialize(apiToken) {
const instance = new MixpanelInstance(apiToken);
instance.initialize().then(() => {
this._onInitialized(instance);
});
const stub = new MixpanelStub();
this._instances[apiToken] = stub;
return stub;
},
_onInitialized(instance) {
const stub = this._instances[instance.apiToken];
Object.keys(stub.queue).forEach(funcName => {
stub.queue[funcName].forEach(({args, resolve, reject}) => {
instance[funcName].call(instance, args)
console.log(funcName, args);
resolve();
});
});
Object.assign(stub, instance);
}
}
const mixpanel = Mixpanel.initialize("6888bfdec29d84ab2d36ae18c57b8535");
mixpanel.track("hello world").then(() => console.log('promise resolved'));
class MixpanelStub {
constructor() {
this.queue = {track: []};
}
track(event, properties) {
console.log('queuing event for tracking');
this.queue.track.push({args: [event, properties], promise: new Promise);
}
}
const Mixpanel = {
_instances: {},
initialize(apiToken) {
const instance = new MixpanelInstance(apiToken);
instance.initialize().then(() => {
this._onInitialized(instance);
});
const stub = new MixpanelStub();
this._instances[apiToken] = stub;
return stub;
},
_onInitialized(instance) {
const stub = this._instances[instance.apiToken];
Object.keys(stub.queue).forEach(funcName => {
stub.queue[funcName].forEach(args => {
instance[funcName].call(instance, args)
console.log(funcName, args);
});
});
Object.assign(stub, instance);
}
}
const mixpanel = Mixpanel.initialize("6888bfdec29d84ab2d36ae18c57b8535");
mixpanel.track("hello world");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment