Today’s Gotcha: Dynamic data in Rails scopes

I recently got caught out by an aspect of ActiveRecord scopes that I had totally forgotten about.

Lets say you are building a library book tracking system in Rails and you want to know which books are due today. You might be tempted to write something like this

class Book < ActiveRecord::Base
  scope :due, where('due_date = ?', Date.today)
end

That looks nice, and if you are not paying attention then you might think that Book.due would return all the books due that day.

You’d be (mostly) wrong.

A scope is defined at initialisation, so what you start up your server, console, or in my case the DelayedJob worker, Date.today gets executed once and a scope is created called due that will return all the Books that are due on the day you start the process.

So how do you create a scope that does what we want?  There are two approaches you could take…

1) Lambdas

You can pass a lambda to scope which will be executed at runtime, not at initialisation time…

scope :due, lambda { where('due < = ?', Date.now) }

2) Don’t use scopes

Scopes are nice, but as the likes of ‘where’ return ActiveRecord::Relation objects, just like a scope, we can replace our scopes with ordinary class methods…

class << self
  def self.due
    where('due = ?', Date.today)
  end
end

This will behave in the same way as the lambda version, but personally I find it cleaner, especially if the query is non-trivial.

This is all covered in the Rails Guides and was a good reminder for me that it’s worth having a quick read of them every once in a while to refresh your memory, especially for things that you don’t use that often.

Bath Digital Festival & Storm Hack Day

We’re delighted to announce the launch of the website for Bath’s first ever Digital Festival - http://bathdigitalfestival.com 

We’ve been working closely with the organising team over the past months to put together the branding, message and website for the Festival, and we’re really happy with the outcome. For those of you who don’t know, the Bath Digital Festival is a week-long collection of events celebrating the place of digital in all our lives, and the considerable impact that Bath has on the digital sector.

During the Festival, Storm will be hosting an Open Hack Day for designers and developers to come along, join a team, and build some apps. We’re also inviting members of the public along to see how ideas get turned into apps.

If you’re interested in attending the hack day, please add yourself the the waiting list: http://bathdigitalfestival.com/programme/storm-hack-day

Cloud Experts Launches

We have just launched a brand new site for Cloud Experts – a hosted desktop service that solves the financial issues associated with aging hardware for SMEs.

Cloud Experts allows companies struggling to update Microsoft Windows on older machines and servers a cheaper alternative than updating hardware.

Cloud Experts

By logging into Cloud Experts Hosted Desktop, users instantly experience the latest Microsoft Office 2010 productivity suite, with all of the familiar programmes including Word, PowerPoint, Excel, as well as hosted Exchange 2010 and Outlook 2010.

The website is powerful, allowing company owners to create users, grant access permissions to a wide range of applications, and setup email addresses, all through a very simple web interface.

Adam, our technical director, said “This project was a fantastic technical challenge to work on. Integrating with the various Windows technologies involved, such as Exchange and Active Directory, was new to all of us at Storm but the final result is absolutely fantastic and we’re really pleased with it.”

Storm news roundup 13-01-12

Our favourite web(ish) stories from the week…

Adam:

“The Raspberry Pi has started to be manufactured – in the Far East. The start of manufacturing is an achievement to be celebrated. The device, which will cost around £16-23, packs some serious punch:

‘HDMI for connection to a TV, USB for the keyboard, SD card for storage, and runs Linux on an ARM chip, with OpenGL ES 2.0 for the graphics. Its 700MHz ARM-11 processor is supported with 128MB of on-board memory.’

(source: reghardware.com)

The more expensive Model B chip will also come with 10/100Mb/s Ethernet. However, the fact that a UK charity is having to build a UK designed piece of hardware outside of the UK is not to be celebrated. They tried, boy did they try, but there was one significant stumbling block:

‘Simply put, if we build the Raspberry Pi in Britain, we have to pay a lot more tax. If a British company imports components, it has to pay tax on those (and most components are not made in the UK). If, however, a completed device is made abroad and imported into the UK – with all of those components soldered onto it – it does not attract any import duty at all.’

Now to my mind, that is just about as backwards as a tax system can possibly be when you want to encourage a resurgence in manufacturing in the UK. If you agree, please sign this E-petition.

Paul:

“The Education Secretary Michael Gove announced to broad acclaim (often from those of us unaccustomed to agreeing with him on anything much) from those in the software industry that the ICT curriculum is going to be overhauled, with a greater concentration on the fundamentals of computer science, logic and programming, rather than teaching people how to use products like Word and Powerpoint.

