Permalink
Browse files

work for Rugalytics on Rugrat greasemonkey script

  • Loading branch information...
1 parent b9c945a commit 9d951b6c376022e9783c9341eb5fd6dc8e19977c @robmckinnon committed Dec 2, 2008
Showing with 278 additions and 6 deletions.
  1. +1 −0 Manifest
  2. +35 −0 README
  3. +35 −0 README.rdoc
  4. +3 −0 lib/rugalytics/profile.rb
  5. +165 −0 lib/rugalytics/rugrat.user.js
  6. +39 −6 lib/rugalytics/server.rb
View
@@ -6,6 +6,7 @@ lib/rugalytics/item.rb
lib/rugalytics/profile.rb
lib/rugalytics/report.rb
lib/rugalytics/server.rb
+lib/rugalytics/rugrat.user.js
lib/rugalytics.rb
LICENSE
Manifest
View
@@ -234,6 +234,41 @@ automatically, e.g.:
top_items_over_week = report.items.sort_by{|i| i.unique_pageviews.to_i}.reverse
+==Rugalytics on Rugrat - a Greasemonkey script for Firefox
+
+TO INSTALL:
+
+Create a rugalytics.yml config file containing:
+
+ ---
+ account: your_account_name
+ profile: your_profile_name
+ username: your_user_name
+ password: your_pass_w
+
+
+Run rugalytics executable on console in same directory as rugalytics.yml:
+
+ > rugalytics
+
+
+Add Greasemonkey to Firefox: https://addons.mozilla.org/firefox/748/
+
+Go to: http://localhost:8888/
+
+Add rugrat user script: http://localhost:8888/rugrat.user.js
+
+Configure site for rugrat to run on:
+
+ Tools -> Greasemonkey -> Manage User Scripts
+ -> select rugrat -> press "Add"
+ -> add your site, e.g. http://your_site.com/*
+ -> press "Close"
+
+Browse your site!
+
+
+
==Acknowledgements
Rugalytics started life as a fork of jnunemaker's Statwhore. As the code and
View
@@ -234,6 +234,41 @@ automatically, e.g.:
top_items_over_week = report.items.sort_by{|i| i.unique_pageviews.to_i}.reverse
+==Rugalytics on Rugrat - a Greasemonkey script for Firefox
+
+TO INSTALL:
+
+Create a rugalytics.yml config file containing:
+
+ ---
+ account: your_account_name
+ profile: your_profile_name
+ username: your_user_name
+ password: your_pass_w
+
+
+Run rugalytics executable on console in same directory as rugalytics.yml:
+
+ > rugalytics
+
+
+Add Greasemonkey to Firefox: https://addons.mozilla.org/firefox/748/
+
+Go to: http://localhost:8888/
+
+Add rugrat user script: http://localhost:8888/rugrat.user.js
+
+Configure site for rugrat to run on:
+
+ Tools -> Greasemonkey -> Manage User Scripts
+ -> select rugrat -> press "Add"
+ -> add your site, e.g. http://your_site.com/*
+ -> press "Close"
+
+Browse your site!
+
+
+
==Acknowledgements
Rugalytics started life as a fork of jnunemaker's Statwhore. As the code and
@@ -94,6 +94,9 @@ def convert_options_to_uri_params(options)
params[:seg] = '1'
params[:segkey] = options[:segment] ? options[:segment] : 'organization'
end
+ if options[:value]
+ params[:gval] = options[:value]
+ end
params
end
@@ -0,0 +1,165 @@
+// ==UserScript==
+// @name rugrat
+// @namespace github.com/robmckinnon/rugalytics
+// @description a mousehole to google analytics data
+// @include http://your_website/*
+// ==/UserScript==
+
+window.Rugrat = function() {;}
+
+window.Rugrat.prototype = {
+
+ replace_all: function(text, old, replace) {
+ while(text.indexOf(old) > 0) { text = text.replace(old,replace); }
+ return text;
+ },
+
+ keyword_grab: function(keywords, keyword_id, segment) {
+ var the_url = "http://localhost:8888/keyword_detail?keywords="+keywords+"&segment="+segment;
+ GM_log(the_url);
+ var base = this;
+ GM_xmlhttpRequest({
+ method:"GET",
+ url: the_url,
+ headers:{
+ "User-Agent":"Mozilla/5.0",
+ "Accept":"application/json"
+ },
+ onload:function(response) {
+ var status = base.replace_all(response.statusText,' ','');
+ if (status == 'OK') {
+ var json = response.responseText;
+ var data = eval('(' + json + ')');
+ base.display_keywords(data, keyword_id, segment);
+ }
+ }
+ });
+ },
+
+ display_keywords: function(data, keyword_id, segment) {
+ var items = data.items;
+ var comma = '';
+ for (var i in items) {
+ var item = items[i];
+ var name = 'red';
+
+ if (segment == 'city') {
+ name = item.city;
+ } else {
+ name = item.network_location;
+ }
+ if (segment == 'city' || (name.indexOf('mini') >= 0 || name.indexOf('trea') >= 0) ) {
+ var element = document.getElementById(keyword_id);
+ var html = element.innerHTML;
+ element.innerHTML = html + comma + ' <small>'+ name +'</small>';
+ comma = ',';
+ }
+ }
+ },
+
+ data_grab: function() {
+ var pathname = window.location.pathname;
+ var the_url = "http://localhost:8888/top_content_detail_keywords?url="+pathname;
+ GM_log(the_url);
+ var base = this;
+ GM_xmlhttpRequest({
+ method:"GET",
+ url: the_url,
+ headers:{
+ "User-Agent":"Mozilla/5.0",
+ "Accept":"application/json"
+ },
+ onload:function(response) {
+ var status = base.replace_all(response.statusText,' ','');
+ if (status == 'OK') {
+ var json = response.responseText;
+ var data = eval('(' + json + ')');
+ base.display_data(data);
+ }
+ }
+ });
+ },
+
+ grab_unique_page_views: function() {
+ var pathname = window.location.pathname;
+ var the_url = "http://localhost:8888/top_content_detail?value=unique_pageviews&url="+pathname;
+ GM_log(the_url);
+ var base = this;
+ GM_xmlhttpRequest({
+ method:"GET",
+ url: the_url,
+ headers:{
+ "User-Agent":"Mozilla/5.0",
+ "Accept":"application/json"
+ },
+ onload:function(response) {
+ var status = base.replace_all(response.statusText,' ','');
+ if (status == 'OK') {
+ var json = response.responseText;
+ var data = eval('(' + json + ')');
+ base.display_unique_page_views(data);
+ }
+ }
+ });
+ },
+
+ display_unique_page_views: function(data) {
+ var visits = data.unique_pageviews_graph.points;
+ var max = Math.max.apply( Math, visits) * 1.2;
+ var points = visits.join(',');
+ var sparkline = 'http://chart.apis.google.com/chart?chs=200x40&cht=ls&chds=0,' + max + '&chco=0077CC&chm=B,E6F2FA,0,0,0&chls=1,0,0&chd=t:'+points;
+ var img = '<img style="vertical-align: middle;border: none;width:200px; height:40px;" src="' + sparkline + '"></img>';
+
+ var element = document.getElementById("r_unique_page_views");
+ element.innerHTML = img + ' ' + data.unique_pageviews_total;
+ },
+
+ display_data: function(data) {
+ var lines = '';
+
+ lines += '<h3 style="margin-top: 0.5em;">Unique Page Views</h3>';
+ lines += '<span id="r_unique_page_views" style="width:200px; height:40px;"></span>';
+ lines += '<h3 style="margin-top: 0.5em;">Entrance keywords</h3>';
+
+ for (var i in data.items) {
+ var item = data.items[i];
+ var url = '';
+ var keywords = item.keyword;
+ lines += '<p id="x' + i + '" style="margin : 0.5em; font-size: 0.75em"><a href="'+url+'" style="text-decoration: none;">' + keywords + '</a> (' + item.unique_pageviews + ')</p>';
+ }
+
+ var menudiv = document.createElement('div');
+ menudiv.style.position = 'fixed';
+ menudiv.style.top = '5px';
+ menudiv.style.left = '55%';
+ menudiv.style.padding = '10px';
+ menudiv.style.backgroundColor = '#fff';
+ menudiv.style.border="2px solid";
+ menudiv.zIndex = '3';
+ // menudiv.style.display='none';
+
+ var menuobj = document.createElement('div');
+ menuobj.innerHTML = lines;
+ menudiv.appendChild(menuobj);
+
+ var content = document.getElementById('content');
+ content.appendChild(menudiv);
+
+ this.grab_unique_page_views();
+ for (var i in data.items) {
+ var keywords = data.items[i].keyword;
+ this.keyword_grab(keywords, 'x'+i, 'city');
+ this.keyword_grab(keywords, 'x'+i, 'organization');
+ }
+ },
+
+ init: function() {
+ this.data_grab();
+ }
+};
+
+window.addEventListener("load", function () {
+ var ahole = new window.Rugrat();
+ ahole.init();
+}, false);
+
@@ -10,25 +10,58 @@ def initialize
def call(env)
path = env['PATH_INFO'].tr('/','')
- request = Rack::Request.new(env)
- report_name = (path + '_report').to_sym
- send_data(report_name, request.GET.symbolize_keys)
+
+ if path.empty?
+ html = %Q|
+ <h1>Rugalytics on Rugrat</h1>
+ <p>TO INSTALL:</p>
+ <p>Add Greasemonkey to Firefox: <a href="https://addons.mozilla.org/firefox/748/">https://addons.mozilla.org/firefox/748/</a></p>
+ <p>Add rugrat user script: <a href="http://localhost:8888/rugrat.user.js">rugrat.user.js</a></p>
+ <p>Configure site for rugrat to run on:</p>
+ <p><code>Tools -&gt; Greasemonkey -&gt; Manage User Scripts</code><br/>
+ <code>-&gt; select rugrat -&gt; press "Add" -&gt; add your site, e.g. http://your_site.com/* -&gt; press "Close"</code></p>
+ <p>Create a rugalytics.yml config file containing:</p><pre>
+---
+account: your_account_name
+profile: your_profile_name
+username: your_user_name
+password: your_pass_w
+
+</pre>
+ <p>Run rugalytics on console in same directory as rugalytics.yml:</p>
+ <p><code>&gt; rugalytics</code></p>
+ <p>Browse your site!</p>|
+ [200, {'Content-Type' => "text/html"}, html]
+ elsif path == 'rugrat.user.js'
+ [200, {'Content-Type' => "application/json"}, File.new(File.dirname(__FILE__)+'/rugrat.user.js').readlines ]
+ else
+ request = Rack::Request.new(env)
+ report_name = (path + '_report').to_sym
+ send_data(report_name, request.GET.symbolize_keys)
+ end
end
def send_data report_name, params
key = (params.values << report_name).join('')
@reports[key] ||= @profile.send(report_name, params)
report = @reports[key]
+
json = report.attributes.to_json
- json = add_in_front params, json
- json = add_in_front({:report_name => report.name}, json)
+ add_in_front params, json
+ report.attribute_names.each do |name|
+ if name[/(.*)pageviews_graph/]
+ total = "#{$1}pageviews_total".to_sym
+ add_in_front({total => report.send(total)}, json)
+ end
+ end
+ add_in_front({:report_name => report.name}, json)
[200, {'Content-Type' => "application/json"}, json ]
end
private
def add_in_front hash, json
- json.sub('{', hash.to_json.chomp("}") + ', ' )
+ json.sub!('{', hash.to_json.chomp("}") + ', ' ) unless hash.empty?
end
end
end

0 comments on commit 9d951b6

Please sign in to comment.