get utm paramater from url in javascript



UTM paramaters are widely used in adcampaigns to check sources are traffice. google ads and other ads send a parameters, such as utm_source, utm_medium, utm_campaign in urls.

Some of the utm paramaters are as follows:-
1) Campaign Source (utm_source) – Required parameter to identify the source of your traffic such as: search engine, newsletter, or other referral.
2) Campaign Medium (utm_medium) – Required parameter to identify the medium the link was used upon such as: email, CPC, or other method of sharing.
3) Campaign Term (utm_term) – Optional parameter suggested for paid search to identify keywords for your ad. You can skip this for Google AdWords if you have connected your AdWords and Analytics accounts and use the auto-tagging feature instead.
4) Campaign Content (utm_content) – Optional parameter for additional details for A/B testing and content-targeted ads.
5) Campaign Name (utm_campaign) – Required parameter to identify a specific product promotion or strategic campaign such as a spring sale or other promotion.

Simple code snippet to get get url paramater values for utm sources.

//function to get url paramaters
function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

//variable to stor url values
var utmsource = getUrlVars()["utm_source"];
var utm_medium = getUrlVars()["utm_medium"];
var utm_campaign = getUrlVars()["utm_campaign"];

Similarly you can get all other valiables from url.

0 comments :