The Google Analytics API gives you read-only access to the data kept in a Google Analytics profile. Essentially every piece of information that can be displayed in the Google Analytics web interface is accessible through the API in the form of GData feeds.

If you want to consume the Google Analytics API from .NET you essentially have two options. You can use the GData .NET Client library (which doesn't yet officially list Google Analytics as a supported service, but if you browse the source you'll find it). Or you can roll up your sleeves and issue the HTTP GETs and POSTs yourself.

If you're doing the later you might find that this little bit of C# we created could save you some time.

using System;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Text;
using System.Web;
using System.Xml;

namespace GoogleAnalytics
{
   
public static class Client
   
{
      
public static string GetAuthorizationToken(string email, string password)
      {
         
using (var webClient = new WebClient())
         {
            
var nameValueCollection = new NameValueCollection();
            nameValueCollection.Add(
"Email", email);
            nameValueCollection.Add(
"Passwd", password);
            nameValueCollection.Add(
"service", "analytics");

            var response = webClient.UploadValues("https://www.google.com/accounts/ClientLogin", nameValueCollection);
            
var values = Encoding.Default.GetString(response).Split('\n');

            return values.First(value => value.StartsWith("Auth=")).Replace("Auth=", String.Empty);
         }
      }

      public static SyndicationFeed GetAccountFeed(string authorizationToken)
      {
         
using (var webClient = new WebClient())
         {
            webClient.Headers.Add(
"GData-Version", "2");
            webClient.Headers.Add(
"Authorization", String.Format("GoogleLogin Auth={0}", authorizationToken));

            var response = webClient.DownloadString("https://www.google.com/analytics/feeds/accounts/default");

            return SyndicationFeed.Load(XmlReader.Create(new StringReader(response)));
         }
      }

      public static SyndicationFeed GetDataFeed(string authorizationToken, string profileId, DateTime startDate, DateTime endDate, string metrics, string dimensions, string sort, int? maxResults)
      {
         
using (var webClient = new WebClient())
         {
            
var query = HttpUtility.ParseQueryString(String.Empty);
            query.Add(
"ids", profileId);
            query.Add(
"start-date", startDate.ToString("yyyy-MM-dd"));
            query.Add(
"end-date", endDate.ToString("yyyy-MM-dd"));
            query.Add(
"metrics", metrics);

            if (!String.IsNullOrEmpty(dimensions))
            {
               query.Add(
"dimensions", dimensions);
            }

            if (!String.IsNullOrEmpty(sort))
            {
               query.Add(
"sort", sort);
            }

            
if (maxResults.HasValue)
            {
               query.Add(
"max-results", maxResults.Value.ToString());
            }

            var uri = new UriBuilder("https://www.google.com/analytics/feeds/data");
            uri.Query = query.ToString();

            webClient.Headers.Add("GData-Version", "2");
            webClient.Headers.Add(
"Authorization", String.Format("GoogleLogin Auth={0}", authorizationToken));

            var response = webClient.DownloadString(uri.Uri.AbsoluteUri);

            return SyndicationFeed.Load(XmlReader.Create(new StringReader(response)));
         }
      }
   }
}

Shawn Miller

Think Globally, Tweet Locally

November 29, 20091:04 AM

Twitter recently launched their Geotagging API which allows third party Twitter apps to publish the location from where a tweet was posted.

We've had the ability to view a stream of tweets on Atlas for a while now. Tweets that match a search query (or the public timeline) are placed on the map based on what the Twitter user specified as their location in their Twitter profile.

The addition of geotagging to the Twitter API provides Atlas with the exact location from where a tweet was made. Now, if Atlas sees a tweet with a geotag we'll use it instead of the user's location specified in their Twitter profile.

As the Twitter Geotagging FAQ points out, "Geotaggging is disabled by default for everyone. You can enable it from the Settings>>Account tab".

The Google Closure Compiler is a tool for making JavaScript download and run faster. It is a true compiler for JavaScript. Instead of compiling from a source language to machine code, it compiles from JavaScript to better JavaScript. It parses your JavaScript, analyzes it, removes dead code and rewrites and minimizes what's left.

The best part of the Closure Compiler is its REST API. Here's 5 lines of C# that will optimize your JavaScript:

string js = "alert('Hello World');";

using (var webClient = new WebClient())
{
   webClient.Headers.Add(
"content-type", "application/x-www-form-urlencoded");
   
var postData = String.Format("js_code={0}&output_info=compiled_code", HttpUtility.UrlEncode(js));
   
js = webClient.UploadString("http://closure-compiler.appspot.com/compile", postData);
}

