Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 110 additions & 34 deletions src/main/java/com/brsanthu/googleanalytics/GoogleAnalytics.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand All @@ -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());
Expand Down Expand Up @@ -194,52 +198,124 @@ private void processParameters(GoogleAnalyticsRequest request, List<NameValuePai
}

/**
* Processes the custom dimensions and adds the values to list of parameters, which would be posted to GA.
* Processes the custom dimensions/metrics and adds the values to list of parameters, which would be posted to GA.
*
* @param request
* @param postParms
* @param defaultCustomElements
* @param requestCustomElements
*/
private void processCustomDimensionParameters(@SuppressWarnings("rawtypes") GoogleAnalyticsRequest request, List<NameValuePair> postParms) {
Map<String, String> customDimParms = new HashMap<String, String>();
for (String defaultCustomDimKey : defaultRequest.customDimensions().keySet()) {
customDimParms.put(defaultCustomDimKey, defaultRequest.customDimensions().get(defaultCustomDimKey));
}
private void processCustomParameters(@SuppressWarnings("rawtypes") GoogleAnalyticsRequest request, List<NameValuePair> postParms,
Map<String, String> defaultCustomElements, Map<String, String> requestCustomElements) {

@SuppressWarnings("unchecked")
Map<String, String> requestCustomDims = request.customDimensions();
for (String requestCustomDimKey : requestCustomDims.keySet()) {
customDimParms.put(requestCustomDimKey, requestCustomDims.get(requestCustomDimKey));
}
Map<String, String> customParms = new HashMap<String, String>();
customParms.putAll(defaultCustomElements);
customParms.putAll(requestCustomElements);

for (String key : customDimParms.keySet()) {
postParms.add(new BasicNameValuePair(key, customDimParms.get(key)));
for (Entry<String, String> 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<NameValuePair> postParms) {
Map<String, String> customMetricParms = new HashMap<String, String>();
for (String defaultCustomMetricKey : defaultRequest.custommMetrics().keySet()) {
customMetricParms.put(defaultCustomMetricKey, defaultRequest.custommMetrics().get(defaultCustomMetricKey));
private void processCustomSubParameters(@SuppressWarnings("rawtypes") GoogleAnalyticsRequest request, List<NameValuePair> postParms) {

int productIndex = 0;

@SuppressWarnings("unchecked")
Map<Integer, SubParameters> productParametersMap = request.getProductParameters();
for (Entry<Integer, SubParameters> prodocutParametersEntry : productParametersMap.entrySet()) {

productIndex = prodocutParametersEntry.getKey();

for (Entry<GoogleAnalyticsParameter, String> 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<String, String> requestCustomMetrics = request.custommMetrics();
for (String requestCustomDimKey : requestCustomMetrics.keySet()) {
customMetricParms.put(requestCustomDimKey, requestCustomMetrics.get(requestCustomDimKey));
Map<Integer, SubParameters> productImpressionParametersMap = request.getProductImpressionParameters();
for (Entry<Integer, SubParameters> prodocutImpressionParametersEntry : productImpressionParametersMap.entrySet()) {

int listIndex = prodocutImpressionParametersEntry.getKey();

Map<Integer, SubParameters> productImpressionProductParametersMap = prodocutImpressionParametersEntry.getValue().subParameters();
for (Entry<Integer, SubParameters> prodocutParametersEntry : productImpressionProductParametersMap.entrySet()) {

productIndex = prodocutParametersEntry.getKey();

for (Entry<GoogleAnalyticsParameter, String> 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<Integer, SubParameters> promotionParametersMap = request.getPromotionParameters();
for (Entry<Integer, SubParameters> promotionParametersEntry : promotionParametersMap.entrySet()) {

promoIndex = promotionParametersEntry.getKey();

for (Entry<GoogleAnalyticsParameter, String> 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<NameValuePair> postParms,
int listIndex, int productIndex) {

// custom dimensions
for (Entry<Integer, String> 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<Integer, Integer> 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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -85,18 +88,51 @@ 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"}),
ITEM_CODE("ic", new String[] {"item"}, 500),
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"}),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is "col"

PRODUCT_IMPRESSION_LIST_NAME("pr$nm", new String[] {"pageview", "event"}),
PRODUCT_IMPRESSION_SKU("pr$pi#id", new String[] {"pageview", "event"}),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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),
Expand Down
Loading