Created
January 29, 2020 21:41
-
-
Save ZAYEC77/2348471dd3723ec22666275ef72a0dd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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