We switched JsFramework's JavaScript optimization from Yahoo's YUI Compressor to Google's Closure Compiler.

The Closure Compiler does a slightly better job optimizing our JavaScript than the YUI Compressor, saving about 1.4 KB. However, the real win is the Closure Compiler's REST API which we were able to get up and running in about four lines of C# code. The big advantage with the REST API is there's no JAR file to maintain/deploy and Google can keep the Closure Compiler API up to date as changes are made.

JsFramework unit tests are still passing after making the switch to Closure Compiler.

Shawn Miller

Map Your Kanye West Tweets

September 15, 200911:08 AM

From the Twitter Search FAQ, "There is an undeniable need to search, filter, and otherwise interact with the volumes of news and information being transmitted to Twitter every second. Twitter Search helps you filter all the real-time information coursing through our service."

We've recently upgraded our Twitter feature in Atlas to include search. Click the Twitter icon in Atlas (you'll find it under the "experimental" tab) and we'll begin by displaying tweets from the public timeline. Enter your own search term OR select a currently trending topic from the dropdown and Atlas will begin streaming search results as they happen.

Dr. Andrew Hudson-Smith of Digital Urban offers a word of advice, "Warning you could spend hours just watching this Twitter visualization using Atlas."

Pro tip: You can create your own Twitter search map URL using http://atlas.freshlogicstudios.com/?twitter={searchTerm}

Shawn Miller

About This Location

July 13, 20091:27 PM

In Atlas, if you right click any spot on the map (or click on any pushpin) you're greeted with a handy little menu. The last option in this menu we've named "about this location".

When you select "about this location" Atlas is going to do three things for you. First, it's going to show the latitude and longitude coordinates for the spot you've clicked. Next, it's going to display the readable street address for the spot you've clicked using a process called reverse geocoding. We've had this for a while now.

This next part is new. Atlas will now query Wikipedia for relevant articles near the spot you've clicked. If Atlas is able to locate a relevant article, it will display a short summary and provide a link to the full article on Wikipedia.

We've talked about Atlas & Wikipedia before, but we feel integrating Wikipedia articles into the "about this location" is a lot more useful.

Shawn Miller

Basic, Advanced, Experimental

July 10, 200912:34 AM

Tonight's Atlas improvements includes reorganizing the tab menus that used to read: start, features, feeds, and services. They now read: basic, advanced, and experimental.

We hope this new change makes it easier to discover new ways to use Atlas. If you have any other suggestions, we'd love to hear it.

Shawn Miller

JsFramework

July 08, 200910:57 PM
JsFramework is an object oriented JavaScript library with a programming model similar to the Microsoft .NET Framework.

We started this library a couple of years ago because we hated working with JavaScript's Date class. Back then we simply called the library "Scripts". Now that we've added more and more JavaScript implementations of the .NET Framework we decided to give it a new name; JsFramework.

JsFramework is meant to supplement jQuery and the Microsoft AJAX Library. If you'd like to work with things like DateTime.ToShortDateString(), String.Format(), and Uri.GetHost() in JavaScript you should glance at the examples and give it a shot.

The freshlogicstudios.com redesign is up. You're soaking in it. And right now many of you are thinking, "uh, it looks pretty much like it did back in 2007." You're right. It's old school.

So, what's new in this redesign?
Nothing really. We took a small break from project work to tidy things up around here. Don't freak out; it was time for us to remove some older portions of the site.

What happened to your gadgets?
We had a great time working with the Windows Live team and various groups inside Microsoft creating web gadgets for live.com. Sadly there doesn't appear to be much activity with web gadgets lately. If you're still using live.com as your homepage you can find our gadgets on our archived gadgets page or on the Windows Live Gallery.

What happend to your JavaScript library?
We moved it to jsframework.com. We think JsFramework is a much better name than Fresh Logic Studios Scripts; I'm sure you'll agree.

What about Atlas?
Same great Atlas. If you've not tried some of the more advanced features, you should give them a shot. You could map videos from youtube, track shipments, map twitter posts... or use it to fight fires.

Shawn Miller

Search Twitter With Atlas

March 02, 200910:02 PM

We still need to polish up the user interface, and it performs slower than we'd like, but tonight we were able to throw together a Twitter search visualization using Atlas. Try it out...

Search