diff --git a/src/main/java/com/brsanthu/googleanalytics/GoogleAnalytics.java b/src/main/java/com/brsanthu/googleanalytics/GoogleAnalytics.java index a9a2094..dc3e50b 100644 --- a/src/main/java/com/brsanthu/googleanalytics/GoogleAnalytics.java +++ b/src/main/java/com/brsanthu/googleanalytics/GoogleAnalytics.java @@ -24,6 +24,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.concurrent.Callable; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingDeque; @@ -123,7 +124,7 @@ public void setHttpClient(CloseableHttpClient httpClient) { this.httpClient = httpClient; } - @SuppressWarnings({ "rawtypes" }) + @SuppressWarnings({ "rawtypes", "unchecked" }) public GoogleAnalyticsResponse post(GoogleAnalyticsRequest request) { GoogleAnalyticsResponse response = new GoogleAnalyticsResponse(); if (!config.isEnabled()) { @@ -140,11 +141,14 @@ public GoogleAnalyticsResponse post(GoogleAnalyticsRequest request) { processParameters(request, postParms); //Process custom dimensions - processCustomDimensionParameters(request, postParms); + processCustomParameters(request, postParms, defaultRequest.customDimensions, request.customDimensions); //Process custom metrics - processCustomMetricParameters(request, postParms); - + processCustomParameters(request, postParms, defaultRequest.customMetrics, request.customMetrics); + + // Process productParameters + processCustomSubParameters(request, postParms); + logger.debug("Processed all parameters and sending the request " + postParms); HttpPost httpPost = new HttpPost(config.getUrl()); @@ -194,52 +198,124 @@ private void processParameters(GoogleAnalyticsRequest request, List postParms) { - Map customDimParms = new HashMap(); - for (String defaultCustomDimKey : defaultRequest.customDimensions().keySet()) { - customDimParms.put(defaultCustomDimKey, defaultRequest.customDimensions().get(defaultCustomDimKey)); - } + private void processCustomParameters(@SuppressWarnings("rawtypes") GoogleAnalyticsRequest request, List postParms, + Map defaultCustomElements, Map requestCustomElements) { - @SuppressWarnings("unchecked") - Map requestCustomDims = request.customDimensions(); - for (String requestCustomDimKey : requestCustomDims.keySet()) { - customDimParms.put(requestCustomDimKey, requestCustomDims.get(requestCustomDimKey)); - } + Map customParms = new HashMap(); + customParms.putAll(defaultCustomElements); + customParms.putAll(requestCustomElements); - for (String key : customDimParms.keySet()) { - postParms.add(new BasicNameValuePair(key, customDimParms.get(key))); + for (Entry e : customParms.entrySet()) { + postParms.add(new BasicNameValuePair(e.getKey(), e.getValue())); } } - /** - * Processes the custom metrics and adds the values to list of parameters, which would be posted to GA. - * - * @param request - * @param postParms - */ - private void processCustomMetricParameters(@SuppressWarnings("rawtypes") GoogleAnalyticsRequest request, List postParms) { - Map customMetricParms = new HashMap(); - for (String defaultCustomMetricKey : defaultRequest.custommMetrics().keySet()) { - customMetricParms.put(defaultCustomMetricKey, defaultRequest.custommMetrics().get(defaultCustomMetricKey)); + private void processCustomSubParameters(@SuppressWarnings("rawtypes") GoogleAnalyticsRequest request, List postParms) { + + int productIndex = 0; + + @SuppressWarnings("unchecked") + Map productParametersMap = request.getProductParameters(); + for (Entry prodocutParametersEntry : productParametersMap.entrySet()) { + + productIndex = prodocutParametersEntry.getKey(); + + for (Entry productParametersValueEntry + : prodocutParametersEntry.getValue().getParameters().entrySet()) { + + String key = productParametersValueEntry.getKey().getParameterName() + .replace("#", Integer.toString(productIndex)); + + postParms.add(new BasicNameValuePair(key, productParametersValueEntry.getValue())); + } + + customDimensionAndMetrics(prodocutParametersEntry.getValue(), postParms, 0, productIndex); + } + productIndex = 0; + @SuppressWarnings("unchecked") - Map requestCustomMetrics = request.custommMetrics(); - for (String requestCustomDimKey : requestCustomMetrics.keySet()) { - customMetricParms.put(requestCustomDimKey, requestCustomMetrics.get(requestCustomDimKey)); + Map productImpressionParametersMap = request.getProductImpressionParameters(); + for (Entry prodocutImpressionParametersEntry : productImpressionParametersMap.entrySet()) { + + int listIndex = prodocutImpressionParametersEntry.getKey(); + + Map productImpressionProductParametersMap = prodocutImpressionParametersEntry.getValue().subParameters(); + for (Entry prodocutParametersEntry : productImpressionProductParametersMap.entrySet()) { + + productIndex = prodocutParametersEntry.getKey(); + + for (Entry productParametersValueEntry + : prodocutParametersEntry.getValue().getParameters().entrySet()) { + + String key = productParametersValueEntry.getKey().getParameterName() + .replace("#", Integer.toString(productIndex)) + .replace("$", Integer.toString(listIndex)); + + postParms.add(new BasicNameValuePair(key, productParametersValueEntry.getValue())); + } + + customDimensionAndMetrics(prodocutParametersEntry.getValue(), postParms, listIndex, productIndex); + + } + } - - for (String key : customMetricParms.keySet()) { - postParms.add(new BasicNameValuePair(key, customMetricParms.get(key))); + + int promoIndex = 0; + + @SuppressWarnings("unchecked") + Map promotionParametersMap = request.getPromotionParameters(); + for (Entry promotionParametersEntry : promotionParametersMap.entrySet()) { + + promoIndex = promotionParametersEntry.getKey(); + + for (Entry productParametersValueEntry + : promotionParametersEntry.getValue().getParameters().entrySet()) { + + String key = productParametersValueEntry.getKey().getParameterName() + .replace("&", Integer.toString(promoIndex)); + + postParms.add(new BasicNameValuePair(key, productParametersValueEntry.getValue())); + } } } - + private void customDimensionAndMetrics(SubParameters parameterSet, List postParms, + int listIndex, int productIndex) { + + // custom dimensions + for (Entry customDimensionEntry : parameterSet.customDimensions().entrySet()) { + int customIndex = customDimensionEntry.getKey(); + String key = GoogleAnalyticsParameter.PRODUCT_IMPRESSION_CUSTOM_DIMENSION.getParameterName() + .replace("#", Integer.toString(productIndex)) + .replace("$", Integer.toString(listIndex)) + .replace("§", Integer.toString(customIndex)) + + customDimensionEntry.getKey(); + + postParms.add(new BasicNameValuePair(key, customDimensionEntry.getValue())); + } + + // custom metrics + for (Entry customMetricsEntry : parameterSet.customMetrics().entrySet()) { + int customIndex = customMetricsEntry.getKey(); + String key = GoogleAnalyticsParameter.PRODUCT_IMPRESSION_CUSTOM_METRIC.getParameterName() + .replace("#", Integer.toString(productIndex)) + .replace("$", Integer.toString(listIndex)) + .replace("§", Integer.toString(customIndex)) + + customMetricsEntry.getKey(); + + postParms.add(new BasicNameValuePair(key, Integer.toString(customMetricsEntry.getValue()))); + } + } + private void gatherStats(@SuppressWarnings("rawtypes") GoogleAnalyticsRequest request) { String hitType = request.hitType(); diff --git a/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsParameter.java b/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsParameter.java index 4874148..ef1822d 100644 --- a/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsParameter.java +++ b/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsParameter.java @@ -21,6 +21,9 @@ * @author Santhosh Kumar */ public enum GoogleAnalyticsParameter { + + // TODO: check private final static String[] SUPPORT_PAGEVIEW_EVENT = new String[] {"pageview", "event"} + //General PROTOCOL_VERSION("v", true), TRACKING_ID("tid", true), @@ -85,11 +88,11 @@ public enum GoogleAnalyticsParameter { EVENT_VALUE("ev", false, "integer", new String[] {"event"}), //E-Commerce - TRANSACTION_ID("ti", new String[] {"transaction", "item"}, 500), - TRANSACTION_AFFILIATION("ta", new String[] {"transaction"}, 500), - TRANSACTION_REVENUE("tr", false, "currency", new String[] {"transaction"}), - TRANSACTION_SHIPPING("ts", false, "currency", new String[] {"transaction"}), - TRANSACTION_TAX("tt", false, "currency", new String[] {"transaction"}), + TRANSACTION_ID("ti", new String[] {"transaction", "item", "pageview", "event"}, 500), + TRANSACTION_AFFILIATION("ta", new String[] {"transaction", "pageview", "event"}, 500), + TRANSACTION_REVENUE("tr", false, "currency", new String[] {"transaction", "pageview", "event"}), + TRANSACTION_SHIPPING("ts", false, "currency", new String[] {"transaction", "pageview", "event"}), + TRANSACTION_TAX("tt", false, "currency", new String[] {"transaction", "pageview", "event"}), ITEM_NAME("in", new String[] {"item"}, 500), ITEM_PRICE("ip", false, "currency", new String[] {"item"}), ITEM_QUANTITY("iq", false, "integer", new String[] {"item"}), @@ -97,6 +100,39 @@ public enum GoogleAnalyticsParameter { ITEM_CATEGORY("iv", new String[] {"item"}, 500), CURRENCY_CODE("cu", new String[] {"transaction", "item"}, 10), + // Enhanced E-Commerce + PRODUCT_SKU("pr#id", new String[] {"pageview", "event"}, 500), + PRODUCT_NAME("pr#nm", new String[] {"pageview", "event"}, 500), + PRODUCT_BRAND("pr#br", new String[] {"pageview", "event"}, 500), + PRODUCT_CATEGORY("pr#ca", new String[] {"pageview", "event"}, 500), + PRODUCT_VARIANT("pr#va", new String[] {"pageview", "event"}, 500), + PRODUCT_PRICE("pr#pr", false, "currency", new String[] {"pageview", "event"}), + PRODUCT_QUANTITY("pr#qt", false, "integer", new String[] {"pageview", "event"}), + PRODUCT_COUPON_CODE("pr#cc", new String[] {"pageview", "event"}, 500), + PRODUCT_POSITION("pr#ps", false, "integer", new String[] {"pageview", "event"}), + PRODUCT_CUSTOM_DIMENSION("pr#cd§", new String[] {"pageview", "event"}), + PRODUCT_CUSTOM_METRIC("pr#cm§", false, "integer", new String[] {"pageview", "event"}), + PRODUCT_ACTION("pa", new String[] {"pageview", "event"}), + TRANSACTION_COUPON_CODE("tcc", new String[] {"pageview", "event"}), + PRODUCT_ACTION_LIST("pal", new String[] {"pageview", "event"}), + CHECKOUT_STEP("cos", false, "integer", new String[] {"pageview", "event"}), + CHECKOUT_STEP_OPTION("pal", new String[] {"pageview", "event"}), + PRODUCT_IMPRESSION_LIST_NAME("pr$nm", new String[] {"pageview", "event"}), + PRODUCT_IMPRESSION_SKU("pr$pi#id", new String[] {"pageview", "event"}), + PRODUCT_IMPRESSION_NAME("pr$pi#nm", new String[] {"pageview", "event"}), + PRODUCT_IMPRESSION_BRAND("pr$pi#br", new String[] {"pageview", "event"}), + PRODUCT_IMPRESSION_CATEGORY("pr$pi#ca", new String[] {"pageview", "event"}), + PRODUCT_IMPRESSION_VARIANT("pr$pi#va", new String[] {"pageview", "event"}), + PRODUCT_IMPRESSION_POSITION("pr$pi#ps", false, "integer", new String[] {"pageview", "event"}), + PRODUCT_IMPRESSION_PRICE("pr$pi#pr", false, "currency", new String[] {"pageview", "event"}), + PRODUCT_IMPRESSION_CUSTOM_DIMENSION("pr$pi#cd§", new String[] {"pageview", "event"}), + PRODUCT_IMPRESSION_CUSTOM_METRIC("pr$pi#cm§", false, "integer", new String[] {"pageview", "event"}), + PROMOTION_ID("promo&id", new String[] {"pageview", "event"}), + PROMOTION_NAME("promo&nm", new String[] {"pageview", "event"}), + PROMOTION_CREATIVE("promo&cr", new String[] {"pageview", "event"}), + PROMOTION_POSITION("promo&ps", new String[] {"pageview", "event"}), + PROMOTION_ACTION("promoa", new String[] {"pageview", "event"}), + //Social Interactions SOCIAL_NETWORK("sn", new String[] {"social"}, 50), SOCIAL_ACTION("sa", new String[] {"social"}, 50), diff --git a/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsRequest.java b/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsRequest.java index f353e82..56048c1 100644 --- a/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsRequest.java +++ b/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsRequest.java @@ -47,6 +47,7 @@ import static com.brsanthu.googleanalytics.GoogleAnalyticsParameter.SCREEN_RESOLUTION; import static com.brsanthu.googleanalytics.GoogleAnalyticsParameter.SESSION_CONTROL; import static com.brsanthu.googleanalytics.GoogleAnalyticsParameter.TRACKING_ID; +import static com.brsanthu.googleanalytics.GoogleAnalyticsParameter.TRANSACTION_REVENUE; import static com.brsanthu.googleanalytics.GoogleAnalyticsParameter.USER_ID; import static com.brsanthu.googleanalytics.GoogleAnalyticsParameter.USER_LANGUAGE; import static com.brsanthu.googleanalytics.GoogleAnalyticsParameter.VIEWPORT_SIZE; @@ -69,6 +70,9 @@ public class GoogleAnalyticsRequest { protected Map parms = new HashMap(); protected Map customDimensions = new HashMap(); protected Map customMetrics = new HashMap(); + protected Map productParameters = new HashMap(); + protected Map productImpressionParameters = new HashMap(); + protected Map promotionParameters = new HashMap(); public GoogleAnalyticsRequest() { this(null, null, null, null); @@ -172,7 +176,7 @@ protected String fromInteger(Integer intValue) { return null; } - return "" + intValue; + return Integer.toString(intValue); } protected Integer toInteger(String intString) { @@ -188,7 +192,7 @@ protected String fromDouble(Double doubleValue) { return null; } - return "" + doubleValue; + return String.format("%.2f", doubleValue); } protected Double toDouble(String doubleString) { @@ -327,6 +331,18 @@ public Map custommMetrics() { return customMetrics; } + public Map getProductParameters() { + return productParameters; + } + + public Map getProductImpressionParameters() { + return productImpressionParameters; + } + + public Map getPromotionParameters() { + return promotionParameters; + } + @Override public String toString() { StringBuilder builder = new StringBuilder(); @@ -345,6 +361,18 @@ public String toString() { builder.append("customMetrics="); builder.append(customMetrics); } + if (productParameters != null) { + builder.append("productParameters="); + builder.append(productParameters); + } + if (productImpressionParameters != null) { + builder.append("productImpressionParameters="); + builder.append(productImpressionParameters); + } + if (promotionParameters != null) { + builder.append("promotionParameters="); + builder.append(promotionParameters); + } builder.append("]"); return builder.toString(); } @@ -1880,4 +1908,358 @@ protected boolean isEmpty(String string) { return string == null || string.trim().length() == 0; } + /** + * Enhanced E-Commerce Tracking + */ + public T transactionId(String value) { + setString(GoogleAnalyticsParameter.TRANSACTION_ID, value); + return (T) this; + } + public String transactionId() { + return getString(GoogleAnalyticsParameter.TRANSACTION_ID); + } + + public T transactionRevenue(Double value) { + setDouble(GoogleAnalyticsParameter.TRANSACTION_REVENUE, value); + return (T) this; + } + public Double transactionRevenue() { + return getDouble(GoogleAnalyticsParameter.TRANSACTION_REVENUE); + } + + public T productSku(int productIndex, String value) { + SubParameters thisProductParameters = getDistinctSubParameters(productParameters, productIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PRODUCT_SKU, value); + return (T) this; + } + + public String productSku(int productIndex) { + return productParameters.get(productIndex).parameter(GoogleAnalyticsParameter.PRODUCT_SKU); + } + + public T productName(int productIndex, String value) { + SubParameters thisProductParameters = getDistinctSubParameters(productParameters, productIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PRODUCT_NAME, value); + return (T) this; + } + + public String productName(int productIndex) { + return productParameters.get(productIndex).parameter(GoogleAnalyticsParameter.PRODUCT_NAME); + } + + public T productBrand(int productIndex, String value) { + SubParameters thisProductParameters = getDistinctSubParameters(productParameters, productIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PRODUCT_BRAND, value); + return (T) this; + } + + public String productBrand(int productIndex) { + return productParameters.get(productIndex).parameter(GoogleAnalyticsParameter.PRODUCT_BRAND); + } + + public T productCategory(int productIndex, String value) { + SubParameters thisProductParameters = getDistinctSubParameters(productParameters, productIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PRODUCT_CATEGORY, value); + return (T) this; + } + + public String productCategory(int productIndex) { + return productParameters.get(productIndex).parameter(GoogleAnalyticsParameter.PRODUCT_CATEGORY); + } + + public T productVariant(int productIndex, String value) { + SubParameters thisProductParameters = getDistinctSubParameters(productParameters, productIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PRODUCT_VARIANT, value); + return (T) this; + } + + public String productVariant(int productIndex) { + return productParameters.get(productIndex).parameter(GoogleAnalyticsParameter.PRODUCT_VARIANT); + } + + public T productPrice(int productIndex, double value) { + SubParameters thisProductParameters = getDistinctSubParameters(productParameters, productIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PRODUCT_PRICE, fromDouble(value)); + return (T) this; + } + + public double productPrice(int productIndex) { + return toDouble(productParameters.get(productIndex).parameter(GoogleAnalyticsParameter.PRODUCT_PRICE)); + } + + public T productQuantity(int productIndex, int value) { + SubParameters thisProductParameters = getDistinctSubParameters(productParameters, productIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PRODUCT_QUANTITY, fromInteger(value)); + return (T) this; + } + + public int productQuantity(int productIndex) { + return toInteger(productParameters.get(productIndex).parameter(GoogleAnalyticsParameter.PRODUCT_QUANTITY)); + } + + public T productCouponCode(int productIndex, String value) { + SubParameters thisProductParameters = getDistinctSubParameters(productParameters, productIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PRODUCT_COUPON_CODE, value); + return (T) this; + } + + public String productCouponCode(int productIndex) { + return productParameters.get(productIndex).parameter(GoogleAnalyticsParameter.PRODUCT_COUPON_CODE); + } + + public T productPosition(int productIndex, int value) { + SubParameters thisProductParameters = getDistinctSubParameters(productParameters, productIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PRODUCT_POSITION, fromInteger(value)); + return (T) this; + } + + public int productPosition(int productIndex) { + return toInteger(productParameters.get(productIndex).parameter(GoogleAnalyticsParameter.PRODUCT_POSITION)); + } + + public T productCustomDimension(int productIndex, int index, String value) { + SubParameters thisProductParameters = getDistinctSubParameters(productParameters, productIndex); + thisProductParameters.customDimension(index, value); + return (T) this; + } + + public String productCustomDimension(int productIndex, int index) { + return productParameters.get(productIndex).customDimension(index); + } + + public T productCustomMetric(int productIndex, int index, int value) { + SubParameters thisProductParameters = getDistinctSubParameters(productParameters, productIndex); + thisProductParameters.customMetric(index, value); + return (T) this; + } + + public int productCustomMetric(int productIndex, int index) { + return productParameters.get(productIndex).customMetric(index); + } + + public T productAction(String value) { + setString(GoogleAnalyticsParameter.PRODUCT_ACTION, value); + return (T) this; + } + + public String productAction() { + return getString(GoogleAnalyticsParameter.PRODUCT_ACTION); + } + + public T transactionCouponCode(String value) { + setString(GoogleAnalyticsParameter.TRANSACTION_COUPON_CODE, value); + return (T) this; + } + + public String transactionCouponCode() { + return getString(GoogleAnalyticsParameter.TRANSACTION_COUPON_CODE); + } + + public T productActionList(String value) { + setString(GoogleAnalyticsParameter.PRODUCT_ACTION_LIST, value); + return (T) this; + } + + public String productActionList() { + return getString(GoogleAnalyticsParameter.PRODUCT_ACTION_LIST); + } + + public T checkoutStep(int value) { + setInteger(GoogleAnalyticsParameter.CHECKOUT_STEP, value); + return (T) this; + } + + public int checkoutStep() { + return getInteger(GoogleAnalyticsParameter.CHECKOUT_STEP); + } + + public T checkoutStepOption(String value) { + setString(GoogleAnalyticsParameter.CHECKOUT_STEP_OPTION, value); + return (T) this; + } + + public String checkoutStepOption() { + return getString(GoogleAnalyticsParameter.CHECKOUT_STEP_OPTION); + } + + public T productImpressionListName(int listIndex, String value) { + SubParameters thisListParameters = getDistinctSubParameters(productImpressionParameters, listIndex); + thisListParameters.parameter(GoogleAnalyticsParameter.PRODUCT_IMPRESSION_LIST_NAME, value); + return (T) this; + } + + public String productImpressionListName(int listIndex) { + return productImpressionParameters.get(listIndex).parameter(GoogleAnalyticsParameter.PRODUCT_IMPRESSION_LIST_NAME); + } + + public T productImpressionSku(int listIndex, int productIndex, String value) { + SubParameters thisListParameters = getDistinctSubParameters(productImpressionParameters, listIndex); + SubParameters thisProductParameters = getDistinctSubParameters(thisListParameters.subParameters(), productIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PRODUCT_IMPRESSION_SKU, value); + return (T) this; + } + + public String productImpressionSku(int listIndex, int productIndex) { + return productImpressionParameters.get(listIndex) + .subParameters().get(productIndex) + .parameter(GoogleAnalyticsParameter.PRODUCT_IMPRESSION_SKU); + } + + public T productImpressionName(int listIndex, int productIndex, String value) { + SubParameters thisListParameters = getDistinctSubParameters(productImpressionParameters, listIndex); + SubParameters thisProductParameters = getDistinctSubParameters(thisListParameters.subParameters(), productIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PRODUCT_IMPRESSION_NAME, value); + return (T) this; + } + + public String productImpressionName(int listIndex, int productIndex) { + return productImpressionParameters.get(listIndex) + .subParameters().get(productIndex) + .parameter(GoogleAnalyticsParameter.PRODUCT_IMPRESSION_NAME); + } + + public T productImpressionBrand(int listIndex, int productIndex, String value) { + SubParameters thisListParameters = getDistinctSubParameters(productImpressionParameters, listIndex); + SubParameters thisProductParameters = getDistinctSubParameters(thisListParameters.subParameters(), productIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PRODUCT_IMPRESSION_BRAND, value); + return (T) this; + } + + public String productImpressionBrand(int listIndex, int productIndex) { + return productImpressionParameters.get(listIndex) + .subParameters().get(productIndex) + .parameter(GoogleAnalyticsParameter.PRODUCT_IMPRESSION_BRAND); + } + + public T productImpressionCategory(int listIndex, int productIndex, String value) { + SubParameters thisListParameters = getDistinctSubParameters(productImpressionParameters, listIndex); + SubParameters thisProductParameters = getDistinctSubParameters(thisListParameters.subParameters(), productIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PRODUCT_IMPRESSION_CATEGORY, value); + return (T) this; + } + + public String productImpressionCategory(int listIndex, int productIndex) { + return productImpressionParameters.get(listIndex) + .subParameters().get(productIndex) + .parameter(GoogleAnalyticsParameter.PRODUCT_IMPRESSION_CATEGORY); + } + + public T productImpressionVariant(int listIndex, int productIndex, String value) { + SubParameters thisListParameters = getDistinctSubParameters(productImpressionParameters, listIndex); + SubParameters thisProductParameters = getDistinctSubParameters(thisListParameters.subParameters(), productIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PRODUCT_IMPRESSION_VARIANT, value); + return (T) this; + } + + public String productImpressionVariant(int listIndex, int productIndex) { + return productImpressionParameters.get(listIndex) + .subParameters().get(productIndex) + .parameter(GoogleAnalyticsParameter.PRODUCT_IMPRESSION_VARIANT); + } + + public T productImpressionPosition(int listIndex, int productIndex, int value) { + SubParameters thisListParameters = getDistinctSubParameters(productImpressionParameters, listIndex); + SubParameters thisProductParameters = getDistinctSubParameters(thisListParameters.subParameters(), productIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PRODUCT_IMPRESSION_POSITION, fromInteger(value)); + return (T) this; + } + + public int productImpressionPosition(int listIndex, int productIndex) { + return toInteger(productImpressionParameters.get(listIndex) + .subParameters().get(productIndex) + .parameter(GoogleAnalyticsParameter.PRODUCT_IMPRESSION_POSITION)); + } + + public T productImpressionPrice(int listIndex, int productIndex, double value) { + SubParameters thisListParameters = getDistinctSubParameters(productImpressionParameters, listIndex); + SubParameters thisProductParameters = getDistinctSubParameters(thisListParameters.subParameters(), productIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PRODUCT_IMPRESSION_PRICE, fromDouble(value)); + return (T) this; + } + + public double productImpressionPrice(int listIndex, int productIndex) { + return toDouble(productImpressionParameters.get(listIndex) + .subParameters().get(productIndex) + .parameter(GoogleAnalyticsParameter.PRODUCT_IMPRESSION_PRICE)); + } + + public T productImpressionCustomDimension(int listIndex, int productIndex, int index, String value) { + SubParameters thisListParameters = getDistinctSubParameters(productImpressionParameters, listIndex); + SubParameters thisProductParameters = getDistinctSubParameters(thisListParameters.subParameters(), productIndex); + thisProductParameters.customDimension(index, value); + return (T) this; + } + + public String productImpressionCustomDimension(int listIndex, int productIndex, int index) { + return productImpressionParameters.get(listIndex) + .subParameters().get(productIndex) + .customDimension(index); + } + + public T productImpressionCustomMetric(int listIndex, int productIndex, int index, int value) { + SubParameters thisListParameters = getDistinctSubParameters(productImpressionParameters, listIndex); + SubParameters thisProductParameters = getDistinctSubParameters(thisListParameters.subParameters(), productIndex); + thisProductParameters.customMetric(index, value); + return (T) this; + } + + public int productImpressionCustomMetric(int listIndex, int productIndex, int index) { + return productImpressionParameters.get(listIndex) + .subParameters().get(productIndex) + .customMetric(index); + } + + public T promotionId(int promotionIndex, String value) { + SubParameters thisProductParameters = getDistinctSubParameters(promotionParameters, promotionIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PROMOTION_ID, value); + return (T) this; + } + + public String promotionId(int promotionIndex) { + return promotionParameters.get(promotionIndex).parameter(GoogleAnalyticsParameter.PROMOTION_ID); + } + + public T promotionName(int promotionIndex, String value) { + SubParameters thisProductParameters = getDistinctSubParameters(promotionParameters, promotionIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PROMOTION_NAME, value); + return (T) this; + } + + public String promotionName(int promotionIndex) { + return promotionParameters.get(promotionIndex).parameter(GoogleAnalyticsParameter.PROMOTION_NAME); + } + + public T promotionCreative(int promotionIndex, String value) { + SubParameters thisProductParameters = getDistinctSubParameters(promotionParameters, promotionIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PROMOTION_CREATIVE, value); + return (T) this; + } + + public String promotionCreative(int promotionIndex) { + return promotionParameters.get(promotionIndex).parameter(GoogleAnalyticsParameter.PROMOTION_CREATIVE); + } + + public T promotionPosition(int promotionIndex, String value) { + SubParameters thisProductParameters = getDistinctSubParameters(promotionParameters, promotionIndex); + thisProductParameters.parameter(GoogleAnalyticsParameter.PROMOTION_POSITION, value); + return (T) this; + } + + public String promotionPosition(int promotionIndex) { + return promotionParameters.get(promotionIndex).parameter(GoogleAnalyticsParameter.PROMOTION_POSITION); + } + + public T promotionAction(String value) { + setString(GoogleAnalyticsParameter.PROMOTION_ACTION, value); + return (T) this; + } + + public String promotionAction() { + return getString(GoogleAnalyticsParameter.PROMOTION_ACTION); + } + + private SubParameters getDistinctSubParameters(Map pmap, int index) { + if (!pmap.containsKey(index)) pmap.put(index, new SubParameters()); + return pmap.get(index); + } } diff --git a/src/main/java/com/brsanthu/googleanalytics/SubParameters.java b/src/main/java/com/brsanthu/googleanalytics/SubParameters.java new file mode 100644 index 0000000..959cd65 --- /dev/null +++ b/src/main/java/com/brsanthu/googleanalytics/SubParameters.java @@ -0,0 +1,96 @@ +package com.brsanthu.googleanalytics; + +import java.util.HashMap; +import java.util.Map; + +public class SubParameters { + + protected Map parms = new HashMap(); + protected Map customDimensions = new HashMap(); + protected Map customMetrics = new HashMap(); + + protected Map subParameters = new HashMap(); + + // general parameters + public String parameter(GoogleAnalyticsParameter parameter) { + return parms.get(parameter); + } + + public void parameter(GoogleAnalyticsParameter parameter, String value) { + if (value == null) { + parms.remove(parameter); + } else { + parms.put(parameter, value); + } + } + + public Map getParameters() { + return parms; + } + + // custom dimensions + public String customDimension(int index) { + return customDimensions.get(index); + } + + public void customDimension(int index, String value) { + customDimensions.put(index, value); + } + + public Map customDimensions() { + return customDimensions; + } + + // custom metrics + public int customMetric(int index) { + return customMetrics.get(index); + } + + public void customMetric(int index, int value) { + customMetrics.put(index, value); + } + + public Map customMetrics() { + return customMetrics; + } + + // sub parameters + public SubParameters subParameters(int index) { + return subParameters.get(index); + } + + public void subParameters(int index, SubParameters value) { + subParameters.put(index, value); + } + + public Map subParameters() { + return subParameters; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("SubParameters ["); + if (parms != null) { + builder.append("parms="); + builder.append(parms); + builder.append(", "); + } + if (customDimensions != null) { + builder.append("customDimensions="); + builder.append(customDimensions); + builder.append(", "); + } + if (customMetrics != null) { + builder.append("customMetrics="); + builder.append(customMetrics); + } + if (subParameters != null) { + builder.append("subParameters="); + builder.append(subParameters); + } + builder.append("]"); + return builder.toString(); + } + +}