// After including analytics.js, but before using it anywhere... // Wrap identify so that all traits persist in a cookie, // and each call is merged with previously persisted traits var identifyFn = analytics['identify']; analytics['identify'] = function(){ var new_traits, new_arguments = [], referrer_traits, id, old_traits, all_traits = {}; // First arg might be the ID, or the attributes may be set anonymously if(arguments[0] && (typeof arguments[0] == 'string')){ id = arguments.shift(); } new_traits = arguments[0]; // Collect existing and previous traits together old_traits = _.extend({}, JSON.parse($.cookie('analytics_user_traits'))); all_traits = _.extend({}, old_traits, new_traits); // Also collect and merge referrers and campaigns referrer_traits = getReferrerTraits(); all_traits = _.extend({}, all_traits, referrer_traits); // Persist traits for next time $.cookie("analytics_user_traits", JSON.stringify(all_traits)); // identify the user with all traits to the analytics services if(typeof id == 'string') { new_arguments.push(id); } new_arguments.push(all_traits); identifyFn.apply(identifyFn, new_arguments); };