4 ms·
Something that has always puzzled me is why only GA follows the saner approach to push all the function calls in an array which the async loaded script can pick
by shubhamjain 9y ago
Something that has always puzzled me is why only GA follows the saner approach to push all the function calls in an array which the async loaded script can pick up later. Many of the popular analytics solutions (like, Segment, Mixpanel) create a factory function that initialises all API calls with a generic body. It seems rather unnecessary and only adds to the boilerplate. Take a look at the GA's init code and compare it with Segment's [1].
[1]: https://prnt.sc/fpj9ec https://prnt.sc/fpj9ec
- CaveTech 9y agoSome libraries (fb, ga) using a single factory method that handles all commands. Ex: fb('init', {}) The others (segment, heap) use explicit functions for different actions. Ex: heap.init() The main benefit is you can detect API violations without loading the async script. If you call an invalid function with the former you have to wait for it to execute at some point in the future. This allows you to surface errors immediately - breaking current exection - even if it's additional libraries havent loaded.
- TheAceOfHearts 9y agoHaving written one of those analytics snippets... Hindsight is 20:20. When I wrote it, having a facade class seemed reasonable. Eventually I realized the GA approach would've been better, but at that point we couldn't change it because we were worried it would freak out our customers.