I am trying to insert video tracking with GTM on a Wordpress website, and use two custom JS codes for that: youtube-is-present

function () {
for (var e = document.getElementsByTagName('iframe'), x = e.length; x--;)
    if (/youtube.com\/embed/.test(e[x].src)) return true;
return false;


and another one to send the data for the usage of the video to Google Analytics

function() {
  var status = {{Video Status}};
  switch (status) {
    case 'start':
      return 'Play';
    case 'pause':
      return 'Pause';
    case 'seek':
      return 'Seeking';
    case 'progress':
      return + {{Video Percent}} + '% Watched';
    case 'complete':
      return '100% Watched';

For some reason none of those work, despite the fact that the code generated by the Wordpress plugin contains all that is needed for the video.

Does anyone have idea why the video cannot be found and the tag be fired?

1 Answer 1


I could be mistaken, but I don't think that interactions within the Youtube video iframe can be tracked by Analytics. This is because of cross-origin policy (different domains). You can track Analytics data on same-origin (same domain).

For instance, if I were to embed example.com into an iframe on my website, I couldn't have Analytics track when users click links on example.com or interact with that site. That's cross-origin.

For the same reasons, I don't think you can track when someone is interacting with a Youtube video that is in an iframe on your site. It's possible that Wordpress plugin was designed to track interactions with videos that are called from your own server, but not Youtube's.

You can track when a user loads a video into a Youtube iframe on your site when he clicks the link to that video. You can do that with analytics with:

<a href="" onClick="ga('send', 'event', 'Video', 'Watch', 'VideoWatched');">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.