Gove summed it up pretty well: ‘Instead of children bored out of their minds being taught how to use Word and Excel by bored teachers, we could have 11-year-olds able to write simple 2D computer animations using an MIT tool called Scratch. By 16, they could have an understanding of formal logic previously covered only in University courses and be writing their own apps for smartphones.’ “

Mike:

“The Google ‘Search, plus your world‘ feature (put simply: Google+ network activity pops up as search results) has dominated lots of tech headlines this week. One of the most interesting pieces about it was this one on The Daily Beast which suggests that Google is actually doing this to break into Facebook’s walled garden, albeit in a fairly spectacular roundabout route…”

Felix, Liam and Andrew all picked up on the SOPA blackout story:

Felix:

“Reddit and I Can Has Cheezburger will be closing their sites on the 18th January in protest of the SOPA internet censorship bill, and Wikipedia editors are having a fierce debate whether to join in”

Liam:

“Next Wednesday, the ‘front page of the internet’, Reddit, will be turning off for 12 hours, to protest the proposed US law known as SOPA. They hope to encourage more websites to join them, with a few names stepping up to make a stand.”

Andrew:

“In news that can only further galvanize the growing anti SOPA coalition, the latest internet juggernaut has chosen to side with the likes of Reddit and Mozilla to send a message of defiance to the proponents of the impending ‘Stop Online Piracy Act’.

In what is being hailed as one of the most monumental days in the internets history, internet comedy auteurs ‘I can haz cheezburger’ has allied itself to the anti SOPA cause, choosing to blackout all aspects of it’s hilarious meme-based original content on the 18th of this month.

I can only say i’m glad that i can one day say to my future children that I witnessed it – the day the Berlin wall fell, the day Nelson Mandela was freed from his years of incarceration, and the day icanhazcheezburger.com went dark in protest to SOPA”


Using the Top Conversion Paths Report: Defining Custom Channel Groupings

 

The top conversion paths report is an awesome bit of kit; one of the five new multi-channel funnel reports in Google Analytics. The report shows a graphical table of the most common channels a user passes through to complete a site goal. The basic channel grouping is as follows:

Channel Description
1.Paid Advertising Visits from AdWords and/or links tagged with a medium of “cpc”, “ppc”, “cpm”, “cpv”, “cpa”, “cpp”.
2.Organic Search Visits from unpaid search on any search engine (i.e. medium=”organic”).
3.Social Network Visits from any of about 150 social networks.
4.Referral Visits from websites that are not social networks.
5.Email Visits that are tagged with a medium of “email”.
6.Feed Visits that are tagged with a medium of “feed”.
7.Direct Visits in which the visitor typed the name of your website URL into the browser or who came to your site via a bookmark.

Note that the channel listing follows an ordering process, in that visits which are not tagged as belonging to the first channel (Paid Advertising) fall through to the rule for the second channel, and so on. In the basic channel grouping, the direct channel also includes visits from a source/medium of “not set,” so that 100% of the site traffic is defined by this set of rules.

A classic example of how a visitor would interact with your site via different channels would be someone coming across your site via a Google search, bookmarking it, and then returning later via the bookmark to complete your goal:

Search, direct path

This is great, and with a table full of information like this, there is plenty of information to extract. However, the real power of the report comes in defining your own channel groupings. For example, in the above we see that the user originally came across the site by an organic search. We could create two channels to replace this one, breaking it down further into searches that already knew of your brand, and searches that came across your site with no prior knowledge of it (filtering by keyword.)

In order to set up your own channels, a simple way to do so is to edit the basic grouping. An easy way to copy the basic grouping template is found in the channel groupings drop down above the data set:

 

 

To move the channel ordering, drag on the grey boxes on the left of the buttons. Highlight the right hand side of a button to edit:

The interface is the same as in the search filter and advanced segments, so to create another rule highlight the “add ‘AND’ statement,” and select the dimension of keyword as below. Enter your brand name, and relabel the channel title before saving. Your groupings will also then be able to be used in the assisted conversions report.

 

 

From here, you can edit and create more channels to suit your needs. Track campaigns, visits through your blog, visits from your twitter account, visits from retweets, visits from apps and much more; giving you the vital information on how all these channels interact.

