Using the adapter pattern

When building an app, a program, or a web server, it is good practice to configure logging or analytics so it is easy to inspect program behavior at runtime. When running on a debugger or deployed on a user's device, or on a server, requirements for logging or recording analytics may change. During development, for example, it is not recommended to send analytics to the production server.

The adapter pattern is a great way to abstract and connect different logging or analytics infrastructures or classes, depending on runtime. It is based on the idea that classes will wrap your specialized code, in order to provide a common interface. This is very useful when dealing with third-party libraries, on which the code isn't easily changeable.

For this example, we'll use two very popular tracking libraries as examples: Mixpanel and Google Analytics for Firebase. We'll demonstrate how it is possible to unify the analytics into a single interface, and how to use one or the other at runtime.