Quantcast

Saturday, November 8, 2008

Tracking Java Versions using Google Analytics

Update: This code does not work as expected (it will only report the newest JRE version). Please see the newer version here.

Ever wonder what Java version your website visitors have installed? Here's how you can leverage Google Analytics to find out:

  1. Locate the Google Analytics script in your HTML page (you must be using the new "ga.js" version). Here is what mine looks like:
    <script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
    var pageTracker = _gat._getTracker("UA-XXXXX-X");
    pageTracker._initData();
    pageTracker._trackPageview();
    </script>

  2. Insert the following code after that script:
    <script src='http://java.com/js/deployJava.js' type='text/javascript'></script>
    <script type='text/javascript'>
    var jreVersions = deployJava.getJREs();
    if (jreVersions.length==0)
    pageTracker._setVar("Java: none");
    for (var i=0; i<jreVersions.length; ++i)
    pageTracker._setVar("Java: " + jreVersions[i]);
    </script>

  3. Please note, if you are using Blogger you will need to encode the '<' character as "&lt;". Open up Error Console in your browser to double-check that the script is not generating any errors.
  4. Wait 24 hours for Google to update the report.
  5. View the updated data at Google Analytics -> Visitors -> User Defined.
  6. There are two ways to drill-down based on user-defined values:
    1. Advanced Segments -> New -> Dimensions -> Visitors -> User Defined. For example:
      1. Track users without Java: Matches Exactly: "Java: none"
      2. Track users with Java: Starts With: "Java:" and Does Not Match Exactly: "Java: none"
      3. Track users with Java 1.6: Starts With: "Java: 1.6"
      4. You can then check how many Flash users also had Java installed. Or how many Windows users had Java 1.6 installed. Or the connection speed of Java users.
    2. Visitors -> User Defined -> [Pick One] -> Dimension. For example, you can now click on Java: 1.6.0 -> Dimension -> Browser to find out what browsers visitors with Java 1.6.0 were using.
  7. Publish your results here: http://forums.java.net/jive/thread.jspa?messageID=317425