Last active
August 29, 2015 14:02
-
-
Save leebrooks0/32448344dc196c495b86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```rb | |
def associative_create_statement(query_params={}) | |
user_agent = request.user_agent | |
browser = Browser.new(ua: user_agent) | |
os = get_os(browser) | |
query_params.reverse_merge!( | |
original_url: request.original_url, | |
controller_name: controller_name, | |
action_name: action_name, | |
user_id: user_id, | |
request_hash: @impressionist_hash, | |
session_hash: session_hash, | |
ip_address: ip, # otherwise we get Cloudflare's IPs | |
referrer: request.referer, | |
## Set custom field values below: | |
# Location | |
latitude: latitude, | |
longitude: longitude, | |
city: city, | |
country: country, | |
# Browser, OS, device etc. | |
user_agent: user_agent, | |
os: os, | |
browser: browser.name, | |
browser_version: browser.version, | |
browser_modern: browser.modern?, | |
mobile: browser.mobile?, | |
tablet: browser.tablet?, | |
console: browser.console?, | |
tv: browser.tv? | |
) | |
end | |
# https://github.com/fnando/browser/blob/master/lib/browser/methods/platform.rb#L101 | |
# #platform returns :other if not :mac, :windows or :linux | |
# If ios or android then return instead of :other | |
def get_os(browser) | |
os = browser.platform # :mac, :windows, :linux or :other | |
os = :android if browser.android? | |
os = :ios if browser.ios? | |
os | |
end | |
def ip | |
# Cloudflare intercept all traffic and pass along the connecting IP: | |
# https://support.cloudflare.com/hc/en-us/articles/200170986-How-does-CloudFlare-handle-HTTP-Request-headers- | |
request.headers['CF-Connecting-IP'] || request.remote_ip | |
end | |
def latitude | |
geoip.latitude if geoip | |
end | |
def longitude | |
geoip.longitude if geoip | |
end | |
def city | |
geoip.city_name if geoip | |
end | |
def country | |
geoip.country_name if geoip | |
end | |
def geoip | |
geoip = GeoIP.new("#{ Rails.root }/db/GeoLiteCity.dat") | |
geoip.local_ip_alias = '105.236.216.37' # Western Cape, ZA - else returns nil on local addresses | |
geoip.city(ip) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment