Getting the yclid on your website for tracking conversions

The yclid parameter is added to Yandex Direct ad URLs. To track conversions by yclid, set up extracting the yclid values from URLs and saving them on your server, and then send them to Yandex Metrica in a CSV file.

Before you start, make sure you can:

  • Change the HTML code of the website pages to enable saving of yclid parameter values from ad URLs.
  • Save the yclid parameter values along with the user information collected on your website.

Step 1. Configure saving yclid values from Yandex Direct ad URLs

Add a JavaScript code to your website pages' HTML code to save yclid values as cookies. In the example below, the cookie is assigned the name yclid.

<script type="text/javascript">
function setCookie(name, value, days){
    var date = new Date();
    date.setTime(date.getTime() + (days*24*60*60*1000)); 
    var expires = "; expires=" + date.toGMTString();
    document.cookie = name + "=" + value + expires + ";path=/";
}
function getParam(p){
    var match = RegExp('[?&]' + p + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
var yclid = getParam('yclid');
if(yclid){
    setCookie('yclid', yclid, 90);
}
</script> 

We recommend placing this code in the body element. This way you don’t have to add it every time you create an ad.

Step 2. Set up yclid extraction from cookies

In the HTML code of your website pages, embed the code for extracting and sending yclid values to your server. You can do this on a page where users fill out a form with their contact details or other information. We recommend that you transmit yclid as a hidden value of a field in the form filled out by the users.

Note

Code for transmitting the yclid can be written in any programming language supported by your server or the user's browser. The example uses JavaScript.

<form action="" name="myForm">   
    Name: <input type="text" name="name">
    <!--Hidden field for extracting yclid-->   
    <input type="hidden" id="yclid_field" name="yclid_field" value="">    
    <input type="submit" value="Submit Form" name="btnSubmit">  
</form>

<!--Extracting yclid and changing the hidden field-->
<script> 
  function readCookie(name) { 
  var n = name + "="; 
  var cookie = document.cookie.split(';'); 
  for(var i=0;i < cookie.length;i++) {      
      var c = cookie[i];      
      while (c.charAt(0)==' '){c = c.substring(1,c.length);}      
      if (c.indexOf(n) == 0){return c.substring(n.length,c.length);} 
  } 
  return null; 
  } 
  window.onload = function() {      
      document.getElementById('yclid_field').value = 
  readCookie('yclid'); 
  } 
</script>

Chat with us

Write an email

Please note: Our support team will never initiate a call to you. Do not follow any instructions of people who call you and introduce themselves as the Yandex Metrica support team.