Skip to content

Instantly share code, notes, and snippets.

@ZAYEC77
Created January 29, 2020 21:41
Show Gist options
  • Save ZAYEC77/2348471dd3723ec22666275ef72a0dd5 to your computer and use it in GitHub Desktop.
Save ZAYEC77/2348471dd3723ec22666275ef72a0dd5 to your computer and use it in GitHub Desktop.
const METHODS = {
TRACK: 'track',
IDENTIFY: 'identify',
};
class AnalyticsService {
providers = [];
idenitify(userId, userData) {
this.callMethod(METHODS.IDENTIFY, [userId, userData]);
}
track(event, properties) {
this.callMethod(METHODS.TRACK, [event, properties]);
}
registerProvider(provider) {
this.providers.push(provider);
}
callMethod(method, args) {
this.providers.map(p => p.handle(method, args));
}
}
class BaseAnalyticsProvider {
constructor() {
if (typeof this.handle !== 'function') {
throw new TypeError('Method "handle" must be implemented');
}
}
}
class GoogleAnalyticsProvider extends BaseAnalyticsProvider {
constructor(apiKey) {
super();
// do some stuff
}
handle(method, args) {
console.log("GA: ", method, args);
}
}
class AmplitudeAnalyticsProvider extends BaseAnalyticsProvider {
handle(method, args) {
console.log("Amplitude: ", method, args);
}
}
analyticsService = new AnalyticsService();
analyticsService.registerProvider(new GoogleAnalyticsProvider());
analyticsService.registerProvider(new AmplitudeAnalyticsProvider());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment