Question

I am very very new to Javascript so i do apologise if its simple.

On my site I have lots of pages with lots of information on them, the info is split into sections in an accordion format.

I want to be able to track if someone engages with the expandable sections but I only need to know one click/Event per user at this moment in time.

I just need to know where i would put my code and what javascript i would need to write in order to track if someone clicks on a section then stops tracking once they have clicked.

In my head I am thinking of having script per expandable section but if someone clicks on one, how will the other sections know not to track any more.

An example is http://www.disabledgo.com/access-guide/tower-hamlets-council/tower-of-london

I hope someone is able to help.

Thanks

Was it helpful?

Solution

If you want to only track on thing you can add a Boolean value to a variable inside an if statement and test that for each event.

So in basic language

lets say you have a button.

<button id='b1'>my button<button>

if you want to only track one of the button clicks you can do something like this. Note: I used jQuery so you need to link to the api in your head tags.

var boolval = true;
$('#b1').click(function () {
    if (boolval) { 
        //alert('worked!'); for debugging
        _gaq.push(['category', 'action','lable','opt_interaction','value'])
        boolval = false;
    }
});

I added an if statement based on the variable boolval that I set to false after the first click. Then when you click again it checks it and comes up false and does not fire the function.

Here is a working jsfiddle http://jsfiddle.net/nicomiceli/L2Efh/

You can do this for your accordion menu. Set the event listener to your class and after the first click make something false so if they try it again it won't fire.

Let me know if you have any questions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top