@Override public void onAspectSuperAttributeAdded(String key, Object value) { addSuperAttribute(key, value); }
@Override public void onAspectSuperAttributeRemoved(String key) { removeSuperAttribute(key); } }
public void trackEvent(String eventName) { trackEvent(new Event(eventName, null, null, null, superAttributes)); }
@Test public void removeSuperAttributes() { tracklytics.addSuperAttribute("key1", "value1"); tracklytics.addSuperAttribute("key2", "value2"); tracklytics.addSuperAttribute("key3", "value3"); tracklytics.removeSuperAttribute("key1"); tracklytics.removeSuperAttribute("key2"); Map<String, Object> attributes = new HashMap<>(); attributes.put("key4", "value4"); tracklytics.trackEvent("event_name", attributes); verify(eventSubscriber).onEventTracked(eventCaptor.capture()); assertThat(eventCaptor.getValue().name).isEqualTo("event_name"); assertThat(eventCaptor.getValue().attributes).containsExactly("key4", "value4"); assertThat(eventCaptor.getValue().superAttributes).containsExactly("key3", "value3"); }
@TrackEvent("on_create") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Tracklytics.init(new EventSubscriber() { @Override public void onEventTracked(Event event) { // Send your events to Mixpanel, Fabric etc } }).setEventLogListener(new EventLogListener() { @Override public void log(String message) { // Set your logger here. ie: Logger or Timber Log.d("Tracker", message); } }); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @TrackEvent("button_click") @FixedAttribute(key = "button_name", value = "Login") @Override public void onClick(View v) { } }); }
@Test public void removeSuperAttributeFromAspects() { tracklytics.addSuperAttribute("key1", "value1"); tracklytics.addSuperAttribute("key2", "value2"); tracklytics.onAspectSuperAttributeRemoved("key1"); tracklytics.trackEvent("event_name"); verify(eventSubscriber).onEventTracked(eventCaptor.capture()); assertThat(eventCaptor.getValue().name).isEqualTo("event_name"); assertThat(eventCaptor.getValue().superAttributes).containsExactly("key2", "value2"); }
@Test public void log() throws Throwable { EventLogListener logger = mock(EventLogListener.class); tracklytics.setEventLogListener(logger); Map<String, Object> attributes = new HashMap<>(); attributes.put("key", "value"); tracklytics.trackEvent("event", attributes); verify(logger).log("event-> {key=value}, super attrs: {}, filters: null"); } }
@Test public void addSuperAttributesToEvent() { tracklytics.addSuperAttribute("key1", "value1"); tracklytics.addSuperAttribute("key2", "value2"); Map<String, Object> attributes = new HashMap<>(); attributes.put("key3", "value3"); tracklytics.trackEvent("event_name", attributes); verify(eventSubscriber).onEventTracked(eventCaptor.capture()); assertThat(eventCaptor.getValue().name).isEqualTo("event_name"); assertThat(eventCaptor.getValue().attributes).containsExactly("key3", "value3"); assertThat(eventCaptor.getValue().superAttributes).containsExactly("key1", "value1", "key2", "value2"); }
@Before public void setup() { Tracklytics.init(new EventSubscriber() { @Override public void onEventTracked(Event event) { triggeredEvents.put(event.name, event); } }); }
@Test public void addSuperAttributeFromAspects() { tracklytics.onAspectSuperAttributeAdded("key1", "value1"); tracklytics.trackEvent("event_name"); verify(eventSubscriber).onEventTracked(eventCaptor.capture()); assertThat(eventCaptor.getValue().name).isEqualTo("event_name"); assertThat(eventCaptor.getValue().superAttributes).containsExactly("key1", "value1"); }
public static Tracklytics init(EventSubscriber eventSubscriber) { Tracklytics tracklytics = new Tracklytics(eventSubscriber); TracklyticsAspect.subscribe(tracklytics); return tracklytics; }
private void trackEvent(Event event) { eventSubscriber.onEventTracked(event); log(event); }
@Test public void trackFromAspectEvent() throws Throwable { class Foo { @TrackEvent("event_name") public void foo() { } } TrackEvent trackEvent = Foo.class.getMethod("foo").getAnnotation(TrackEvent.class); Map<String, Object> attributes = new HashMap<>(); attributes.put("key", "value"); tracklytics.onAspectEventTriggered(trackEvent, attributes); verify(eventSubscriber).onEventTracked(eventCaptor.capture()); assertThat(eventCaptor.getValue().name).isEqualTo("event_name"); assertThat(eventCaptor.getValue().attributes).containsExactly("key", "value"); }
@Before public void setup() { initMocks(this); tracklytics = Tracklytics.init(eventSubscriber); when(eventSubscriber.toString()).thenReturn("Tracklytics"); when(trackEvent.value()).thenReturn("event"); when(trackEvent.filters()).thenReturn(new int[]{1, 2}); }
@Override public void onAspectEventTriggered(TrackEvent trackEvent, Map<String, Object> attributes) { trackEvent(new Event(trackEvent, attributes, superAttributes)); }
public void trackEvent(String eventName, Map<String, Object> attributes) { trackEvent(new Event(eventName, null, null, attributes, superAttributes)); }
@Test public void trackWithoutAnnotation() { Map<String, Object> attributes = new HashMap<>(); attributes.put("key", "value"); tracklytics.trackEvent("event_name", attributes); verify(eventSubscriber).onEventTracked(eventCaptor.capture()); assertThat(eventCaptor.getValue().name).isEqualTo("event_name"); assertThat(eventCaptor.getValue().attributes).containsExactly("key", "value"); }
@Test public void trackWithEvent() { tracklytics.trackEvent("event_name"); verify(eventSubscriber).onEventTracked(eventCaptor.capture()); assertThat(eventCaptor.getValue().name).isEqualTo("event_name"); assertThat(eventCaptor.getValue().attributes).isNull(); }