Android Open Source - piwik-sdk-android Custom Variables






From Project

Back to project page piwik-sdk-android.

License

The source code is released under:

Copyright 2014 Piwik team All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redi...

If you think the Android project piwik-sdk-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Android SDK for Piwik//from   w  ww  . j a  va 2s  . c o  m
 *
 * @link https://github.com/piwik/piwik-android-sdk
 * @license https://github.com/piwik/piwik-sdk-android/blob/master/LICENSE BSD-3 Clause
 */

package org.piwik.sdk;

import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Arrays;
import java.util.HashMap;


public class CustomVariables extends HashMap<String, JSONArray> {
    /**
     * You can track up to 5 custom variables for each user to your app,
     * and up to 5 custom variables for each screen view.
     * <p/>
     * Desired json output:
     * {
     * "1":["OS","iphone 5.0"],
     * "2":["Piwik Mobile Version","1.6.2"],
     * "3":["Locale","en::en"],
     * "4":["Num Accounts","2"],
     * "5":["Level","over9k"]
     * }
     */
    private static final int MAX_VARIABLES = 5;
    protected static final int MAX_LENGTH = 200;

    protected CustomVariables() {
        super(MAX_VARIABLES);
    }

    /**
     * Custom variable names and values are limited to 200 characters in length each.
     *
     * @param index this Integer accepts values from 1 to 5.
     *              A given custom variable name must always be stored in the same "index" per session.
     *              For example, if you choose to store the variable name = "Gender" in index = 1
     *              and you record another custom variable in index = 1, then the "Gender" variable
     *              will be deleted and replaced with the new custom variable stored in index 1.
     * @param name  of a specific Custom Variable such as "User type".
     * @param value of a specific Custom Variable such as "Customer".
     * @return super.put result if index in right range and name/value pair aren't null
     */
    public JSONArray put(int index, String name, String value) {
        if (index > 0 && index <= MAX_VARIABLES && name != null & value != null) {

            if (name.length() > MAX_LENGTH) {
                Log.w(Tracker.LOGGER_TAG, String.format("Name is too long %s", name));
                name = name.substring(0, MAX_LENGTH);
            }

            if (value.length() > MAX_LENGTH) {
                Log.w(Tracker.LOGGER_TAG, String.format("Value is too long %s", value));
                value = value.substring(0, MAX_LENGTH);
            }

            return put(Integer.toString(index), new JSONArray(Arrays.asList(name, value)));
        }
        Log.d(Tracker.LOGGER_TAG, "Index is out of range or name/value is null");
        return null;
    }

    /**
     * @param index  index accepts values from 1 to 5.
     * @param values packed key/value pair
     * @return super.put result or null if key is null or value length is not equals 2
     */
    @Override
    public JSONArray put(String index, JSONArray values) {
        if (values.length() == 2 && index != null) {
            return super.put(index, values);
        }
        Log.d(Tracker.LOGGER_TAG, "value length should be equal 2");
        return null;
    }

    @Override
    public String toString() {
        if (size() == 0) {
            return null;
        }
        return new JSONObject(this).toString();
    }

}




Java Source Code List

com.piwik.demo.ApplicationTest.java
com.piwik.demo.DemoActivity.java
com.piwik.demo.DemoApp.java
com.piwik.demo.SettingsActivity.java
org.piwik.sdk.CustomVariables.java
org.piwik.sdk.Dispatchable.java
org.piwik.sdk.DispatchingHandler.java
org.piwik.sdk.PiwikApplication.java
org.piwik.sdk.Piwik.java
org.piwik.sdk.TrackerBulkURLProcessor.java
org.piwik.sdk.TrackerBulkURLWrapper.java
org.piwik.sdk.Tracker.java