Skip to content

Instantly share code, notes, and snippets.

@followdarko
Created February 1, 2022 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save followdarko/4077461764c7df65833b7ff27b192193 to your computer and use it in GitHub Desktop.
Save followdarko/4077461764c7df65833b7ff27b192193 to your computer and use it in GitHub Desktop.
import { initializeApp } from 'firebase/app';
import { getAnalytics, logEvent } from 'firebase/analytics';
import { firebaseConfig } from '../store/environment';
/**
* It's important to double-check event names
* cause it's crucial to have same namimg across platforms
*/
const CUSTOM_TYPING = new Set([
'session_started',
'visited_first_time',
'profile_page.loaded',
'profile_page.updated',
]);
export class Tracker {
constructor (firebaseConfig, analytical_id) {
const app = initializeApp(firebaseConfig);
this.analytics = getAnalytics(app);
this.analytical_id = analytical_id;
this.visitedFirstTime(analytical_id);
this.sendEvent('session_started', { analytical_id });
}
/**
* TODO:
* Whould be best to add typings on event options
*/
sendEvent (eventName, options = {}) {
if (!isProduction) { // exist only on dev
if (!CUSTOM_TYPING.has(eventName)) {
console.error('You are using wrong event name. Please double-check event name.');
return;
}
}
logEvent(this.analytics, eventName, options);
}
visitedFirstTime (analytical_id) {
if (readCookie('visited_first_time')) { // true
this.sendEvent('visited_first_time', { analytical_id });
setCookie('visited_first_time');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment