Tracking Specific Domains in Google Analytics/Google Docs

February 1st, 2009  |  Published in ruby  |  3 Comments

Say you want to keep an eye on a given set of referrers in Google Analytics:




    #!/usr/bin/ruby

    require 'rubygems'

    require "google_spreadsheet"

    require 'rugalytics'  



    session = GoogleSpreadsheet.login("GOOGLE USERNAME", "GOOGLE PASSWORD")

    Rugalytics.login 'ANALYTICS USERNAME', 'ANALYTICS PASSWORD'  



    # the key is derived from the 'key' param in the URL, e.g.

    # http://spreadsheets.google.com/ccc?key=THE KEY&hl=en

    ws = session.spreadsheet_by_key("SPREADSHEET KEY").worksheets



    domains = [ARRAY OF DOMAINS]



    start_cell = 2



    @profiles = {'ACCOUNT' => 'PROFILE', 

                 'ACCOUNT2' => 'PROFILE2', 

                 }

 

       @profiles.each do | a,r |

           sheet_title = a

           sheet = ws.find { |s| s.title == sheet_title }

           profile = Rugalytics.find_profile(a,r)



    i = 12

      while i > 0 

          range_start = Date.today - i.months

          range_end = range_start + 1.months - 1.days

          report = profile.referring_sources_report :rows=>1000, :from => range_start, :to => range_end

          col = sheet.num_cols + 1

          report.items.each do |r|

             if social_domains.include?(r.source)

               row = social_domains.index(r.source) + 2

               sheet[1, col] = "#{range_start} - #{range_end}"

               sheet[row, col] = r.visits.to_i

               sheet.save

             end

         end

        i = i - 1 

      end

    end



    

It needs a little setup work up front: You need a Google Spreadsheet, and the first column needs to be populated with the domains you populate the domains array with. If you have more than one profile to track in Analytics, you need to create a worksheet with the name of each profile in your spreadsheet.

I’d rather be using the more full-featured roo to handle the spreadsheet part, but it isn’t playing nice with rugalytics at the moment. I wrote both the authors, so maybe something good will come of that.

Responses

  1. Colin Meyer says:

    February 2nd, 2009 at 9:37 am (#)

    Michael, I’ve been greatly enjoying reading about all the ruby coding you’ve been up to lately. You are giving a fantastic example of practical coding: automating tasks and/or arranging and exploring data that would be difficult to do otherwise. I’ve long felt that programming should be a basic skill taught in school, right along with reading, ‘riting and ‘rithmatic (‘rogramming?).

  2. mph says:

    February 2nd, 2009 at 10:09 am (#)

    Thanks, Colin.

    You hit one of the reasons I’ve been enjoying Ruby. The syntax is simple and forgiving in a way that really sticks with me, but with underlying power that AppleScript doesn’t have. It makes Ruby feel a lot like a “civilian” language to me. When I compare its syntax to things like I’ve seen in complex macros written for Word, it seems like any lazy power user type could pick it up.

    I remember talking to friends in the late ’90s who had come to Perl for the same reasons: They weren’t CS people and never would have bothered with a language like C, but Perl made it easy for them to have a point of entry into Web programming.

    I also love rubygems and the appreciate the people writing them. In that example above, out of the 42 lines of code:

    • 21 lines are basic setup
    • 21 lines are the code I had to write for myself.

    I ought to take a look at how many lines are involved in the google_spreadsheet and rugalytics gems, because it’s pretty amazing how much of the hard work of building connections to their respective APIs and retrieving objects goes on in something it takes me four lines to invoke and configure.

  3. In Which Mike Internalizes the Whole Class Thing :: dot unplanned says:

    February 2nd, 2009 at 8:42 pm (#)

    […] anyhow, another triumph of Rubygems married to really basic logic, though unlike this morning’s entry I actually contributed 200+ lines of code you can’t see through the CDEV […]

Leave a Response

© Michael Hall, licensed under a Creative Commons Attribution-ShareAlike 3.0 United States license.