Created
January 31, 2017 13:50
-
-
Save takuaraki/eb51407ab3598a4200fc6e359ba65647 to your computer and use it in GitHub Desktop.
AnalyticsEventBuilder(Firebase)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.taku; | |
import android.os.Bundle; | |
import com.google.firebase.analytics.FirebaseAnalytics; | |
import javax.inject.Inject; | |
/** | |
* Builder of analytics event. | |
*/ | |
public class AnalyticsEventBuilder { | |
FirebaseAnalytics firebaseAnalytics; | |
private Event event; | |
private Bundle bundle; | |
@Inject | |
public AnalyticsEventBuilder(FirebaseAnalytics firebaseAnalytics) { | |
this.firebaseAnalytics = firebaseAnalytics; | |
bundle = new Bundle(); | |
} | |
/** | |
* Set event. | |
* | |
* @param event {@link Event} | |
* @return {@link AnalyticsEventBuilder} | |
*/ | |
public AnalyticsEventBuilder event(Event event) { | |
this.event = event; | |
return this; | |
} | |
/** | |
* Put parameter and value. | |
* | |
* @param parameter | |
* @param value | |
* @return {@link AnalyticsEventBuilder} | |
*/ | |
public AnalyticsEventBuilder putParameter(Parameter parameter, Value value) { | |
bundle.putString(parameter.name(), value.getValue()); | |
return this; | |
} | |
/** | |
* Send event. | |
* | |
* @throws IllegalStateException No event is set. | |
*/ | |
public void send() { | |
if (event == null) { | |
throw new IllegalStateException("No event is set."); | |
} | |
firebaseAnalytics.logEvent(event.name(), bundle); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.taku; | |
/** | |
* Analytics event. | |
*/ | |
public enum Event { | |
DISPLAY, | |
TAP | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.taku; | |
/** | |
* Analytics event parameter. | |
*/ | |
public enum Parameter { | |
SCREEN | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.taku; | |
/** | |
* Value of analytics event parameter. | |
*/ | |
public enum Value { | |
MAIN, | |
INPUT, | |
SAVE_BUTTON; | |
public String getValue() { | |
return name().toLowerCase(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment