Fetching Custom Variable Values using Piwik API

1.2k views Asked by At

I am trying to use this api CustomVariables.getCustomVariablesValuesFromNameId (idSite, period, date, idSubtable, segment = '') from piwik to fetch CustomVariable values. But there is no example for it and also how to get idsubtable value ?

Is there any way to fetch custom variable values from piwik using api ?

Any help or example would be appreciated.

1

There are 1 answers

0
halfdan On

Granted - this is not easy on first sight. So here's how it works:

You can find an example for CustomVariables.getCustomVariables at http://demo.piwik.org/index.php?module=API&action=listAllAPI&idSite=7&period=day&date=yesterday#CustomVariables. The resulting XML contains all existing Custom Variables.

Lets take the following as an example:

<result>
    <row>
        <label>Forum status</label>
        <nb_visits>593</nb_visits>
        <nb_actions>1571</nb_actions>
        <max_actions>40</max_actions>
        <sum_visit_length>116860</sum_visit_length>
        <bounce_count>389</bounce_count>
        <goals>
            <row idgoal='1'>
                <nb_conversions>5</nb_conversions>
                <nb_visits_converted>5</nb_visits_converted>
                <revenue>15</revenue>
            </row>
            <row idgoal='2'>
                <nb_conversions>9</nb_conversions>
                <nb_visits_converted>9</nb_visits_converted>
                <revenue>9</revenue>
            </row>
            <row idgoal='3'>
                <nb_conversions>2</nb_conversions>
                <nb_visits_converted>2</nb_visits_converted>
                <revenue>2</revenue>
            </row>
        </goals>
        <nb_conversions>16</nb_conversions>
        <revenue>26</revenue>
        <idsubdatatable>110</idsubdatatable>
    </row>
</result>

The <label> in this case stands for the key of the custom variable. The included metrics such as nb_visits, nb_actions, etc. are summed values for that specific custom variable. In order to get the values of the Forum status custom variable you need to use the value of idsubdatatable as the idSubtable parameter.

This results in the following URL: http://demo.piwik.org/index.php?module=API&method=CustomVariables.getCustomVariablesValuesFromNameId&idSite=7&period=day&date=yesterday&format=xml&idSubtable=110&token_auth=anonymous

Calling this URL will yield something like the following result:

<result>
    <row>
        <label>Anonymous</label>
        <nb_visits>544</nb_visits>
        <nb_actions>1185</nb_actions>
        <max_actions>38</max_actions>
        <sum_visit_length>83459</sum_visit_length>
        <bounce_count>380</bounce_count>
        <goals>
            <row idgoal='1'>
                <nb_conversions>5</nb_conversions>
                <nb_visits_converted>5</nb_visits_converted>
                <revenue>15</revenue>
            </row>
            <row idgoal='2'>
                <nb_conversions>9</nb_conversions>
                <nb_visits_converted>9</nb_visits_converted>
                <revenue>9</revenue>
            </row>
        </goals>
        <nb_conversions>14</nb_conversions>
        <revenue>24</revenue>
    </row>
    <row>
        <label>LoggedIn user</label>
        <nb_visits>49</nb_visits>
        <nb_actions>386</nb_actions>
        <max_actions>40</max_actions>
        <sum_visit_length>33401</sum_visit_length>
        <bounce_count>9</bounce_count>
        <goals>
            <row idgoal='3'>
                <nb_conversions>2</nb_conversions>
                <nb_visits_converted>2</nb_visits_converted>
                <revenue>2</revenue>
            </row>
        </goals>
        <nb_conversions>2</nb_conversions>
        <revenue>2</revenue>
    </row>
</result>

Each row's <label> contains a value for the Forum status custom variable and the metrics for that value.