I have code to get GA parameters that have been stored in a cookie and make them the values of inputs in a form that isn't working and I can't for the life of me figure out why.
(function( $ ) {
if (Cookies.get('gaParams')) {
var params = Cookies.get('gaParams');
}
$('#source').val(params['utm_source'] ? params['utm_source'] : 'organic');
$('#medium').val(params['utm_medium'] ? params['utm_medium'] : '');
$('#campaign').val(params['utm_campaign'] ? params['utm_campaign'] : '');
$('#keywords').val(params['utm_terms'] ? params['utm_terms'] : '');
console.log(params,params['utm_medium'],params.utm_medium);
}(jQuery));
{"utm_medium":"testing","utm_source":"whatever"} undefined undefined
Because the variable params
is string type . You should use this method JSON.parse()
to parse a JSON string
var params = Cookies.get('gaParams');
params = JSON.parse(params);