Custom variables not set in the conversion when using JS tracking API


#1

Hi,

I have some problems with custom variables (with “page”- scope) and the goal conversions. It doesn’t work for me :slight_smile:

There aren’t any conversions for the custom variables. There are conversions for e.g. the country.

To reproduce the bug, I used a fresh installation with the latest code. I defined a goal, which must be triggered manually and which can be converted multiple times per visit.

I trigger that goal with javascript, some time after the page loaded (eg. the user made some interactions like pressing a play button or something else).

My HTML for testing (which simulates my needs):


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Test</title>
</head>
<body>

    <!-- Piwik -->
    <script type="text/javascript">
        var pkBaseURL = (("https:" == document.location.protocol) ? "https://192.168.57.171/" : "http://192.168.57.171/");
        document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
        var logit = function() {
            try {
             var piwikGoalTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
             piwikGoalTracker.trackGoal(1);
             }
             catch( err ) {}
        };

        try {
        var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
        piwikTracker.setCustomVariable(
            1,
            "Test",
            "testit",
            "page"
        );
        piwikTracker.trackPageView();
        piwikTracker.enableLinkTracking();
        window.setTimeout("logit()", 5000)
        } catch( err ) {}
    </script>
    <noscript><p><img src="http://192.168.57.171/piwik.php?idsite=1" style="border:0" alt="" /></p></noscript>
    <!-- End Piwik Tracking Code -->
</body>
</html>

Thanks,
panicskillingme

PS: The custom variable columns in the database table “log_conversion” are all empty.


#2

Me again.

I looked into the code and tried to fix the problem. I changed the JS tracking code to the following:


var logit = function() {
            try {
             var piwikGoalTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
             piwikGoalTracker.setCustomVariable(
                 1,
                 "Test",
                 "testit",
                 "page"
             );
             piwikGoalTracker.trackGoal(1);
             }
             catch( err ) {}
        };

        try {
        var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);

        piwikTracker.trackPageView();
        piwikTracker.enableLinkTracking();
        window.setTimeout("logit()", 2000)
        } catch( err ) {}

I looked at the request with firebug. The parameter cvar is set ({“1”:[“Test”,“testit”]}). But because it’s not within the “visit”-scope, it won’t be logged.

In core/Tracker/Visit.php on line 142 only the custom variables with the scope “visit” will be recognized. So it’s not possible to get custom variables within the “page” scope to be logged with the goal.

Can you verify this?

If I add the following code (line 143-145), it works. But I think this will break other things.


                if(empty($this->visitorCustomVariables))
                {
                        $this->visitorCustomVariables = self::getCustomVariables($scope = 'page', $this->request);
                }


(Matthieu Aubry) #3

it’s not possible to get custom variables within the “page” scope to be logged with the goal.
That’s correct, by design Piwik does not create a page view for Goals conversions, therefore there is no Custom Variable of scope “page” but only scope “visit”