<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Daniel Eneström &#187; Google Analytics</title>
	<atom:link href="http://enestrom.com/tag/google-analytics/feed/" rel="self" type="application/rss+xml" />
	<link>http://enestrom.com</link>
	<description>Keep It Simply Stupid</description>
	<lastBuildDate>Fri, 23 Dec 2011 00:50:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Google Analytics and Flex using ExternalInterface</title>
		<link>http://enestrom.com/20081114/google-analytics-and-flex-using-externalinterface/</link>
		<comments>http://enestrom.com/20081114/google-analytics-and-flex-using-externalinterface/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 10:17:09 +0000</pubDate>
		<dc:creator>daniel</dc:creator>
				<category><![CDATA[Adobe Flex]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[ExternalInterface]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Google Analytics]]></category>

		<guid isPermaLink="false">http://blog.enestrom.com/?p=64</guid>
		<description><![CDATA[A big issue for people creating flash sites is getting the site to work well with Google Analytics. &#8220;The page doesn&#8217;t refresh. How can I track the clicks?&#8221; Well, it is actually very easy. If you look at the trace &#8230; <a href="http://enestrom.com/20081114/google-analytics-and-flex-using-externalinterface/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://enestrom.com/20081114/google-analytics-and-flex-using-externalinterface/' addthis:title='Google Analytics and Flex using ExternalInterface ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>A big issue for people creating flash sites is getting the site to work well with Google Analytics. &#8220;The page doesn&#8217;t refresh. How can I track the clicks?&#8221;</p>
<p>Well, it is actually very easy. If you look at the trace script Google Analytics gives you to add to your HTML code you can find a call to a method that actually records the event. This method is simple to call using JavaScript.</p>
<p>I have solved it like this in my latest Flex App (which is a public site). NOTE: This is for the new trace code version.</p>
<p><strong>1. Paste the Google Analytics trace code as usual just before the &lt;/BODY&gt; tag.</strong><br />
Check your Google Analytics account for the correct code.</p>
<p><strong>2. See to it that your embedded flash works with ExternalInterface.</strong><br />
This can be a bit tricky, but in my experience the things that do the trick are to change <em>allowScriptAccess</em> to <em>always</em> and inside the Flex App call a custom JavaScript function on <em>creationComplete</em> like so: <em>ExternalInterface.call(&#8216;initFlash&#8217;)</em>. In my html this initFlash function creates a variable reference to the embedded flash. This sort of &#8220;creates the connection&#8221; between them. I&#8217;m not sure why this is so, but for me it works, so I&#8217;m happy with that. If there is a need I would be glad to create a more thorough tutorial on the use of ExternalInterface. Just let me know.</p>
<p><strong>3. Create a custom JavaScript that passes the URL you want to register to the Google Analytics script.</strong><br />
This is not necessary, but I have found it easier to work with, as you don&#8217;t need to edit you call from inside of Flex if something changes in the Google code or such.</p>
<pre lang="javascript">function trackURL(url)
{
    pageTracker._trackPageview(url);
}</pre>
<p><strong>4. Call your custom javascript from within Flex.</strong><br />
I created a static class for this. (I love static classes). I named it <em>Analytics.as</em> and placed it in the root source folder in the Flex App. It looks like this. All it does really is call the JavaScript using ExternalInterface, but putting it within a static class lets you call it from anywhere in your application without having to pass on references to this or that object or function.</p>
<pre lang="actionscript">package
{
   public class Analytics
   {
      import flash.external.ExternalInterface;

      public static function track(url:String) : void
      {
         ExternalInterface.call("trackURL", url);
      }
   }
}</pre>
<p>And anywhere in your app write:</p>
<pre lang="actionscript">
Analytics.track('/path_to_tha_page_you_want_to_track/');
</pre>
<p>(NOTE: You have to start your path with a slash).</p>
<p><strong>There you go.</strong> It now should track the URL:s you want and give you nice statistics.</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://enestrom.com/20081114/google-analytics-and-flex-using-externalinterface/' addthis:title='Google Analytics and Flex using ExternalInterface ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://enestrom.com/20081114/google-analytics-and-flex-using-externalinterface/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

