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, check the following:
  • On the Campaign parameters page in Yandex.Direct, the Tag links for Yandex.Metrica option is enabled. This means that the yclid values are sent in your campaign's ad URLs.
  • You can change the HTML code of the website pages to enable saving yclid parameter values from ad URLs.
  • You can 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 the JavaScript snippet 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 putting this code in the body element. This way you don't have to add it every time you create an ad.

Step 2. Configure detecting the yclid value from cookies

In the HTML code of your website pages, insert the code for detecting yclid values and sending them 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 the yclid as a hidden value of a field on 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 yclid extraction--> 		
    <input type="hidden" id="yclid_field" name="yclid_field" value=""> 	 	
    <input type="submit" value="Submit Form" name="btnSubmit"> 	
</form>

<!--yclid extraction and hidden field change-->
<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>