Skip to content

Commit

Permalink
Add Custom Dimensions plugin support;
Browse files Browse the repository at this point in the history
Add dimensions id and variable limitations;
  • Loading branch information
dotsbb committed Dec 6, 2015
1 parent 6059e3f commit f0d6d9c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ This method uses ``SharedPreferences`` to ensures that tracking application down
((YourApplication) getApplication()).getTracker().trackAppDownload();
```

#### Custom Dimensions
To track [Custom Dimensions](https://plugins.piwik.org/CustomDimensions) in scope Action or Visit
consider following example:

```java

Tracker tracker = ((YourApplication) getApplication()).getTracker();
tracker.track(
new CustomDimensions()
.set(1, "foo")
.set(2, "bar")
);
```

#### Ecommerce

Piwik provides ecommerce analytics that let you measure items added to carts,
Expand Down
13 changes: 9 additions & 4 deletions piwik-sdk/src/main/java/org/piwik/sdk/TrackMe.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public class TrackMe {
private final HashMap<String, String> mQueryParams = new HashMap<>(DEFAULT_QUERY_CAPACITY);
private final CustomVariables mScreenCustomVariable = new CustomVariables();

protected synchronized TrackMe set(@NonNull String key, String value) {
if (value == null)
mQueryParams.remove(key);
else if (value.length() > 0)
mQueryParams.put(key, value);
return this;
}

/**
* You can set any additional Tracking API Parameters within the SDK.
* This includes for example the local time (parameters h, m and s).
Expand All @@ -34,10 +42,7 @@ public class TrackMe {
* @return tracker instance
*/
public synchronized TrackMe set(@NonNull QueryParams key, String value) {
if (value == null)
mQueryParams.remove(key.toString());
else if (value.length() > 0)
mQueryParams.put(key.toString(), value);
set(key.toString(), value);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.piwik.sdk.plugins;

import org.piwik.sdk.Piwik;
import org.piwik.sdk.tools.Logy;
import org.piwik.sdk.TrackMe;

/**
* This plugins allows you to track any Custom Dimensions.
* In order to use this functionality install and configure
* https://plugins.piwik.org/CustomDimensions plugin.
*/
public class CustomDimensions extends TrackMe {
protected static final String LOGGER_TAG = Piwik.LOGGER_PREFIX + "CustomDimensions";
/**
* This method sets a tracking API parameter dimension%dimensionId%=%dimensionValue%.
* Eg dimension1=foo or dimension2=bar.
* So the tracking API parameter starts with dimension followed by the set dimensionId.
* @param dimensionId accepts values greater than 0
* @param dimensionValue is limited to 255 characters
* @return instance of CustomDimensions plugin
*/
public synchronized CustomDimensions set(int dimensionId, String dimensionValue) {
if (dimensionId < 1){
Logy.w(LOGGER_TAG, "dimensionId should be great than 0");
return this;
}
if (dimensionValue != null && dimensionValue.length() > 255){
Logy.w(LOGGER_TAG, "dimensionValue will be truncated to 255 chars");
dimensionValue = dimensionValue.substring(0, 255);
}
set("dimension" + dimensionId, dimensionValue);
return this;
}

}
28 changes: 28 additions & 0 deletions piwik-sdk/src/test/java/org/piwik/sdk/TrackerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.piwik.sdk.ecommerce.EcommerceItems;
import org.piwik.sdk.plugins.CustomDimensions;
import org.robolectric.Robolectric;
import org.robolectric.annotation.Config;

Expand Down Expand Up @@ -834,6 +835,33 @@ public void testPreviousVisit() throws Exception {
assertEquals(1000l, Long.parseLong(queryParams.get(QueryParams.PREVIOUS_VISIT_TIMESTAMP)));
}

@Test
public void testSetCustomDimensions() throws Exception {
CustomDimensions customDimensions = new CustomDimensions();
customDimensions.set(0, "foo");
customDimensions.set(1, "foo");
customDimensions.set(2, "bar");
customDimensions.set(3, "empty").set(3, null);
customDimensions.set(4, "");

QueryHashMap<String, String> queryParams = parseEventUrl(customDimensions.build());

assertEquals(queryParams.get("dimension1"), "foo");
assertEquals(queryParams.get("dimension2"), "bar");
assertFalse(queryParams.containsKey("dimension0"));
assertFalse(queryParams.containsKey("dimension3"));
assertFalse(queryParams.containsKey("dimension4"));
}

@Test
public void testSetCustomDimensionsMaxLength() throws Exception {
CustomDimensions customDimensions = new CustomDimensions();
customDimensions.set(1, new String(new char[1000]));

QueryHashMap<String, String> queryParams = parseEventUrl(customDimensions.build());
assertEquals(queryParams.get("dimension1").length(), 255);
}

private static class QueryHashMap<String, V> extends HashMap<String, V> {

private QueryHashMap() {
Expand Down

0 comments on commit f0d6d9c

Please sign in to comment.