getCustomVariable conditions


#1

Hi!

I am a long time user of Piwik and have recently thought it might be a good idea to track some custom variables. I have a page where there is a JS variable somewhere in the head of the document. Since the variable is PHP generated based on a user prodile, I leave it where it is at the moment:


<script type="text/javascript">
  var gender = male;
</script>

I am using asynchronous tracking and Drupal CMS which has it’s own module for inserting Piwik code. It has possibilities to insert additional lines before or after “_paq.push([“trackPageView”])”. Since I am trying to insert some custom variables, I am injecting the code via the “before” box.

What I need to do:
[ol]
[li] get the variable value from a cookie
[/li][li] evaluate it using some an “if” condition
[/li][li] either set a new variable value or leave it as it is
[/li][/ol]


_paq.push([ function() {gender = ((this.getCustomVariable(1)[1] != "N/A" ) ? this.getCustomVariable(1)[1] : gender);}]);
_paq.push(["setCustomVariable", "1", "Gender", gender, "visit"]);

The first line gets a variable from a cookie and if its value is not “N/A”, it uses the value already set, otherwise uses the value of the “gender” variable. As a result, the variable “gender” should either have a new value or should have the already set value.
Then the second line inserts the value of “gender” into a cookie.

What happens now is that the value for “gender” is only set at the very beginning of my page. Once it reaches the getCustomVariable part of my code, it is evaluated only inside of the function but the global variable “gender” is never changed. Can I somehow manipulate the “gender” variable getting the custom variable from a cookie and evaluating afterwards? I tried dozens of combinations but I am unable to find a new to achieve this with asynchronous tracking.

The variable name and values are just examples so no need to point out that there might be some interferences. That I am aware of. Thank you for any help.


#2

Trying to debug the code I discovered the function for getting the value of a variable is fired after the cookie’s variable value gets set, not before and despite the fact the function for getting the custom variable value is placed before the setting part.

Is there an alternative way of getting the custom variable value?


(Matthieu Aubry) #3

Did you find a bug in piwik or in your code?