Skip to content

Commit

Permalink
Adds custom event handling to Piwik Tracker.
Browse files Browse the repository at this point in the history
Change-Id: I038be238c17f7e137a7d39c911100d99a1f3ec7e
  • Loading branch information
Christian Petrov authored and hstaudacher committed Jun 10, 2014
1 parent 62f45de commit 1f11be1
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 ) ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.junit.Test;



public class RequestKeyProviderTest {

@Test
Expand Down Expand Up @@ -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" ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Action> actionCaptor = ArgumentCaptor.forClass( Action.class );
ArgumentCaptor<AdvancedConfiguration> configCaptor = ArgumentCaptor.forClass( AdvancedConfiguration.class );
ArgumentCaptor<VisitorInformation> 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 ) ) );
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 1f11be1

Please sign in to comment.