S
S
Sergey Shilov2015-10-02 15:08:33
JavaScript
Sergey Shilov, 2015-10-02 15:08:33

How to create events on the site using utm tags?

Hello! Not strong in js, but I will be grateful if you at least orient in which direction to start looking.
The following is required: depending on the parameters of utm tags, I should have a certain event on the site, for example, a person clicked on a certain tag through contextual advertising, and, accordingly, the function corresponding to this tag was executed.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Inchin ☢, 2015-10-02
@Olivoin

See what's in locationand run the desired piece of code.
----------------
You can write a function to get the label:

function getUTM(name){
  var target = location.search.match(
     new RegExp("utm_"+name+"=([^&]+)")
  );

  return target ? target[1] : null;
}

I would either use switch -> case , or an object with methods identical in name to the label values.
switch(getUTM("source")){
   case "yandex": //Делаем что-то
          break;
   case "google": //Или делаем это
          break;
}

Or like this:
var funcGen = {
  "yandex" : function(){ ... },
  "google" : function(){ ... }
}[getUTM("source")];

//Если есть такая - запустим
funcGen&&funcGen();

D
Dmitry Novikov, 2015-10-02
@dmitriy_novikov

utm-tag is essentially a get-parameter.
catch the value of the get-parameter (more conveniently through PHP, not through js) and execute the code you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question