Please note, I have included the entire VS 2005 project with this tutorial in the supplied attachment.   

In order to get your values to your database your going to need a server side language to talk to the db.  So we need to create a VS 2005 project that will be our asp.net web site.  Since this tutorial is more about interoperability between ASP.NET and Actionscript 2.0 im not going to go into great detail here, but I will provide a quick overview of what is needed.

 

  1. Create a web app that will house your flash movie.
  2. Create a page Separate from your flash movie display page, call it "FlashTrack.aspx"
  3. In the Code-behind file create the following private method:

 

 

/// <summary>

/// Logs the Activity to a Sql Database

/// </summary>

/// <param name="obj">The object being tracked</param>

/// <param name="act">The action to track</param>

private void TrackActivity(string obj, string act){

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TrackerDB"].ConnectionString))

{

//create a command element and the parametrs needed

SqlCommand cmd = new SqlCommand("INSERT INTO ActivityLog (Object, Activity, HitTime) VALUES (@Object, @Activity, @HitTime);", conn);

cmd.Parameters.AddWithValue("@Object", obj);

cmd.Parameters.AddWithValue("@Activity", act);

cmd.Parameters.AddWithValue("@HitTime", System.DateTime.Now);

try

   {

                 conn.Open();

                 cmd.ExecuteNonQuery();

                 conn.Close();

          }

          //if there is an error you can handle it here.

          catch (Exception ex)

          {

                 throw (ex);

          }       

}

}

 

  1. In the Page's Load Event Add the following Code:

//make sure something was sent

   if (Request.QueryString != null)

   {

        string myObj = "";

        string myAction = "";

        if (Request.QueryString["obj"] != null)

        {

             myObj = Request.QueryString["obj"].ToString();

        }

        if (Request.QueryString["act"] != null)

        {

            myAction = Request.QueryString["act"].ToString();

        }

        //Call to SQL Database

        TrackActivity(myObj, myAction);

       }


Basically what this does is the following:

 

  1. Check the querystring to make sure there are variables in it
  2. Puts the object and action into strings
  3. Sends the strings to a method designed to connect to an SQL Server Database and insert into a table named "ActivityLog" the following 3 parameters:
    1. Object
    2. Activity
    3. HitTime

That’s it really. Now all you have to do is put your flash movie on a page in your website, and add a page like our "FlashTrack.aspx" page to your site and you can be tracking all kinds of user behavioural data from your flash movies right into your SQL Server Databases.  Currently I use this type of tracking to track the following kinds of activities:

 

  • User Session starts (when a user first enters our site)
  • Clicks on products to "learn more" or "buy now"
  • Roll-overs we may have on movies that provide more product information
  • Log-in or Log-outs
  • Feedback clicks, or certain milestones you may have in your site.

You can even use this to send "silent" events to your site.  Because it uses LoadVariables it doesn't do anything to inform the user.  So you can be playing a video or commercial, or even song, and if it is in a flash format you could have actionscript events fire on certain key frames to log how long a person has been watching or listening to something in particular.

 

I have included the VS 2005 Project, the Flash Project, Flash file and Actionscript class file in the download.  I have also included scripts that will create the necessary SQL Server tables.  Once you have a utility class like this and learned the in's and out's of operating back and forth from flash you can create different pages to track different types of activities or send / receive data to and from flash.

 

Note about security:  Please note that since this is user behavioural collection you will need a terms and conditions page on your site somewhere and its always a good idea to include a privacy policy.   Also note that you will need to keep the asp.net and flash movie in the same site or else you will get Cross Site Security Violations.


Some Resources for further reading:

 

ASP.NET and the Querystring:

http://aspnet.4guysfromrolla.com/articles/020205-1.aspx

 

Integrating Flash with ASP.NET:

http://dotnetjunkies.com/Tutorial/986D1568-2686-435A-855A-5A95F25FFC71.dcik

 

LoadVariables and LoadVars:

http://www.actionscript.org/resources/articles/46/1/LoadVariables-and-LoadVars-Objects/Page1.html