URL argument to custom variable


#1

Hi,

First, let me apologize for my english and for my limited skills in Piwik and Javascript programmation.

I’m working on a website in french (basic URL) and with an english version in which the URL contains an argument (the chain “lang=en”).

I was wondering if it was possible to add a custom variable in the javascript piwik tracker combine with a conditionnal test on the URL that will permit to add the language information on the page informations. Would something like that would work :


if (pkBaseURL.match("lang=en")) {
      piwikTracker......
}

Thanks in advance for your help.


#2

Anyone ? Help me please :slight_smile:

Maybe I haven’t been enough clear about what I want. Let me know if you want more details.

See you.


#3

As you say, it should work without any problem.


(JulienM) #4

You are on the right track, have a look at Custom Variables Analytics - Analytics Platform - Matomo and http://piwik.org/docs/javascript-tracking/#toc-custom-variables for more info.


#5

Hi,

Ok, I’ve written a piece of code using the Piwik custom variables documentation but I must have misunderstood something because nothing appears into Piwik.

Here is the code (the part that interrested us) :


try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
var turl = document.URL;
if (turl.match("lang=en")) {
	piwikTracker.setCustomVariable(
		1,
		"Langue",
		"Anglais",
		"page"
	);
}
else {
	piwikTracker.setCustomVariable(
		1,
		"Langue",
		"Français",
		"page"
	);
}
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
}

In the “Action” tab in Piwik I see this custom variable nowhere. What have I done wrong ?

Thanks :slight_smile:


(JulienM) #6

Is it on purpose that you wish to track the language on each page view instead of the whole visit ?

Which version of Piwik are you using ?


#7

I guess I could track the language for the whole visit ; I’m going to change that in my code.

For the version of Piwik, I’m not sure, I just know that it’s not the 1.5 version ; all I can get, is in the piwik.js file that indication :


 * @version $Id: piwik.js 2967 2010-08-20 15:12:43Z vipsoft $

EDIT :
I’ve modified the tracking code into this :


try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
var turl = document.URL;
if (turl.match("lang=en")) {
	piwikTracker.setCustomVariable(
		1,
		"Langue",
		"Anglais",
		"visit"
	);
}
else {
	piwikTracker.setCustomVariable(
		1,
		"Langue",
		"Français",
		"visit"
	);
}
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
}

But it doesn’t seem to work better :confused:

It also seems that since I’ve added my piece of code with the custom variable, none of the visits are registered / tracked by Piwik.

Do you have any idea ?

Thanks.


(JulienM) #8

Can you output the result of this API call from your browser ? :
http://YOUR_SERVER_URL/index.php?module=API&method=ExampleAPI.getPiwikVersion&format=xml&token_auth=YOUR_TOKEN_AUTH

Also, do you see any javascript error on the page being tracked ? In firefox you can see if there are errors on the page using Firebug or by pressing Ctrl + Shift + J.


#9

Hi,

The version of Piwik seems to be the 1.0.

I don’t see any errors with Firebug ; you can visit this page for an example :
http://geo.culture-en-limousin.fr/Le-Salon-des-berces-Sur-les-trois

Thanks.


(JulienM) #10

Custom variables are available starting from Piwik 1.2 see Piwik 1.2: Custom Variables, First party cookies, and more - Analytics Platform - Matomo


#11

Sh**… I’m so sorry… Unfortunately, I’m not the administrator so I don’t know if the upgrade will be done.

Are they any known problems upgrading Piwik from 1.0 to 1.5 ?

Thanks.


(JulienM) #12

Updating from Piwik 1.0 is highly advisable because of security related fixes Professional Security Audit for Piwik 1.1 - Analytics Platform - Matomo

be sure to follow Update - Analytics Platform - Matomo to the letter


#13

Ok, I’ll see with our administrator. Thanks for the advice.

I’ll come back when done and give an update on my attempt.

Thanks again.


#14

Hi,

Piwik has been updated to 1.5 and my javascript code is working : I’ve got statistics about the language used by visitors.

I tried also to add a custom variable with the scope “page” :


var turl = document.URL;
if (turl.match("lang=en")) {
	piwikTracker.setCustomVariable(
		1,
		"Langue",
		"Anglais",
		"visit"
	);
	piwikTracker.setCustomVariable(
		1,
		"Langue de la page",
		"Anglais",
		"page"
	);
}
else {
	piwikTracker.setCustomVariable(
		2,
		"Langue",
		"Français",
		"visit"
	);
	piwikTracker.setCustomVariable(
		2,
		"Langue de la page",
		"Français",
		"page"
	);
}

But it seems that I didn’t really understand how it works : I thought it will give me the count of pages in each language but it seems it’s not exactly what it does. Could you confirm or tell me if it’s possible to achieve what I want ?

Thanks in advance.


#15

Please, help me understand the way to track the total count of page views with custom variable, what did I do wrong in my code ?

Thanks.


(Matthieu Aubry) #16

Custom Variables per page will be reported as a simple overall count, available in Visitors > Custom Variables.


#17

Ok, so I need to change the index to 3 and 4 when I use the page scope because now I’ve got the same statistics for the two custom variables (as you can see in the picture attached) ?

Thanks.