Although this report is hidden away deep in the goals section of the standard reports in Google Analytics, the information gleaned from it is priceless for any site owner.

Understanding Multi-Channel Funnels: An Introduction

Over the last year we have received a barrage of great updates from the chaps over at Google Analytics, with everything from an interface redesign to real-time monitoring of site traffic.

However my favourite of these recent announcements has been the inclusion of five powerful new reports under the label of multi-channel funnels. These new reports are sure to be highly useful tools for advertisers and business owners alike by allowing a more detailed analysis of how they are attracting the most important traffic to their sites.

The reports are found under the Conversions section of the Standard reports, and so in order to be useful, a site must have some Goals enabled. Note that goals do not have to be e-commerce related, but are an essential part of tracking and reaching the targets of any site. For example, if you run a small business and want to know who the people are that click on your contact page, you can set that up as a goal, and then analyse who accomplishes it – who converts.


An interaction with a site is defined by the visit received from a specific channel.  A channel can combine many different dimensions such as source, medium, keyword, campaign etc. The standard channels that the reports use are the following (for more info see here:)


Previously when trying to establish a picture of how effective specific marketing efforts were, Google Analytics would provide information of only the last interaction that led to a conversion. These new reports are extremely valuable as they can show us channels of visits to a site that a user makes within the 30 days before a conversion (provided cookies have not been cleared.)This is important, as a large number of visitors will browse around before eventually deciding to make a conversion.
Overview Report VennUnder the “Multi-Channel Funnels” heading, we are provided with five reports:

Overview Report – Provides a standard graph of goal conversions over time, allowing you to choose and compare between the conversions and assisted conversions metrics. Also includes a visualizer that produces a Venn diagram of conversions and assists between up to 4 selected channels:

Assisted Conversions Report – Allows you to compare the assisted conversions and final interaction conversions metrics. Also includes a ratio of the two, helping the user to analyse which channels spread knowledge of the site, and which lead to directly to conversions.

Top Conversion Paths Report – Shows in picture form, the paths that users took to convert. Also gives statistics on the numbers of people who took the specific path. For more info see here.

Time Lag – Give us information on how long users took to complete their path to conversion. Produces this in the form as below:

Time Lag Report
Path Length – Report produces a similar looking set of data to the Time Lag report, but instead of “Time Lag in Days,” shows us “Path length.”

Put together these reports give us valuable new insight into how a sites most important visitors access its most valuable content. For more information on the power of the top conversion paths report and on custom channel groupings, see our blog post here.

Today’s Gotcha: Localhost is not included in the ‘%’ wildcard when granting MySQL permissions

MySQL administration is one of those things I have to do pretty infrequently, so when I do there’s always something that trips me up and wastes a bit of time.

This week it was granting permissions on a database.

To grant permissions on a database to a user you use something like…

GRANT ALL ON db1.* TO 'user'@'hostname';

Hostname can also be ‘%’, which is a wildcard for any host.

My gotcha was that ‘%’ does not include ‘localhost’, so the locally hosted application couldn’t access the database that I could connect to remotely.

It’s not immediately obvious in the documentation that this is the case.  It says that ‘%’ “ applies to user_name for any host in the 192.168.1 class C subnet”, which of course in hind-sight does not include ‘localhost’.  While the local machine is in that class-C in most networks, the connection will come via the loopback address, not through an external network, so it’s origin host will not be in the ‘%’ set.

It can seem slightly counter intuitive but it makes sense when you understand a little more about what’s happening.

CoffeeScript, SASS and LESS compilation in Visual Studio 2010

I’ve been doing most of my development in Ruby on Rails over the past month and have fallen in love with the Asset Pipeline.  This is a process that lets you write in the new, more expressive forms and then compile and serve the raw CSS and JavaScript to the user.

The enhancements to CSS that are found in SASS and LESS are simply brilliant and save a massive amount of time.  Variables, nested selectors and mixins are just a few of my personal favourites.  CoffeeScript puts a wonderfully expressive veneer on top of JavaScript, removing lots of the grunt work and making your code much cleaner.

Using these improvements to the everyday languages has become second-nature and I’d be reluctant to go back.  Think about how much of a difference jQuery made to JavaScript DOM manipulation and animaton – you just wouldn’t want to go back to how it used to be!

However, my time is split between the Ruby on Rails world and the ASP.NET MVC world.  .NET doesn’t have native support for these new languages yet.  What’s a dev to do?


Continue reading