Created
February 1, 2022 12:35
-
-
Save followdarko/4077461764c7df65833b7ff27b192193 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
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