YouTube embed videos

YouTube provides privacy enhanced mode for embedded videos to avoid loading any tracking cookies. This can be configured when you get the embed like from YouTube. This way you don’t have to worry about consents:

<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/NHEaYbDWyQE" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe><br>

If you don’t have access to the YouTube embed code, you can also replace the url by adding the following javascript to the <head> of your site:

<script type="text/javascript">
var frames = document.getElementsByTagName('iframe');
for (var i=0; i < frames.length; i++)
{
  frames[0].src = frames[0].src.replace(/www.youtube.com/gi,'www.youtube-nocookie.com');
}
</script><br>

JavaScript API

Another way would be to load YouTube embed video using inline javascript code which will honor the cookie preferences:

<div id="ytplayer">You must enable analytical cookies to watch this video</div>
<script type="text/plain" data-consent="analytics">
// Load the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// Replace the 'ytplayer' element with an <iframe> and
// YouTube player after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('ytplayer', {
height: '390',
width: '640',
videoId: 'NHEaYbDWyQE'
});
}
</script><br>

Still need help? Contact Us Contact Us