Effective application analytic

  • Provide api layer between usage of analytics and sending it. Thus, we will able to write tests for it, and check does our analytics api correct or not. Also we will have all analytics in one place.
  • Provide tiny api layer. Thus, the working time and object creation excess will be minimal. Also, we will be able to create analytics double-quick.
  • Provide OOP way to work with analytics. Thus, we will able to mock analytics in test classes, and write appropriate tests with not disturbing analytics system.

Actions with single label

As it appeared by using GAnalytics in real project, there are cases, when you have some event in analytics with category, action and only one label. So in that case you will need to provide a way to describe that label through arguments of action method. It will look something like:

interface CategoryInterface {
fun action(label: Label)
enum class Label { MY_LABEL_NAME }
}
interface CategoryInterface {
@LabelFun(“action”) fun myLabelName()
}
interface CategoryInterface {
fun show(label: PriceCategory)
@LabelFun(“show”, “all_prices”) showAll()
}

interface CategoryInterface {
@LabelFun(“show”, “low_prices”) fun showLow()
@LabelFun(“show”, “medium_prices”) fun showMedium()
@LabelFun(“show”, “high_prices”) fun showHigh()
@LabelFun(“show”, “all_prices”) fun showAll()
}

Postfixes

In GAnalytics v1.0 prefixes were added. Now it is time to introduce postfixes. They looks very similar to prefixes except that they do not use some hidden logic for the case when you use single annotation without parameters. Moreover, that code will not compile:

//example of using @HasPrefix without arguments
interface CategoryInterface {
@HasPrefix fun action()
}
//will produce action = "categoryinterfaceaction"
//example of using @HasPostfix without arguments
interface CategoryInterface {
@HasPostfix fun action()
}
will not compile, since @HasPostfix require to define a name of postfix
interface AnalyticsRange {
fun fromOd(value: Int)
fun toOd(value: Int)
}
@HasPostfix("Od")
interface AnalyticsRange {
fun from(value: Int)
fun to(value: Int)
}

Support of properties in Group Interfaces

If earlier we were able to write only methods without parameters in group interfaces, then from version 1.1 the support of val properties was added. Now you can declare category interfaces in group interface not in fun but also in val.

interface GroupInterface {
fun category(): AnalyticsCategory
fun otherCategory(): AnalyticsOtherCategory
}
interface AnalyticsCategory {
fun action()
}
interface AnalyticsOtherCategory {
fun action()
}
//invocation part
groupInterface.category().action()
groupInterface.otherCategory().action()
interface GroupInterface {
val category: AnalyticsCategory
val otherCategory: AnalyticsOtherCategory
}
interface AnalyticsCategory {
fun action()
}
interface AnalyticsOtherCategory {
fun action()
}
//invocation part
groupInterface.category.action()
groupInterface.otherCategory.action()

Optimized memory consuming

Previously, when you had group interface with category interfaces in your project and you perform analytics sending from them (like analytics.seller().sell("ps4")), then each time you called method from group interface, new special wrapper for category interfaces was created. Moreover, each time new proxy object for category interface was created, which is not so good, especially in terms of memory consuming in android apps.

interface Analytics {
val seller: AnalyticsSeller
val customer: AnalyticsCustomer
}
interface AnalyticsSeller {
fun sell(what: String)
}
interface AnalyticsCustomer {
fun buy(what: String)
}
analytics.seller.sell("something")
analytics.customer.buy("something")

That’s all?

Almost yes. :) One small thing is supporting of hashCode, toString and equals. I understand that I probably did it a little bit hacky, but for now, if you will use debugging tools, you will see nicely explanation for Analytics interface instances (since toString is provided). Also, you be able to compare analytic interfaces, if you for some reason need it.

Afterwords

Now, that’s all. Enjoy the new version of GAnalytics and if you have any questions/proposals or remarks, please, fill free to share it with me.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store