Declarative analytics: gAnalytics

About Analytics in Apps

ServiceLocator.getInstance().getAnalytics().track(CATEGORY, "some_string");
public static void track(String action) {
ServiceLocator.getInstance().getAnalytics().track(CATEGORY, action);
}
public static void track(String action, String label) {
analytics.track(CATEGORY, action, label);
}
public static void track(String category, String action, String label) {
tracker.send(HitBuilders.EventBuilder()
.setCategory(category)
.setAction(action)
.setLabel(label)
.build());
}

Declarative solution (Proxy approach)

@HasPrefix(splitter = "_")
interface AnalyticsCandyShop {
fun near()
fun opened()
fun closed()
fun sell()
}
interface AnalyticsWrapper {
fun <T : Any> create(kClass: KClass<T>) = create(kClass.java)
fun <T : Any> create(clazz: Class<T>): T
}

class AnalyticsSingleWrapper : AnalyticsWrapper {
@Suppress("unchecked_cast")
override fun <T : Any> create(clazz: Class<T>): T {
return Proxy.newProxyInstance(clazz.classLoader, arrayOf<Class<*>>(clazz)) { proxy, method, args ->
//do processing annotations, making conversions
//or
//some other work !HERE!
} as T
}
}
interface EventProvider {
fun provide(event: Event)
}
class AnalyticsSingleWrapper(private val eventProvider: EventProvider) : AnalyticsWrapper {
@Suppress("unchecked_cast")
override fun <T : Any> create(clazz: Class<T>): T {
return Proxy.newProxyInstance(clazz.classLoader, arrayOf<Class<*>>(clazz)) { proxy, method, args ->
//build here category, action, label and value
val event = Event(category, action, label, value)
eventProvider.provide(event)
} as T
}

Annotations

@Category (Scope: Class)

@Action (Scope: Method)

@Label (Scope: Parameter)

@HasPrefix (Scope: Class/Method)

@HasPrefix
interface CandyShop() {
fun opened()
}
@HasPrefix(name = "cs", splitter = "_-_")
interface CandyShop() {
fun opened()
}

@NoPrefix (Scope: Class/Method)

@HasPrefix
interface SomeCategory {
fun action1()
@NoPrefix fun action2()
}

Default behaviour

Future

Conclusion

--

--

--

Love being creative to solve some problems with an simple and elegant ways

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

AWS Infrastructure As Code/ Practice with AWS CloudWatch, SNS & CloudFormation 2

Automating tedious GitHub tasks with Serverless (part 1)

How to use post processing in Unity

Getting Started in Testing Automation

Tutorial — bCFX & FOR Combo Mining

Let's talk about Mongolia — mongo db as a rest API

How to organize your theme

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
Michael Spitsin

Michael Spitsin

Love being creative to solve some problems with an simple and elegant ways

More from Medium

AWS SDK 1.x to 2.x migration and upgrade — learnings and takeaways

Test case failed only when maven build

Spring Events at VISO

Spring cloud: introduction to service discovery using Netflix eureka