ActionScript 3 Tutorial, Using Shared Objects to Show a Visitor's Browsing Time

By Community Member FlashKit

Flash developer Paul Flashdaweb from the FlashKit Forums wrote this ActionScript 3 tutorial which will show you how a SharedObject can be used to show the time a user has been visiting a page on your site--even after they have closed the page.

Let's Get Started

For illustration, I came up with an idea to show users the time they spend browsing a particular page--and it works even after they have closed and reopened the page. As an example of what I am referring to, open this demo page and check out the timer, then close it and open again anytime you want. You will notice that the time will not start again from 0:0, it will continue counting from the values you saw when you previously closed it. Quite nice, isn’t it?

ActionScript Example: Try It Yourself

The above functionality was implemented using Adobe Flash CS3, ActionScript 3 and SharedObject. You may download the source .fla and .as files to work with them on your own.

The Power of Shared Objects

For this tutorial I assume you already know the basics of AS3 and are quite acquainted with the Flash CS IDE. I created a new document and added a new movie clip called "mainObject". I put one instance of it on the stage and linked it to the ActionScript class SharedObjectReader.as.

In our class, we have a member of shared object:

private var _sharedObj:SharedObject;
In the construction of our class, we create children and start the ActionScript timer-based cycle using setInterval.
public function SharedObjectReader ()
{
createChildren();

//load and update the correct values
onEverySec();

//update the values every second
setInterval(onEverySec,1000);
}
In the onEverySec method, we bind our _sharedObj to a local instance with the static method getLocal(), put it in a try-catch block to handle the exceptions. TotalTimeAtThisPage is the name of a Shared Object, but you may use anything in there.
try
{
_sharedObj = SharedObject.getLocal(”TotalTimeAtThisPage”);
}
catch (error:Error)
{
trace(”SharedObject Error:”+error.toString());
return;
}
When using an initialized shared object, we read the values from it, accessing them via a "data" member
//read the values, if user had already been here, he has them already.
_secs = _sharedObj.data.seconds;
_mins = _sharedObj.data.minutes;
Note, "seconds" and "minutes" are my own variable names. You may use any of them here, but remember that you should use the same names when saving the values.

Now we increase the values, format them and update the text which is displayed. And then we store updated values in the same shared object member _sharedObj.

//store the values
_sharedObj.data.seconds = _secs;
_sharedObj.data.minutes = _mins;
Call flush() to save them to hard disk
//saving the values
_sharedObj.flush();
Finish the iteration with the close() method calling. It is a must to call this method otherwise it may cause a crash of the Flash IDE or other problems.
_sharedObj.close();
Well, that's it! ActionScript SharedObjects are not that tough to learn, but they are very helpful for the developer!

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •