From 1f11be10dae552c6ed6704e3170a7092e13e0e34 Mon Sep 17 00:00:00 2001 From: Christian Petrov Date: Fri, 6 Jun 2014 18:12:58 +0200 Subject: [PATCH] Adds custom event handling to Piwik Tracker. Change-Id: I038be238c17f7e137a7d39c911100d99a1f3ec7e --- .../piwik/model/action/EventActionTest.java | 83 +++++++++++++++++++ .../piwik/request/RequestKeyProviderTest.java | 16 ++++ .../tracking/tracker/PiwikTrackerTest.java | 26 ++++++ .../piwik/model/action/EventAction.java | 45 ++++++++++ .../piwik/request/RequestKeyProvider.java | 3 + .../internal/piwik/request/RequestKeys.java | 3 + .../tabris/tracking/tracker/PiwikTracker.java | 15 ++++ 7 files changed, 191 insertions(+) create mode 100644 com.eclipsesource.tabris.tracking.test/src/com/eclipsesource/tabris/tracking/internal/piwik/model/action/EventActionTest.java create mode 100644 com.eclipsesource.tabris.tracking/src/com/eclipsesource/tabris/tracking/internal/piwik/model/action/EventAction.java diff --git a/com.eclipsesource.tabris.tracking.test/src/com/eclipsesource/tabris/tracking/internal/piwik/model/action/EventActionTest.java b/com.eclipsesource.tabris.tracking.test/src/com/eclipsesource/tabris/tracking/internal/piwik/model/action/EventActionTest.java new file mode 100644 index 0000000..c449eb6 --- /dev/null +++ b/com.eclipsesource.tabris.tracking.test/src/com/eclipsesource/tabris/tracking/internal/piwik/model/action/EventActionTest.java @@ -0,0 +1,83 @@ +/******************************************************************************* + * Copyright (c) 2014 EclipseSource and others. All rights reserved. This + * program and the accompanying materials are made available under the terms of + * the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html Contributors: + * EclipseSource - initial API and implementation + ******************************************************************************/ +package com.eclipsesource.tabris.tracking.internal.piwik.model.action; + +import static com.eclipsesource.tabris.tracking.internal.piwik.request.RequestKeyProvider.getRequestKey; +import static com.eclipsesource.tabris.tracking.internal.piwik.request.RequestKeys.EVENT_ACTION; +import static com.eclipsesource.tabris.tracking.internal.piwik.request.RequestKeys.EVENT_CATEGORY; +import static com.eclipsesource.tabris.tracking.internal.piwik.request.RequestKeys.EVENT_NAME; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + + +public class EventActionTest { + + @Test( expected = IllegalArgumentException.class ) + public void testFailsWithNullCategory() { + EventAction eventAction = new EventAction( "foo" ); + + eventAction.setCategory( null ); + } + + @Test( expected = IllegalArgumentException.class ) + public void testFailsWithEmptyCategory() { + EventAction eventAction = new EventAction( "foo" ); + + eventAction.setCategory( "" ); + } + + @Test + public void testAddsCategoryToParameter() throws Exception { + EventAction eventAction = new EventAction( "foo" ).setCategory( "baz" ); + + assertEquals( "baz", eventAction.getParameter().get( getRequestKey( EVENT_CATEGORY ) ) ); + } + + @Test( expected = IllegalArgumentException.class ) + public void testFailsWithNullAction() { + EventAction eventAction = new EventAction( "foo" ); + + eventAction.setAction( null ); + } + + @Test( expected = IllegalArgumentException.class ) + public void testFailsWithEmptyAction() { + EventAction eventAction = new EventAction( "foo" ); + + eventAction.setAction( "" ); + } + + @Test + public void testAddsActionToParameter() throws Exception { + EventAction eventAction = new EventAction( "foo" ).setAction( "baz" ); + + assertEquals( "baz", eventAction.getParameter().get( getRequestKey( EVENT_ACTION ) ) ); + } + + @Test( expected = IllegalArgumentException.class ) + public void testFailsWithNullName() { + EventAction eventAction = new EventAction( "foo" ); + + eventAction.setEventName( null ); + } + + @Test( expected = IllegalArgumentException.class ) + public void testFailsWithEmptyName() { + EventAction eventAction = new EventAction( "foo" ); + + eventAction.setEventName( "" ); + } + + @Test + public void testAddsNameToParameter() throws Exception { + EventAction eventAction = new EventAction( "foo" ).setEventName( "baz" ); + + assertEquals( "baz", eventAction.getParameter().get( getRequestKey( EVENT_NAME ) ) ); + } +} diff --git a/com.eclipsesource.tabris.tracking.test/src/com/eclipsesource/tabris/tracking/internal/piwik/request/RequestKeyProviderTest.java b/com.eclipsesource.tabris.tracking.test/src/com/eclipsesource/tabris/tracking/internal/piwik/request/RequestKeyProviderTest.java index bb23aa0..92db79a 100644 --- a/com.eclipsesource.tabris.tracking.test/src/com/eclipsesource/tabris/tracking/internal/piwik/request/RequestKeyProviderTest.java +++ b/com.eclipsesource.tabris.tracking.test/src/com/eclipsesource/tabris/tracking/internal/piwik/request/RequestKeyProviderTest.java @@ -13,6 +13,7 @@ import org.junit.Test; + public class RequestKeyProviderTest { @Test @@ -191,6 +192,21 @@ public void testStoresEcommerceItems() { assertEquals( "ec_items", requestKeys.get( "ECOMMERCE_ORDER_ITEMS" ) ); } + @Test + public void testStoresEventCategory() { + assertEquals( "e_c", requestKeys.get( "EVENT_CATEGORY" ) ); + } + + @Test + public void testStoresEventAction() { + assertEquals( "e_a", requestKeys.get( "EVENT_ACTION" ) ); + } + + @Test + public void testStoresEventName() { + assertEquals( "e_n", requestKeys.get( "EVENT_NAME" ) ); + } + @Test public void testStoresTokenAuth() { assertEquals( "token_auth", requestKeys.get( "TOKEN_AUTH" ) ); diff --git a/com.eclipsesource.tabris.tracking.test/src/com/eclipsesource/tabris/tracking/tracker/PiwikTrackerTest.java b/com.eclipsesource.tabris.tracking.test/src/com/eclipsesource/tabris/tracking/tracker/PiwikTrackerTest.java index f11c9ec..9d85c6c 100644 --- a/com.eclipsesource.tabris.tracking.test/src/com/eclipsesource/tabris/tracking/tracker/PiwikTrackerTest.java +++ b/com.eclipsesource.tabris.tracking.test/src/com/eclipsesource/tabris/tracking/tracker/PiwikTrackerTest.java @@ -8,6 +8,8 @@ package com.eclipsesource.tabris.tracking.tracker; import static com.eclipsesource.tabris.tracking.internal.piwik.request.RequestKeyProvider.getRequestKey; +import static com.eclipsesource.tabris.tracking.tracker.PiwikTracker.ACTION_EVENT; +import static com.eclipsesource.tabris.tracking.tracker.PiwikTracker.CATEGORY_EVENT; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -142,6 +144,30 @@ public void testSendsEcommerceAction() { actionCaptor.getValue().getParameter().get( getRequestKey( RequestKeys.ECOMMERCE_ORDER_TOTAL ) ) ); } + @Test + public void testSendsEventAction() { + Piwik piwik = mock( Piwik.class ); + PiwikTracker tracker = new PiwikTracker( piwik, fakeTokenAuth ); + TrackingEvent event = new TrackingEvent( EventType.EVENT, createInfo(), "foo", 1 ); + + tracker.handleEvent( event ); + + ArgumentCaptor actionCaptor = ArgumentCaptor.forClass( Action.class ); + ArgumentCaptor configCaptor = ArgumentCaptor.forClass( AdvancedConfiguration.class ); + ArgumentCaptor visitorCaptor = ArgumentCaptor.forClass( VisitorInformation.class ); + verify( piwik ).track( actionCaptor.capture(), visitorCaptor.capture(), configCaptor.capture() ); + assertAdvancedConfiguration( configCaptor.getValue() ); + assertVisitorInformation( visitorCaptor.getValue() ); + assertEquals( "http://appId/action/event/foo", + actionCaptor.getValue().getParameter().get( getRequestKey( RequestKeys.ACTION_URL ) ) ); + assertEquals( CATEGORY_EVENT, + actionCaptor.getValue().getParameter().get( getRequestKey( RequestKeys.EVENT_CATEGORY ) ) ); + assertEquals( ACTION_EVENT, + actionCaptor.getValue().getParameter().get( getRequestKey( RequestKeys.EVENT_ACTION ) ) ); + assertEquals( "foo", + actionCaptor.getValue().getParameter().get( getRequestKey( RequestKeys.EVENT_NAME ) ) ); + } + private void assertVisitorInformation( VisitorInformation visitorInformation ) { assertEquals( "100x200", visitorInformation.getParameter().get( getRequestKey( RequestKeys.VISITOR_RESOLUTION ) ) ); assertEquals( "clientId", visitorInformation.getParameter().get( getRequestKey( RequestKeys.VISITOR_ID ) ) ); diff --git a/com.eclipsesource.tabris.tracking/src/com/eclipsesource/tabris/tracking/internal/piwik/model/action/EventAction.java b/com.eclipsesource.tabris.tracking/src/com/eclipsesource/tabris/tracking/internal/piwik/model/action/EventAction.java new file mode 100644 index 0000000..fafd639 --- /dev/null +++ b/com.eclipsesource.tabris.tracking/src/com/eclipsesource/tabris/tracking/internal/piwik/model/action/EventAction.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2014 EclipseSource and others. All rights reserved. This + * program and the accompanying materials are made available under the terms of + * the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html Contributors: + * EclipseSource - initial API and implementation + ******************************************************************************/ +package com.eclipsesource.tabris.tracking.internal.piwik.model.action; + +import static com.eclipsesource.tabris.internal.Clauses.when; +import static com.eclipsesource.tabris.internal.Clauses.whenNull; +import static com.eclipsesource.tabris.tracking.internal.piwik.request.RequestKeyProvider.getRequestKey; +import static com.eclipsesource.tabris.tracking.internal.piwik.request.RequestKeys.EVENT_ACTION; +import static com.eclipsesource.tabris.tracking.internal.piwik.request.RequestKeys.EVENT_CATEGORY; +import static com.eclipsesource.tabris.tracking.internal.piwik.request.RequestKeys.EVENT_NAME; + + +@SuppressWarnings("restriction") +public class EventAction extends Action { + + public EventAction( String actionUrl ) { + super( actionUrl ); + } + + public EventAction setCategory( String category ) { + whenNull( category ).throwIllegalArgument( "Category must not be null." ); + when( category.isEmpty() ).throwIllegalArgument( "Category must not be empty." ); + addParameter( getRequestKey( EVENT_CATEGORY ), category ); + return this; + } + + public EventAction setAction( String action ) { + whenNull( action ).throwIllegalArgument( "Action must not be null." ); + when( action.isEmpty() ).throwIllegalArgument( "Action must not be empty." ); + addParameter( getRequestKey( EVENT_ACTION ), action ); + return this; + } + + public EventAction setEventName( String name ) { + whenNull( name ).throwIllegalArgument( "Name must not be null." ); + when( name.isEmpty() ).throwIllegalArgument( "Name must not be empty." ); + addParameter( getRequestKey( EVENT_NAME ), name ); + return this; + } +} diff --git a/com.eclipsesource.tabris.tracking/src/com/eclipsesource/tabris/tracking/internal/piwik/request/RequestKeyProvider.java b/com.eclipsesource.tabris.tracking/src/com/eclipsesource/tabris/tracking/internal/piwik/request/RequestKeyProvider.java index 854b44b..892c912 100644 --- a/com.eclipsesource.tabris.tracking/src/com/eclipsesource/tabris/tracking/internal/piwik/request/RequestKeyProvider.java +++ b/com.eclipsesource.tabris.tracking/src/com/eclipsesource/tabris/tracking/internal/piwik/request/RequestKeyProvider.java @@ -52,6 +52,9 @@ public class RequestKeyProvider { mapping.put( RequestKeys.ECOMMERCE_ORDER_SHIPPING, "ec_sh" ); mapping.put( RequestKeys.ECOMMERCE_ORDER_DISCOUNT, "ec_dt" ); mapping.put( RequestKeys.ECOMMERCE_ORDER_ITEMS, "ec_items" ); + mapping.put( RequestKeys.EVENT_CATEGORY, "e_c" ); + mapping.put( RequestKeys.EVENT_ACTION, "e_a" ); + mapping.put( RequestKeys.EVENT_NAME, "e_n" ); mapping.put( RequestKeys.TOKEN_AUTH, "token_auth" ); mapping.put( RequestKeys.VISITOR_IP_OVERRIDE, "cip" ); mapping.put( RequestKeys.VISITOR_DATETIME_OVERRIDE, "cdt" ); diff --git a/com.eclipsesource.tabris.tracking/src/com/eclipsesource/tabris/tracking/internal/piwik/request/RequestKeys.java b/com.eclipsesource.tabris.tracking/src/com/eclipsesource/tabris/tracking/internal/piwik/request/RequestKeys.java index c41dd05..5c9ea05 100644 --- a/com.eclipsesource.tabris.tracking/src/com/eclipsesource/tabris/tracking/internal/piwik/request/RequestKeys.java +++ b/com.eclipsesource.tabris.tracking/src/com/eclipsesource/tabris/tracking/internal/piwik/request/RequestKeys.java @@ -44,6 +44,9 @@ public class RequestKeys { public static final String ECOMMERCE_ORDER_SHIPPING = "ECOMMERCE_ORDER_SHIPPING"; public static final String ECOMMERCE_ORDER_DISCOUNT = "ECOMMERCE_ORDER_DISCOUNT"; public static final String ECOMMERCE_ORDER_ITEMS = "ECOMMERCE_ORDER_ITEMS"; + public static final String EVENT_CATEGORY = "EVENT_CATEGORY"; + public static final String EVENT_ACTION = "EVENT_ACTION"; + public static final String EVENT_NAME = "EVENT_NAME"; public static final String TOKEN_AUTH = "TOKEN_AUTH"; public static final String VISITOR_IP_OVERRIDE = "VISITOR_IP_OVERRIDE"; public static final String VISITOR_DATETIME_OVERRIDE = "VISITOR_DATETIME_OVERRIDE"; diff --git a/com.eclipsesource.tabris.tracking/src/com/eclipsesource/tabris/tracking/tracker/PiwikTracker.java b/com.eclipsesource.tabris.tracking/src/com/eclipsesource/tabris/tracking/tracker/PiwikTracker.java index 870c983..c03349d 100644 --- a/com.eclipsesource.tabris.tracking/src/com/eclipsesource/tabris/tracking/tracker/PiwikTracker.java +++ b/com.eclipsesource.tabris.tracking/src/com/eclipsesource/tabris/tracking/tracker/PiwikTracker.java @@ -26,6 +26,7 @@ import com.eclipsesource.tabris.tracking.internal.piwik.model.VisitorInformation; import com.eclipsesource.tabris.tracking.internal.piwik.model.action.Action; import com.eclipsesource.tabris.tracking.internal.piwik.model.action.EcommerceAction; +import com.eclipsesource.tabris.tracking.internal.piwik.model.action.EventAction; import com.eclipsesource.tabris.tracking.internal.piwik.model.action.SearchAction; import com.eclipsesource.tabris.tracking.internal.piwik.model.ecommerce.EcommerceItem; import com.eclipsesource.tabris.tracking.internal.piwik.model.ecommerce.EcommerceItemsBuilder; @@ -55,6 +56,9 @@ @SuppressWarnings("restriction") public class PiwikTracker implements Tracker { + static final String ACTION_EVENT = "custom"; + static final String CATEGORY_EVENT = "tabris.event"; + private final Piwik piwik; private final String tokenAuth; @@ -97,6 +101,8 @@ private Action createAction( TrackingEvent event ) { action = createSearchAction( event ); } else if( event.getType() == EventType.ORDER ) { action = createEcommerceAction( event ); + } else if( event.getType() == EventType.EVENT ) { + action = createEventAction( event ); } return action; } @@ -157,6 +163,15 @@ private EcommerceItem createEcommerceItem( OrderItem item ) { return ecommerceItem; } + private Action createEventAction( TrackingEvent event ) { + String eventId = ( String )event.getDetail(); + EventAction eventAction = new EventAction( createHost( event ) + "/action/event/" + eventId ); + eventAction.setCategory( CATEGORY_EVENT ); + eventAction.setAction( ACTION_EVENT ); + eventAction.setEventName( eventId ); + return eventAction; + } + private String createHost( TrackingEvent event ) { return "http://" + event.getInfo().getAppId(); }