@anonymous-piwik-user opened this Issue on November 14th 2011

Hello, I found a bug in the Piwik 1.6 regarding the custom variables which are sent via Piwik Java Tracker (org.piwik.SimplePiwikTracker.java, namely).

Even if I send tracking requests with more than 1 custom variables, Piwik application shows just only one custom variable.

I checked the source-code briefly, and it seems to be a bug in the following point.

-class : "Piwik_Tracker_Visit" (file:%PIWIK_HOME%/core/Tracker/Visit.php)
-method : getCustomVariables (line:1028)

Here is the snippet which seems to be wrong(from line:1049).

foreach($customVar as $id => $keyValue)
        {
            $id = (int)$id;
            if($id < 1
                || $id > Piwik_Tracker::MAX_CUSTOM_VARIABLES
                || count($keyValue) != 2
                || (!is_string($keyValue[0]) && !is_numeric($keyValue[0]))
            )
            {
                printDebug("Invalid custom variables detected (id=$id)");
                continue;
            }
            if(empty($keyValue[1]))
            {
                $keyValue[1] = "";
            }
            // We keep in the URL when Custom Variable have empty names
            // and values, as it means they can be deleted server side

            $key = self::truncateCustomVariable($keyValue[0]);
            $value = self::truncateCustomVariable($keyValue[1]);
            $customVariables['custom_var_k'.$id] = $key;
            $customVariables['custom_var_v'.$id] = $value;
        }

Here, we fetch custom variables with the index which starts with 0.
Also this index is stored on the variable "$id"

For example)

$customVar[0] : CUSTOM_VAR_1->A
$customVar[1] : CUSTOM_VAR_2->B

Looping should be following.

(1) first fetch -> $id:0 , $keyValue -> "CUSTOM_VAR_1->A"
(2) second fetch -> $id:1 , $keyValue -> "CUSTOM_VAR_2->B"

Continuously the "if" statement checks whether$id is smaller than 1or not.
If yes, this custom variable will be ignored by "continue" statement.
But the first custom variable is pointed by $id=0 because the index starts with 0.

As a result..
When only one custom variable is defined - no custom variable will be shown.
When more than one custom variables are defined - the first custom variable always drops out.

I could make sure by printing out $customVar.
$customVar :

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(9) "Modelname"
    [1]=>
    string(19) "BROWSER Firefox 7.0"
  }
  [1]=>
  array(2) {
    [0]=>
    string(15) "Prefered Markup"
    [1]=>
    string(4) "html"
  }
}

In my case, the first one "Modelname" isn't shown always.

So I tried to correct the logic, as an example.

        foreach($customVar as $id_tmp => $keyValue)
        {
            $id = ((int)$id_tmp) + 1;
            if($id < 1
                || $id > Piwik_Tracker::MAX_CUSTOM_VARIABLES
                || count($keyValue) != 2
                || (!is_string($keyValue[0]) && !is_numeric($keyValue[0]))
            )
            {
                printDebug("Invalid custom variables detected (id=$id)");
                continue;
            }
            if(empty($keyValue[1]))
            {
                $keyValue[1] = "";
            }
            // We keep in the URL when Custom Variable have empty names
            // and values, as it means they can be deleted server side

            $key = self::truncateCustomVariable($keyValue[0]);
            $value = self::truncateCustomVariable($keyValue[1]);
            $customVariables['custom_var_k'.$id] = $key;
            $customVariables['custom_var_v'.$id] = $value;
        }

I did it by "+1" to $id in order that $id doen't have 0.

As I am not so familiar withphp as well as Piwik application, it would be helpful if you can give other good solutions.

But I would like to fix in order to show all custom variables which recieved from PiwikTracker.

Thanks a lot for your work and help :)

Rie

Keywords: Custom Variablies

@mattab commented on November 22nd 2011 Member

tracking several custom variables works for me. Are you really using the JAVA tracker from #2172 or are you using the JAVASCRIPT tracker?

@anonymous-piwik-user commented on November 28th 2011

We are using the JAVA tracker API from :
http://dev.piwik.org/trac/browser/tags/1.6/libs/PiwikTracker/java

Why are you asking for?

@mattab commented on November 28th 2011 Member

rie, if you have found a bug in the JAVA tracker, please submit a patch to it (or the bug report) in the JAVA tracker ticket at: #2172 -- Thanks!

@anonymous-piwik-user commented on December 1st 2011

Hi, can you check again what I wrote ? I mean this bug is not for Java Tracker, but PHP.

We need to fix folloging php class & method.

-class : "Piwik_Tracker_Visit" (file:%PIWIK_HOME%/core/Tracker/Visit.php)
-method : getCustomVariables (line:1028)

Thanks a lot for your help!

@mattab commented on December 1st 2011 Member

Please submit a javascript code to replicate the issue you describe, along with the expected output / actual output

@anonymous-piwik-user commented on December 12th 2011

Hi Matt,

thanks a lot for your help.
I test with java-script tracker, and it worked fine with more than onecustom variable.
So we made sure that this problem comes from Java tracker.
I post the issue to the #2172 .

Thanks a lot!

This Issue was closed on December 12th 2011
Powered by GitHub Issue Mirror