Created
March 2, 2012 15:19
-
-
Save hexatridecimal/1959135 to your computer and use it in GitHub Desktop.
Google2Piwik Automatic Import of All Web Properties
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'inifile' | |
require "mysql" | |
config = IniFile.load('google2piwik.conf') | |
begin | |
mysql = config['mysql'] | |
dbh = Mysql.real_connect(mysql['host'], | |
mysql['user'], | |
mysql['passwd'], | |
mysql['db']) | |
puts "Server version: " + dbh.get_server_info | |
File.open(ARGV.shift).each_line do |line| | |
if line =~ /^Site:(.*?)table_id: (.*)$/ | |
name = $1.strip.downcase | |
ga = $2.strip | |
name.gsub!(/\/$/, '') | |
next unless name =~ /(?:\.(?:com|net|org|edu|gov|us|info|biz))$/ | |
url = "http://#{name}/" | |
dbh.query("INSERT INTO piwik_site (name,main_url,ts_created,ecommerce,timezone,currency,excluded_ips,excluded_parameters) VALUES ('#{dbh.quote(name)}', '#{dbh.quote(url)}', NOW(), 0, 'UTC', '', '', '')") | |
res = dbh.query("SELECT LAST_INSERT_ID()") | |
site_id = nil | |
res.each do |row| | |
site_id = row[0] | |
end | |
STDERR.puts "site_id : #{site_id}" | |
STDERR.puts "site_url: #{url}" | |
config['piwik']['site_id'] = site_id | |
config['piwik']['site_url'] = url | |
config.write('google2piwik.conf') | |
system("/usr/local/bin/python", "google2piwik.py") | |
end | |
end | |
rescue Mysql::Error => e | |
puts "Error code: #{e.errno}" | |
puts "Error message: #{e.error}" | |
puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate") | |
ensure | |
# disconnect from server | |
dbh.close if dbh | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment