On same Webpage, Custom Variables gets appended always to the previous ones

Q. How can we remove index information of previous tracking attempt’s
indexed information, from cvar,scope=‘page’ variable ?

Scenario :

I am working currently on project that includes Liferay Portal integrated
with Springs and hibernate.

There are two sections on single page

  1. Search section
  2. Browse section

Search section has one integrated button of Download. In download section,
we have applied tracking for few things by using ‘setCustomVariable’ of
piwik.js, and output of call to piwik.php comes like

cvar output(through HTTPFox) :

{“1”:[“Initiate Download”,“155231”],“3”:[“source”,“portal”]}


Now when we apply tracking through ‘setCustomVariable’ in Browse
section(on same page), we get

“1”:[“Initiate Download”,“155231”]…

appended to our new piwik.php call. Comes like :

{“1”:[“Initiate
Download”,“155231”],“3”:[“source”,“portal”],“9”:[“ASSET”,“37”]}


Expected Output :

{“9”:[“ASSET”,“37”]}

In next tracking attempt(Browse on same page where search has been done).


Tracking.js


function trackCustVar(index,name,val,type,title,callback)
{
if(typeof(piwikTrackingEnabled) != “undefined” && piwikTrackingEnabled==“true”) {
if(title!=false)
_paq.push([‘setDocumentTitle’,‘custom-’+title]);
_paq.push([‘setCustomVariable’,3,‘source’,‘portal’,‘page’]);
_paq.push([‘deleteCustomVariable’, 1, “page”]); // added this but getting blank strings for index 1.
_paq.push([“trackPageView”]);
_paq.push([‘setCustomVariable’,index,name,val,type,callback]);
if(typeof(userId) != “undefined” && typeof(ssoToken) != “undefined” && userId != “” && ssoToken !="") {
_paq.push([‘setCustomVariable’,1,‘userId’,userId,‘visit’]);
_paq.push([‘setCustomVariable’,3,‘ssoToken’,ssoToken,‘visit’]);
}
_paq.push([“disableCookies”]);
_paq.push([“trackPageView”]);
}
}



Search section/button’s java-script code :


if (filesToPrint.length == 0){
$("#btn_save").addClass(‘hide’);
} else {
$("#btn_save").off(‘click’);
$("#btn_save").removeClass(‘hide’).on(‘click’, function(){

					var downloadlist = '';
					checked_for_print.each(function(i,v){
						downloadlist +=$(v).attr("data-content-id")+',';
					});
					downloadlist = downloadlist.substr(0,downloadlist.length-1);
					       trackCustVar(1, 'Initiate Download', downloadlist , 'page', 'Document Tab');

					appPackage.modules.share.downloadTrigger(filesToPrint);

				}

cvar output(through HTTPFox) :

{“1”:[“Initiate Download”,“155231”],“3”:[“source”,“portal”]}



Browse section/button’s js code :


        $(".dropdown-menu-unstyled li a.listItemLabel").on("click", function() {
            var $this = $(this),
                assetid = [],
                url = '',
                data_column = parseInt($this.parents('.filter-col-browse').data('column')),
                perspective = $this.data('perspective');
            
            switch (data_column){
            case 1: assetid.push($this.data('conceptid'));
            		      trackCustVar (9, $this.data('perspective') , assetid[0] , 'page', 'Portal Filters');
                        url = 'some_URL';
                    break;
                    
            case 2: assetid.push($this.data('conceptid'));
                    assetid.push($this.parents('.filter-col-browse').siblings('div[data-column="1"]').find('li.active a:first-child').data('conceptid'));
                                 trackCustVar (9, $this.data('perspective') , assetid[1]  +';' + assetid[0] , 'page', 'Portal Filters');
                    if(perspective == "COUNTRY") {
                        url = 'some_URL';
                    } else {
                        url = 'some_URL';
                    }   
                    break;
            };
            
            window.location = url;
            appPackage.Navigation.startLoader();

cvar’s output :

{“9”:[“ASSET”,“37”]}


Problem.
 I want to remove ("1":["Initiate Download","155231"],"3":["source","portal"],) part from last output. It gets appended by default. and when we remove custom variable, the value of index still gets displayed with blank strings like ({"1":["",""],"3":["source","portal"],"9":["Initiate Download,551996","37"]}).