Skip to content

Instantly share code, notes, and snippets.

@dannyvassallo
Created March 26, 2015 19:21
Show Gist options
  • Save dannyvassallo/385d36e331dc8c803237 to your computer and use it in GitHub Desktop.
Save dannyvassallo/385d36e331dc8c803237 to your computer and use it in GitHub Desktop.
Translates fngt UTM arguments to readable UTM arguments for Google Analytics outside of facebook tan apps
//Get fngt appdata
var urlParams;
(window.onpopstate = function () {
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);
urlParams = {};
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
})();
var app_data = urlParams["app_data"];
if (app_data) {
//Parses fngt appdata into JSON hash
var params = $.parseJSON(app_data);
//Turns hash keys and values into arrays
var array_keys = new Array();
var array_values = new Array();
for (var key in params) {
array_keys.push(key);
array_values.push(params[key]);
}
//rebuilds url
var url = document.location.host;
var redirectUrl = "http://"+url+"?"+array_keys[0]+"="+array_values[0]+
"&"+array_keys[1]+"="+array_values[1]+
"&"+array_keys[2]+//this index has no properties in this campaign
"&"+array_keys[3]+"="+array_values[3]+
"&"+array_keys[4]+"="+array_values[4];
//redirects with proper utm set up
location.href = redirectUrl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment