Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Analytics in an Aspect Oriented manner

Analytics in an Aspect Oriented manner

This presentation describes how we can use Aspect Oriented Programming to enhance SRP of SOLID design principles. The presentation shows how it is used in Android development.

This has been presented in an internal weekly engineering meeting at ezhome

Spiros Economakis

April 19, 2017
Tweet

More Decks by Spiros Economakis

Other Decks in Technology

Transcript

  1. Why analytics are so important? • Help to build an

    efficient marketing strategy • Discover which parts of our apps people are using, and which parts they aren’t. • Learn which parts of our apps drive valuable conversions. • See if people actually use the app (retention) more……..
  2. Tracking Complex Case - Hide public void successAuthentication(final UserViewModel model)

    { Map<String, Object> trackingData = new HashMap<>(); trackingData.put("id", model.id); trackingData.put("email", model.email); //more tracking data CustomTracker.trackEvent("loginSuccessEvent", trackingData); }
  3. Tracking Simple Case - Composition private final AuthTrackingDelegate trackingDelegate =

    new AuthTrackingDelegate(this) @OnClick(R.id.authenticationSignInBtn) public void onSignInClicked() { this.trackingDelegate.onSignInClicked(); }
  4. Tracking Complex Case - Composition private final AuthTrackingDelegate trackingDelegate =

    new AuthTrackingDelegate(this) public void successAuthentication(final UserViewModel model) { this.trackingDelegate.successAuthentication(“loginSuccessEvent”, model.getTrackingData()); }
  5. Tracking is a Cross-Cutting Concern The concerns representing functionalities for

    secondary requirements are referred to as crosscutting concerns or system-wide concerns. OR It is a concern which is applicable throughout the application and it affects the entire application.
  6. Cross-Cutting Concern - Examples • Tracking analytics • Logging •

    Persistence • Performance monitoring • Data Validation • Caching etc…..
  7. AOP definition Aspect-oriented programming entails breaking down program logic into

    "concerns" (cohesive areas of functionality). This means, that with AOP, we can add executable blocks to some source code without explicitly changing it. This programming paradigm pretends that “cross-cutting concerns” should be implemented once and injected it many times into those places. NOTE: AOP is not something new, exists by 2001 and it’s wide spreaded in many languages
  8. AOP terminology (1) Join point: Join points are points in

    the execution of the system, such as method calls or method entry Pointcut: An expression which tells a code injection tool where to inject a particular piece of code eg. to which join points to apply a particular advice. Advice: The code that is injected to a class file. Typically we talk about before, after, and around advices, which are executed before, after, or instead of a target method.
  9. AOP terminology (2) Aspect: The combination of the pointcut and

    the advice is termed an aspect. For instance, we add a tracking aspect to our application by defining a pointcut and giving the correct advice. Weaving: The process of injecting code – advices – into the target places – join points.
  10. Pointcuts with Annotations Events • @TrackEvent - constructor & method

    • @TrackEventAttribute - method params only • @TrackStaticAttribute - method params only Session • @TrackSession - constructor & method • @TrackSessionAttribute - method params only
  11. Aspect @Aspect public class TrackEventAspect { /** * Pointcut for

    methods */ @Pointcut("execution(@com.ezhome.appcustomer.core.tracking.annotation.TrackEvent * *(..))") public void methodAnnotatedWithTrackEvent() { } /** * Pointcut for constructors */ @Pointcut("execution(@com.ezhome.appcustomer.core.tracking.annotation.TrackEvent *.new(..))") public void constructorAnnotatedTrackEvent() { } @Around("methodAnnotatedWithTrackEvent() || constructorAnnotatedTrackEvent()") public Object weaveJoinPointTrackEvent(ProceedingJoinPoint joinPoint) throws Throwable { //injecting code – advices – into the joint points } }
  12. Advice for Pointcuts @Aspect public class TrackEventAspect { /** *

    Pointcut for methods */ @Pointcut("execution(@com.ezhome.appcustomer.core.tracking.annotation.TrackEvent * *(..))") public void methodAnnotatedWithTrackEvent() { } /** * Pointcut for constructors */ @Pointcut("execution(@com.ezhome.appcustomer.core.tracking.annotation.TrackEvent *.new(..))") public void constructorAnnotatedTrackEvent() { } @Around("methodAnnotatedWithTrackEvent() || constructorAnnotatedTrackEvent()") public Object weaveJoinPointTrackEvent(ProceedingJoinPoint joinPoint) throws Throwable { //injecting code – advices – into the joint points } }
  13. Advice for Pointcuts @Aspect public class TrackEventAspect { /** *

    Pointcut for methods */ @Pointcut("execution(@com.ezhome.appcustomer.core.tracking.annotation.TrackEvent * *(..))") public void methodAnnotatedWithTrackEvent() { } /** * Pointcut for constructors */ @Pointcut("execution(@com.ezhome.appcustomer.core.tracking.annotation.TrackEvent *.new(..))") public void constructorAnnotatedTrackEvent() { } @Around("methodAnnotatedWithTrackEvent() || constructorAnnotatedTrackEvent()") public Object weaveJoinPointTrackEvent(ProceedingJoinPoint joinPoint) throws Throwable { //injecting code – advices – into the joint points } }
  14. Tracking Simple Case - AOP @OnClick(R.id.authenticationSignInBtn) void onSignInClicked() { Segment.trackEvent("loginEvent");

    KeenIO.trackEvent("loginEvent"); GoogleAnalytics.trackEvent("loginEvent"); ……. }
  15. Tracking Simple Case - AOP @TrackEvent("loginEvent") @OnClick(R.id.authenticationSignInBtn) public void onSignInClicked()

    { } @OnClick(R.id.authenticationSignInBtn) void onSignInClicked() { Segment.trackEvent("loginEvent"); KeenIO.trackEvent("loginEvent"); GoogleAnalytics.trackEvent("loginEvent"); ……. }
  16. Tracking Complex Case - AOP public void successAuth(final UserViewModel model)

    { Map<String, Object> trackingData = new HashMap<>(); trackingData.put("id", model.id); trackingData.put("email", model.email); //more tracking data CustomTracker.trackEvent("loginSuccessEvent", trackingData); }
  17. Tracking Complex Case - AOP @TrackEvent("loginSuccessEvent") void successAuth(@TrackEventAttribute final UserViewModel

    model) { } public void successAuth(final UserViewModel model) { Map<String, Object> trackingData = new HashMap<>(); trackingData.put("id", model.id); trackingData.put("email", model.email); //more tracking data CustomTracker.trackEvent("loginSuccessEvent", trackingData); }
  18. Polyglot AOP AOP within the language, or as an external

    library: • Python • Haskell • Ruby • Swift • Delphi • Lua etc…...
  19. Conclusion • Enhance SRP of the SOLID principles • Clean,

    readable and reusable code • Increases modularity • Testable code • Build or load time code injection. Source-code weaving