#!/usr/bin/env ruby | |
############## | |
#Usage: ruby <FULL_PATH>/get_tealium_configuration.rb | |
# | |
#Pre-requisites: | |
# Any web browser which Watir can call is installed on the box | |
# Ruby 1.9.3+ | |
# watir-webdriver gem | |
# [Optional] node.js and JavaScript beautifier module - https://github.com/einars/js-beautify. Install to default path. | |
# | |
#Create html file for each Tealium profile-environment combination | |
#The html file contains the Tealium JS loader script | |
#Use webdriver (browser automation) to open the html in a browser | |
#The response from Tealium will contain the http tag (pixel) locations as src attributes of script tags | |
#Extract all the http tag locations | |
#Visit each tag location, get the Tealium JavaScript for the tag and create file for each tag | |
#Beautify the source code | |
#Write to an output directory | |
############## | |
require 'watir-webdriver' | |
require 'logger' | |
require './local_config' | |
#Delete dir contents | |
def clean_dir(log, dir_name) | |
if File.directory?(dir_name) | |
begin | |
FileUtils.rm_rf(dir_name) | |
rescue | |
#Okay to ignore error if directory could not be cleaned | |
log.warning("Unable to delete " + file_path + ". Continuing...") | |
end | |
end | |
end | |
#Create dir if necessary""" | |
def create_dir(dir_name) | |
if !Dir.exists?(dir_name) | |
Dir.mkdir(dir_name) | |
end | |
end | |
def get_exe_path() | |
return File.expand_path(File.dirname(__FILE__)) | |
end | |
def get_full_path(file) | |
#Return absolute path to this script dir | |
return File.join(get_exe_path(), file) | |
end | |
#Create directories for each environment | |
def create_dirs(log, output_dir, profile_list, env_list) | |
create_dir(output_dir) | |
for profile in profile_list | |
create_dir(File.join(output_dir, profile)) | |
for env in env_list | |
create_dir(File.join(output_dir, profile, env)) | |
end | |
end | |
end | |
def get_tealium_dir(dir_name, profile, env) | |
return File.join(dir_name, profile, env) | |
end | |
def get_tealium_file(tealium_dir, profile, env) | |
return File.join(tealium_dir, env + '.html') | |
end | |
def get_tealium_html(tealium_file) | |
return "file:///" + tealium_file | |
end | |
def get_tealium_html_no_url(tealium_html) | |
return tealium_html.sub( %r{file:///}, "") | |
end | |
#Create file based on content | |
def create_tealium_html(file_name, content) | |
File.open(file_name, 'w+') {|f| f.write(content) } | |
end | |
#Get all Tealium script src tags using a local file | |
def get_tealium_script_srcs(b, tealium_get_html, tealium_prefix) | |
tealium_local_file = get_tealium_html_no_url(tealium_get_html) | |
if (!File.file?(tealium_local_file)) | |
raise Exception.new(tealium_local_file + " html file could not be found. " + | |
"This file should contain the JavaScript src tag for Tealium. Aborting...") | |
end | |
#Get all Tealium script src tags | |
b.goto tealium_get_html | |
script_list = [] | |
b.scripts.each do |script| | |
src_script = script.attribute_value("src") | |
if !(src_script.nil? || src_script.empty?) | |
src_script = src_script.sub( %r{file:///}, tealium_prefix) | |
script_list.push(src_script) | |
end | |
end | |
script_list = script_list.uniq | |
return script_list | |
end | |
#if node based js-beautify module exists, beautify the JavaScript | |
def try_beautify_js(node, beautify, file) | |
if (File.exists?(node) && File.exists?(beautify)) | |
begin | |
%x("#{node}" "#{beautify}" -f #{file} -r) | |
rescue => e | |
#We dont care if the beautify fails | |
log.warning("Beautify failed") | |
log.warning(e.message) | |
end | |
end | |
end | |
def get_utag_name(prepend_name) | |
return 'utag_' + prepend_name + '.js' | |
end | |
def guess_script_name(script_text, tealium_script_guesses, fallback_name) | |
tealium_script_guesses.keys.each do |script_guess| | |
if script_text.include? tealium_script_guesses[script_guess] | |
return get_utag_name(script_guess) | |
end | |
end | |
return get_utag_name(fallback_name) | |
end | |
#Iterate over scripts and write them to utag_[n].js files | |
def download_tealium_files(b, script_list, dir_name, node, beautify, tealium_script_guess) | |
counter = 0 | |
script_list.each do |script| | |
b.goto script | |
#The Tealium script is in the pre element of the html | |
script_text = b.pre().inner_html | |
output_file = guess_script_name(script_text, tealium_script_guess, counter.to_s) | |
#output_file = 'utag_' + counter.to_s + '.js' | |
output_file_full_path = File.join(dir_name, output_file) | |
File.open(output_file_full_path, 'w+') {|f| f.write(script_text)} | |
try_beautify_js(node, beautify, output_file_full_path) | |
counter += 1 | |
end | |
end | |
#For all envs, get Tealium JavaScript files | |
def browser_get_tealium_files(log, b, profile_list, env_list, output_dir, content, tealium_prefix, node, beautify, tealium_script_guess) | |
log.info("Getting tealium files") | |
profile_list.each do |profile| | |
env_list.each do |env| | |
script_list = [] | |
tealium_dir = get_tealium_dir(output_dir, profile, env) | |
tealium_file = get_tealium_file(tealium_dir, profile, env) | |
#Replace env in html template | |
create_tealium_html(tealium_file, content % [profile, env]) | |
tealium_html = get_tealium_html(tealium_file) | |
script_list = get_tealium_script_srcs(b, tealium_html, tealium_prefix) | |
download_tealium_files(b, script_list, tealium_dir, node, beautify, tealium_script_guess) | |
end | |
end | |
end | |
if __FILE__ == $0 | |
#Change to correct dir, since the command could be called from any location | |
Dir.chdir(get_exe_path) | |
log = Logger.new(get_full_path($script_name + '.log')) | |
log.info("Started job " + $script_name) | |
#In case of any error, we log and bail out | |
#Any post processing or monitoring can be done from the outside on the log file | |
begin | |
output_dir = get_full_path($output_directory) | |
browser = Watir::Browser.new | |
clean_dir(log, output_dir) | |
create_dirs(log, output_dir, $profile_list, $env_list) | |
browser_get_tealium_files(log, | |
browser, | |
$profile_list, | |
$env_list, | |
output_dir, | |
$tealium_html_file_content, | |
$setting_tealium_cdn_prefix, | |
$node_js_location, | |
$js_beautifier_location, | |
$tealium_script_tag_guess_signature) | |
browser.close() | |
log.info("Completed job " + $script_name) | |
print("Success") | |
rescue => e | |
log.error("Error in job " + $script_name) | |
log.error(e.message) | |
end | |
end | |
#---------------------------------- | |
#Configuration file | |
#---------------------------------- | |
### Config settings - Replace as necessary ### | |
$setting_tealium_cdn_prefix = "http://tags.tiqcdn.com/" | |
$env_list = ['dev', 'qa'] | |
$profile_list = ['my-profile'] | |
$tealium_script_tag_guess_signature = {'loader' => 'tealium universal tag - utag.loader', 'BazaarVoice' => 'ugc.bazaarvoice.com', 'SiteCatalyst' => 'omtrdc.net' } | |
#TBD - Use correct profile based on final configuration | |
#The environment is the replace-able part of this html template | |
#The title is unique (mytealiumconfiguration) - do NOT change it. This is used to get past any constraining load rules in Tealium. | |
#The title should also be allowed through for any constraining load rule in Tealium. | |
$tealium_html_file_content = '<html><head><title>mytealiumconfiguration</title></head>' + \ | |
'<body><div id="ScriptWrapper">' + \ | |
'<script src="http://tags.tiqcdn.com/utag/vprt/%s/%s/utag.js">' + \ | |
'</script></div></body></html>' | |
$output_directory = "TealiumScripts" | |
$script_name = "get_tealium_configuration" | |
$node_js_location = "c:/Program Files/nodejs/node.exe" | |
$js_beautifier_location = "C:/Program Files/nodejs/node_modules/npm/node_modules/js-beautify/js/bin/js-beautify.js" | |
### ~Config settings ### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment