Skip to content
This repository has been archived by the owner on Jan 11, 2021. It is now read-only.

Commit

Permalink
Merge pull request #32 from alexstyl/analytics_improvement
Browse files Browse the repository at this point in the history
Use only alphanumeric characters for Firebase event names
  • Loading branch information
alexstyl authored Sep 7, 2016
2 parents 7a67987 + 6db1fcb commit 4f90baf
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.alexstyl.specialdates.analytics;

public enum Action {
ADD_BIRTHDAY("add birthday"),
DAILY_REMINDER("enable daily reminder"),
ADD_BIRTHDAY("add_bday"),
DAILY_REMINDER("reminder"),
DONATION("donate"),
INTERACT_CONTACT("interact contact"),
SELECT_THEME("select theme"),
GO_TO_TODAY("go to today");
INTERACT_CONTACT("contact"),
SELECT_THEME("theme"),
GO_TO_TODAY("gotoday");

private final String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ public String getValue() {
return value;
}

@Override
public String toString() {
return "ActionWithParameters{" +
"actionName=" + actionName +
", label='" + label + '\'' +
", value='" + value + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void trackScreen(Screen screen) {
Log.d("Tracking screen_view:" + screen);
}

private String format(ActionWithParameters action) {
return String.format(Locale.US, "%s:%s:%s", action.getName(), action.getLabel(), action.getValue());
static String format(ActionWithParameters action) {
return String.format(Locale.US, "%s_%s_%s", action.getName(), action.getLabel(), action.getValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.alexstyl.specialdates.analytics;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static junit.framework.Assert.fail;

/**
* Class that tests our business logic against all Firebase documentation rules
*
* @see <a href="https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.Event">FirebaseAnalytics.Event documentation</a>
*/
@RunWith(MockitoJUnitRunner.class)
public class FirebaseRulesTest {

private final static int MAX_EVENT_NAME_CHAR_COUNT = 32;
private final static int MAX_EVENTS_COUNT = 500;

@Mock
private Analytics mockAnalytics;

@Test
public void actionUpTo32characterlong() {
for (Action action : Action.values()) {
if (action.getName().length() > MAX_EVENT_NAME_CHAR_COUNT) {
failFor(action);
}
}
}

@Test
public void actionWithParamsUpTo32CharacterLong() {
List<String> labels = Arrays.asList("enabled", "select", "success", "source");
List<String> values = Arrays.asList("true", "false", "success", "external", "sms", "call");

for (Action action : Action.values()) {
for (String label : labels) {
for (String value : values) {
ActionWithParameters actionWithParameters = new ActionWithParameters(action, label, value);
String event = Firebase.format(actionWithParameters);
if (event.length() > MAX_EVENT_NAME_CHAR_COUNT) {
failFor(actionWithParameters);
}

}

}
}
}

@Test
public void doesNotExceedNumberOfAllowedEvents() {
int numberOfEventsUsed = Action.values().length;
if (numberOfEventsUsed > MAX_EVENTS_COUNT) {
fail("You can only use up to " + MAX_EVENTS_COUNT + " unique Firebase events. Currently using " + numberOfEventsUsed);
}

}

private static void failFor(Action action) {
int characterCount = action.getName().length();
fail(action + " is not a correct action to be used. It contains more than " + MAX_EVENT_NAME_CHAR_COUNT + " characters. Has " + characterCount);
}

private static void failFor(ActionWithParameters action) {
int characterCount = Firebase.format(action).length();
String message = action + " is not a correct action to be used. It contains more than " + MAX_EVENT_NAME_CHAR_COUNT + " characters. Has " + characterCount;
fail(message);
}

}

0 comments on commit 4f90baf

Please sign in to comment.