
Typescript default parameters in constructor for GA
Typescript default parameters are the same as in scala. While working on multple google analytics projects it is good to now we can hardcode part of the object and then just use the simple, single object in a constructor, without having to pass the default values.
However !!
Typescript default parameters should be the last on the parameter list in constructor
That said the not optional, required and demanded parameters need to be first. Otherwise intellij or what ever other ide You use might complain and will NOT TELL YOU THAT. I love that small little things You assume would be obvious but then You spend a lot of time to in the end hit a facepalm on Your head…
expost class GoogleAnalyticsV4Event {
//no need to define fields
constructor(
public customData : ExampleAnalyticsEvent , // THIS HAS TO BE FIRST !!
private event:string = 'ga4_event'
private event_name:string = 'our_event_name'
){
this.customData = customData
}
export type ExampleAnalyticsEvent = {
eventCategory?:string;
eventAction?:string;
eventLabel?:string;
}
After that You can juse use it like below and i would always recommend to make in as a const function.
const makeEvent = () => new GoogleAnalyticsV4Event ({
eventCategory:"category", eventAction:"action",eventLabel:"sameLabel"
})
Google recommends for version 4
- using only event name with concatenated values with a delimiter like _ or # , similar as keys in dynamoDb for aws
- The less custom events the better, it makes it easier to create reports from the 'Explore’ view.
- We will not get the option to click and go deeper and deeper into nested events. Everything should be flat based on the recommendation, one big flat table is always easier on the other hand.
- Google saves a lot of money to store and aggregate that data that way 🙂

