<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Abhijeet Kumar on Medium]]></title>
        <description><![CDATA[Stories by Abhijeet Kumar on Medium]]></description>
        <link>https://medium.com/@abhijeetk.9719?source=rss-e8fee6b90fe------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*PafHH3UgeoVksFt91CS6PQ.jpeg</url>
            <title>Stories by Abhijeet Kumar on Medium</title>
            <link>https://medium.com/@abhijeetk.9719?source=rss-e8fee6b90fe------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 13:21:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abhijeetk.9719/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Handling analytics in Android like a pro using delegations!]]></title>
            <link>https://medium.com/@abhijeetk.9719/handling-analytics-in-android-like-a-pro-using-delegations-42270d08e2af?source=rss-e8fee6b90fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/42270d08e2af</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[androiddev]]></category>
            <dc:creator><![CDATA[Abhijeet Kumar]]></dc:creator>
            <pubDate>Sun, 02 Oct 2022 06:14:51 GMT</pubDate>
            <atom:updated>2022-10-02T06:14:51.237Z</atom:updated>
            <content:encoded><![CDATA[<p>App analytics plays a very important role in growing your Android app as they enable us to make better decisions based on user, device, network, and other behaviours. There are a lot of analytics libraries out there tailored for Android apps like Firebase Analytics, Mixpanel etc.</p><p>While integrating these SDKs are quite straightforward, the real complexity starts at the code level. Let me explain with an example, assume we’ve two people working on an app, <em>Agent-47 </em>and <em>Agent-48</em>. Now, the Product Manager <em>Diana</em> comes and asks <em>Agent-47 </em>to implement analytics into the app and she wants to see which all screens were opened and what all buttons were clicked by users. Now, <em>Agent-47</em>, like a pro he is, quickly integrates the analytics SDK and writes the implementation of it.</p><p>For handling screen analytics, he writes the following approach.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1f4784f085a19a0be2b977ecb325c6c/href">https://medium.com/media/e1f4784f085a19a0be2b977ecb325c6c/href</a></iframe><p>And for tracking button clicks he’s doing like this,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/250c6f21cf73c7fc9028e53bbe8abe16/href">https://medium.com/media/250c6f21cf73c7fc9028e53bbe8abe16/href</a></iframe><p><em>Agent-47</em> tests this and everything seems to work as expected, he pushes his code and goes on a holiday. But suddenly our Product Manager <em>Diana </em>comes up with a new requirement where she needs 3 new screens with 3 buttons in each of them, as <em>Agent-47</em> is on holiday she asks <em>Agent-48</em> to implement these new requirements. <em>Agent-48</em> is a rookie, but he implemented all the screens and buttons and the associated functionality.</p><p>Talking about analytics in these screens there are a total of, <strong>2(opening and closing of screens) * 3(number of screens) * 3(number of buttons on each screen) = 18 unique events</strong> of interest happening that we’d want to send for analytics.</p><p>But during implementation, <em>Agent-48 </em>being a rookie, forgot to write analytics trackers for one of the screens and a couple of buttons. Also, notice the code duplication here. Similar code needs to replicated at 18 places!</p><p>… Smell something? Yes, that’s the code smelling and let me explain why.</p><p>Firstly, as the same code is being copied in ~18 places, making any changes to it would be a very painful task. For example, if we’d also want to track which event was triggered by which user, we’ll have to modify our method definition to add user id like,</p><pre>MixpanelHelper.logInteractionEvent(<strong>“help button”</strong>, userId)</pre><p>We can already guess that after updating this method we’ll need to update its usage of it in all the places.</p><p>Secondly, there is no enforcement mechanism that is laying down the rules so that rookies like <em>Agent-48 </em>do not forget to write analytics trackers for screens and buttons.</p><p>Now that we’ve identified the reason our code is smelly, let’s see how we can solve this(Spoiler: We’ll be using inheritance and delegations)!</p><p>Let’s start with handling screen analytics first. As we’ve already identified the problem with the current approach is that we’ve to repeat the same analytics tracking code during the creation and destruction phase of every Fragment, why not create a parent class for Fragment(sort of <em>BaseFragment</em>) and write the analytics code there, right? I mean this is pretty eazy peazy!</p><p>But wait, here me out! Although Base classes like <em>BaseFragment</em>, <em>BaseActivity</em> or <em>BaseButton </em>are really helpful for the implementation of common behaviours but they tend to get messy quickly and usually becomes monolithic. And our whole principle of s<em>eperation-of-concerns</em> get’s defeated.</p><p>To tackle this issue, first, let’s discuss a little bit about the delegation pattern.</p><blockquote>Delegation simply means offloading the incoming request to some other place which should actually be responsible for handling.</blockquote><p>And in the words of the Kotlin team,</p><blockquote>The Delegation pattern has proven to be a good alternative to implementation inheritance, and <strong>Kotlin supports it natively requiring zero boilerplate code.</strong></blockquote><p>Now, let&#39;s briefly talk about how Kotlin natively supports delegation.</p><p>In the delegation pattern, we’ve 3 major components, the Requesting object, a Delegator to handle incoming requests and a Handler for handling those requests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*c0xzJTRksfiBkEXbGw0SLQ.jpeg" /></figure><p>Let’s see how this diagram boils down to our own implementation.</p><p>We’ll start with creating an interface for the request handler. This interface will be used to route incoming work requests to the handler, as well as, it’ll also help to pass the necessary data needed for handling the request. In this case, we’ll be passing the LifecycleOwner of the Fragment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7aac3fb569ea29478e4963f3cc008536/href">https://medium.com/media/7aac3fb569ea29478e4963f3cc008536/href</a></iframe><p>Now we’ll create an implementation or handler for the interface which’ll actually be responsible for handling the incoming work.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea48a717c5eeb43bc6ed2db52b10a857/href">https://medium.com/media/ea48a717c5eeb43bc6ed2db52b10a857/href</a></iframe><p>Here, source.toScreenName() is an extension function on LifecycleOwner and its simple job is to convert the unintelligible object names to human-readable screen names.</p><p>For ex, from the entire object name in string , <em>ExploreFragment{com.hitman.android.feature_explore.ExploreFragment@89acb21}</em>, we’re just interested in sending the name<em> ExploreFragment.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf6c8ddbfd2f90ff4e73ae4751d79796/href">https://medium.com/media/cf6c8ddbfd2f90ff4e73ae4751d79796/href</a></iframe><p><em>Pro tip: Do not forget to unit test this function!</em></p><p>Now, let’s move on to what our BaseFragment will look like.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b432ffc041a77dc04fce4e15e5f67ef/href">https://medium.com/media/1b432ffc041a77dc04fce4e15e5f67ef/href</a></iframe><p>Let’s ignore the View binding related stuffs for now and just focus on <strong>Line 6 </strong>and <strong>Line 24</strong>.</p><p>In Line 6 we’ve</p><blockquote>ScreenAnalyticsHandler by ScreenAnalyticsHandlerImpl()</blockquote><p>Notice the <strong>by </strong>keyword. This is how Kotlin supports the delegation pattern natively. The <em>by </em>clause tells the ScreenAnalyticsHandler interface how it’s method should be implemented, in this case, those methods should possess behaviour as specified in ScreenAnalyticsHandlerImpl class. At the compilation phase, the compiler will generate all the methods of ScreenAnalyticsHandler which routes to ScreenAnalyticsHandlerImpl.</p><p>In Line 24 we’ve,</p><blockquote>registerLifecycleOwner(this)</blockquote><p>This is precisely where we’re telling our ScreenAnalyticsHandler interface about the LifecycleOwner of the fragment that was created. And notice, how we didn’t have to do anything else? It is now the job of ScreenAnalyticsHandler to route this request to ScreenAnalyticsHandlerImpl which will do all the analytics stuff. We just offloaded all the analytics and LifecycleEventObserver related things to the handler and we have made our code a little bit cleaner as well as we’ve also achieved <em>seperation-of-concerns </em>by separating analytics logic from our BaseFragment to ScreenAnalyticsHandlerImpl.</p><p>Now if we’ve to create a new screen or Fragment, we just have to do something like this,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/909e8abbb849506f4b43cb18f702ee3c/href">https://medium.com/media/909e8abbb849506f4b43cb18f702ee3c/href</a></iframe><p>That’s all. Nothing else. We don’t have to worry about manually writing the analytics code whenever we create a new Fragment.</p><p>If you’re still here, my suggestion would be to take the implementation of button click analytics as an exercise, it’ll be quite similar to what we’ve already done :P</p><p>Nevertheless, Let’s quickly see the code part of the implementation,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/396356ec1bdef276732a42f986d0be82/href">https://medium.com/media/396356ec1bdef276732a42f986d0be82/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/746b9bb003039768802b9e3107b97adc/href">https://medium.com/media/746b9bb003039768802b9e3107b97adc/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ecc8be1a21011cc220cd5a04b4de3b43/href">https://medium.com/media/ecc8be1a21011cc220cd5a04b4de3b43/href</a></iframe><p>I really hope you learnt something new today! Also, this is my first article on medium so I’m open to any feedback you might have (:</p><p>Have a nice day!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42270d08e2af" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>