Skip to content

Instantly share code, notes, and snippets.

@hito-asa
Created April 18, 2013 08:48
Show Gist options
  • Save hito-asa/5411250 to your computer and use it in GitHub Desktop.
Save hito-asa/5411250 to your computer and use it in GitHub Desktop.
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
/**
* Google Analyticsに情報を記録するためのキュークラス
* @author h.asai
*/
public class AnalyticsQueue implements Serializable {
private static final long serialVersionUID = -4480220275278177191L;
private String customUrl = null;
private List<AnalyticsEvent> events = new ArrayList<AnalyticsEvent>(1);
private List<AnalyticsCustomVar> customVars = new ArrayList<AnalyticsCustomVar>(1);
private boolean suppressLogging = false;
/**
* 指定されたAnalyticsQueueを取り込みます
* @param source 取り込み元のAnalyticsQueue
*/
public void load(AnalyticsQueue source) {
if (source != null) {
this.customUrl = source.customUrl;
this.events.addAll(source.getEvents());
this.customVars.addAll(source.getCustomVars());
}
}
/**
* Google Analyticsに記録するためのイベントを追加します。<br/>
* (サーバーサイドの挙動のため、直帰率に影響を与えないように記録します)
* @param category イベントのカテゴリ
* @param action イベントのアクション
* @param label イベントのラベル
* @param value イベントの値
*/
public void addEvent(String category, String action, String label, int value) {
this.getEvents().add(new AnalyticsEvent(category, action, label, value));
}
/**
* Google Analyticsに記録するためのカスタム変数を追加します。
* @param index インデックス値
* @param name 名前
* @param value 値
* @param scope スコープ
*/
public void addCustomVar(int index, String name, String value, AnalyticsCustomVar.Scope scope) {
AnalyticsCustomVar var = new AnalyticsCustomVar(index, name, value, scope);
this.getCustomVars().add(var);
}
/**
* カスタムURLが設定されているかどうかを確認します。
* @return カスタムURLが設定されている場合はtrue
*/
public boolean hasCustomUrl() {
return StringUtils.isNotBlank(getCustomUrl());
}
public String getCustomUrl() {
return customUrl;
}
/**
* カスタムURLを設定する(すでに設定されている場合は上書きせずに無視します)
*/
public void setCustomUrl(String customUrl) {
if (StringUtils.isBlank(this.customUrl))
this.customUrl = customUrl;
}
/**
* カスタムURLを設定する(すでに設定されている場合も上書きします)
*/
public void overrideCustomUrl(String customUrl) {
this.customUrl = customUrl;
}
public List<AnalyticsEvent> getEvents() {
return events;
}
public List<AnalyticsCustomVar> getCustomVars() {
return customVars;
}
public boolean isSuppressLogging() {
return suppressLogging;
}
public void setSuppressLogging(boolean suppressLogging) {
this.suppressLogging = suppressLogging;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment