# Keeping Track of Clicks with Shared Object

**URL:** https://forum.kirupa.com/t/keeping-track-of-clicks-with-shared-object/249154
**Category:** flash
**Created:** [January 15, 2008, 10:37pm UTC](https://forum.kirupa.com/t/keeping-track-of-clicks-with-shared-object/249154 "2008-01-15T22:37:02Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Pherank](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@Pherank](https://forum.kirupa.com/u/Pherank)
#### Post date: [January 15, 2008, 10:37pm UTC](https://forum.kirupa.com/t/keeping-track-of-clicks-with-shared-object/249154/1 "2008-01-15T22:37:02Z")

</div>

I’m trying to write a function that keeps a running count whenever a button is clicked - and then writes the count to a Shared Object cookie. Here’s the code:

```auto

function counterVar() {
    var elemSharedObject:SharedObject = SharedObject.getLocal("elementChoices", "/");
    countClicks = elemSharedObject.data.itemCount;
    if (elemSharedObject.data.itemCount == 'undefined' || '' || 'NaN') {
        elemSharedObject.data.itemCount = 1;
        elemSharedObject.flush();
        countClicks = 1;
    } else {
        countClicks++;
        elemSharedObject.data.itemCount = countClicks;
        elemSharedObject.flush();
    }
}

```

And the button code looks like this:

```auto

function click(bt:Object):Void {
    if (bt.target == table_mc.p1_bt) {
        var elemArray:Array = new Array(_root.p1, _root.i1, _root.e1, _root.en1);
        var elemArrayName = "elemArray_" + _root.countClicks;
        storeData(elemArrayName,elemArray);
        //Alert.show("You've added " + _root.i1 + " to the cart");
        counterVar();
    } //else if...
}

```

Everytime it runs the count gets reset to “1”, endlessly. Obviously the logic is flawed. Can anyone help out here?
