Skip to content

Instantly share code, notes, and snippets.

(function(doc) {
var ref = doc.referrer;
if (ref.match(/google\./gi)) {
var query = ref.split('?')[1],
keyword = '(not provided)',
pathname = doc.location.pathname,
rank = null,
param;
if (query) {
@yoavmatchulsky
yoavmatchulsky / gist:4749498
Created February 10, 2013 12:49
Fix for setting a he locale (/he prefix) to named routes. Bug was that #url_for called from views didn't get the correct locale. Solved by set_locale if @locale.nil in #default_url_options
def set_locale
if @locale.nil?
I18n.locale = params[:locale] || I18n.default_locale
@locale = I18n.locale.to_sym
end
end
def default_url_options(options = {})
set_locale if @locale.nil?
@yoavmatchulsky
yoavmatchulsky / cleanup_tasks.rb
Last active December 14, 2015 19:18
rake clean:sessions to delete old sessions
desc 'Cleanup tasks'
namespace :clean do
desc 'Clean sessions table (pass DAYS=X default is 30 days)'
task :sessions => :environment do
days = ENV['DAYS'].to_i
days = 30 if days <= 0
result = ActiveRecord::Base.connection.execute("DELETE FROM sessions WHERE updated_at <= (CURRENT_DATE - INTERVAL '#{days} days')")
affected_rows = result.cmd_tuples
@yoavmatchulsky
yoavmatchulsky / pluralize_with_parenthesis.rb
Created January 21, 2014 11:50
Add #pluralize_with_parenthesis to ActiveSupport::Inflector and Rails helpers. Example: 'article'.pluralize_with_parenthesis => 'article(s)' 'person'.pluralize_with_parenthesis => 'people'
module ActiveSupport
module Inflector
def pluralize_with_parenthesis(word)
rule, replacement = find_inflections(word, inflections.plurals)
if replacement =~ /\A\w+\Z/
word.gsub!(rule, "(#{replacement})")
else
pluralize(word)
end
end
@yoavmatchulsky
yoavmatchulsky / mobile-experiment.html
Last active August 29, 2015 14:08
Only show GA content experiment to mobile traffic
<script>
////// START EDITING HERE
var utmx_key = 'XXXXXXX-X', // change this to your experiment key
show_on = 'mobile', // Can be 'mobile' or 'desktop'
///// STOP EDITING HERE
///////////////////////////
is_mobile = function () {
return /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera\s?mini/i.test(navigator.userAgent.toLowerCase());
},
@yoavmatchulsky
yoavmatchulsky / application.rb
Created June 15, 2015 08:56
Rails with Angular's application.rb
module RailsWithNg
class Application < Rails::Application
[...]
config.assets.paths << Rails.root.join('app', 'ng')
# Add bower CSS components
bower_path = Rails.root.join('vendor', 'assets', 'bower_components')
config.assets.paths << bower_path
config.sass.load_paths << bower_path
def z_score(variation_id)
# Taken from https://github.com/andrew/split/blob/master/lib/split/zscore.rb
n_1 = views_count(variation_id).to_f
n_2 = views_count(popup_id).to_f
p_1 = conversion_rate(variation_id)
p_2 = conversion_rate(popup_id)
if n_1 < 30 || n_2 < 30 || (p_1 * n_1 < 5) || (p_2 * n_2 < 5)
return false
end
RewriteCond %{QUERY_STRING} ^lang=(\w{2})/(.*)$
RewriteRule .* http://%1.example.com/%2 [L,R=301]
@yoavmatchulsky
yoavmatchulsky / gist:cdd94aaae24040d0c38f
Created October 8, 2015 11:00
Mobile detection configuration for nginx.
# /var/log/nginx/conf.d/mobile.conf
map $http_user_agent $mobile {
default 0;
~*(android|bb\d+|meego).+mobile|ip(hone|od)|opera\sm(ob|in)i|mobile.+firefox 1;
~*iemobile|phone|symbian|windows\sce|blackberry|up\.(browser|link) 1;
~*avantgo|bada\/|blazer|compal|elaine|fennec|hiptop|iris|kindle|lge\s|maemo 1;
~*midp|mmp|netfront|palm(\sos)?|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0 1;
~*treo|vodafone|wap|xda|xiino 1;
~*^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a\swa|abac|ac(er|oo|s\-)) 1;
@yoavmatchulsky
yoavmatchulsky / gist:7a04a6008ca06a65e9de
Created October 8, 2015 11:00
Mobile detection configuration for nginx
# /var/log/nginx/conf.d/mobile.conf
map $http_user_agent $mobile {
default 0;
~*(android|bb\d+|meego).+mobile|ip(hone|od)|opera\sm(ob|in)i|mobile.+firefox 1;
~*iemobile|phone|symbian|windows\sce|blackberry|up\.(browser|link) 1;
~*avantgo|bada\/|blazer|compal|elaine|fennec|hiptop|iris|kindle|lge\s|maemo 1;
~*midp|mmp|netfront|palm(\sos)?|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0 1;
~*treo|vodafone|wap|xda|xiino 1;
~*^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a\swa|abac|ac(er|oo|s\-)) 1;