Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking JSON calls with Google Analytics in a Ruby/Rails app

I searched a lot about this issue but I can't find anything good. I think that this is a common problem: you have a web app and build JSON APIs on top of your platform, so you can develop some widgets, mobile apps or third party applications.

I know that there a are a lot of specific analytics services via API (like Mixpanel, Kissmetrics and many others) but I want to track all the JSON calls also via Google Analytics.

I found that the best method is to use the __utm.gif image but strangely I cannot find any plugin or gem to use this image. So I tried to build my own method without success (see the code below)... Someone can help?

def google_analytics_call(page_title)
      today = Time.now.to_i
      utma = cookies[:__utma].to_s
      utmb = cookies[:__utmb].to_s
      utmc = cookies[:__utmc].to_s
      utmz = cookies[:__utmz].to_s
      utma = (rand(8).to_s + "." + rand(10).to_s + "." + today.to_s + "." + today.to_s+ "." + today.to_s) unless cookies[:__utma]
      utmb = rand(8).to_s unless cookies[:__utmb]
      utmc = rand(8).to_s unless cookies[:__utmc]
      utmz = rand(8).to_s+ "." + today + ".2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)" unless cookies[:__utmz]

      Thread.new do
        params = {
          :utmac => GOOGLE_ANALYTICS,
          :utmcc => "__utma%3D"+utma+"%3B%2B"+"__utmb%3D"+utmb+"%3B%2B"+"__utmc%3D"+utmc+"%3B%2B"+"__utmz%3D"+utmz+"%3B%2B",
          :utmcn => "1",
          :utmcs => "-",
          :utmdt => page_title, #page title
          :utmhn => BASE_URL,
          :utmwv => 1,
          :utmt => "page",
          :utmr => request.referer
        }
        puts params

        http_get("www.google-analytics.com", "/__utm.gif", params)
      end
    end
like image 522
zetarun Avatar asked Nov 28 '10 14:11

zetarun


People also ask

Can Google Analytics track API calls?

After installing GoogleAnalyticsTracker into your project, you currently have two options to track your API calls: use the Tracker class or use the included ASP.NET MVC Action Filter. Here's a quick demo of using the Tracker class: 1 Tracker tracker = new Tracker("UA-XXXXXX-XX", "www.example.org"); 2 tracker.

How do I add Google Analytics to Rails app?

Create a Google Tag Manager account Create a "web" type container. Create a New Tag, select "Google Analytics: Universal Analytics" Track Type: Page View, Google Analytics Settings: New Variable and add in your Google analytics property ID UA-123456789-1. Add a trigger for All Pages with type Page View. Save the Tag.


1 Answers

i think you want to use the async tracker, which is part of google analytics. Here is an example of how I track clicks that fire an async AJAX event:

$('.trackable').bind('click', function(event){
              var action = $(this).attr('action');
              var category = $(this).attr('category');
              _gaq.push(['_trackEvent', action, category]);
            });

The "_gaq" is the google asynchronous queue and can be added to any JS function you want to track metrics within. The function above allows you to add a class called "trackable" and track events when the user clicks on it. An example looks like this:

<a category='FEATURED' action='Like This' class='trackable' href="http://MYSSERVER.com/foo.json" target='_blank'>track asynchronously.</a>
like image 91
heavysixer Avatar answered Nov 15 '22 04:11

heavysixer