Last week I’ve been asked a few times how to use the google analytics code with a flash project, the solution is actually pretty simple but very effective, while google provides you with some “help content” it proves itself that the help page they have is quite hmm.. useless.. You don’t want to show the viewer of your application that you are sending data to google by opening a new webpage.. yuk..
- First it’s not how you should do it
- Second there are better solutions to achieve the same result without having pop-up’s appearing all over the place.
If you already know how to set up a Google Analytics profile jump to STEP5 .
1. Let’s start off by browsing to http://www.google.com/analytics and log-in with your Gmail account or sign up for a new account.
In this example I’m going to show you how to track the user clicks from a simple video chooser application.
As I’m not going to explain how to create the video application, I give you here the possibility to download the .fla I’ve created
5. Let’s open our gaTracking.fla document, there you’ll find 2 buttons and a black frame, all graphical objects are on a layer while the actions are on the top layer. Select the actions frame and press the F9 key to open the actionscript panel. You’ll find the actionscript for the video handling and button actions, if you test this fla you’ll see that everything is working as it should, you press Clip1 and the associated video is played.
Here a sample Google Analytics code in my HTML which you can also find on the downloadable sample file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
UA-XXXXXX-X needs to be replaced with the code that google assigned to your account.
7. Now let’s add the tracking options to our buttons. Go back to Flash, select the first frame containing the actions and create a new function right after the initClip() function:
1 2 3 4
the function expects one argument called trackingString the string can be whatever you want, just be aware that the string you passing will be the one that is going to show up at googleAnalytics report.
Everything inside the parentheses “()” can be user defined, in my case I’m using the root/ before the string so that I know that the flv is located at the root of the webserver.
Last but not least below the complete code used for this example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
This is it, actually the whole process is very simple, and sadly the support page of google analytics is not very well documented for people using actionscript 3.0, I hope I brought some light into this very important feature.
Have fun embedding those codes