Tracking Plays of Embedded YouTube Videos

As part of an experiment, you may want to test different embedded YouTube videos to find out which one converts better.  You can create a goal that will track whether a video is played, or even a goal that tracks if the video finished playing.  YouTube provides the YouTube Player API through which you can trigger a conversion on the above-mentioned events.

This can be achieved a couple of different ways, and we provide one example, but it depends on your specific scenario and what your original page contains.  Please see YouTube references at the bottom of this article for further information.

The YouTube code requires an ID to identify the iframe where the video is.  If there is no iframe already, then it requires a div with an ID; YouTube will replace the div with the iframe and video player.

If your original page already has an iframe with an embedded video, but no ID, you can add an ID with the Code Editor.  Similarly, if it has no iframe but a div where you want to place the embedded video, you must add an ID to the div, and you can do that with the Code Editor.

Normally the Original is not modified with the Code Editor but in this case you may need to do so to add the ID as this is essential to track clicks on the original as well as the variations.

You can add the YouTube code on the Global Experience JS where it will apply to the original and all the variations, or else add the code individually to the Original and each variation's Custom JS, instead of the Global Experience JS.

Example Configuration:

Code Editor:CodeEditor.png

See the Pen Code Editor / YouTube / Alex by George Crewe (@gcrewe) on CodePen.

In the above code, replace 'element', with the element you are targeting, replace width and height as needed, and xxxxxx with the video ID in the source.

Global Experience JS:GlobalExperienceJS.png

GlobalExperienceJS-code.png

See the Pen Global Experience JS / YouTube / Alex by George Crewe (@gcrewe) on CodePen.

Place the above code in Global Experience JS, and create a JavaScript goal for which event(s) you want to track.  See https://convert.zendesk.com/hc/en-us/articles/114093992211-Custom-Prebuilt-Goals#create-a-js-goal - once the goal is created you will have a goal ID per the article.  In the code above, where you see _conv_q.push(["triggerConversion", "xxxxxxxx"]); replace xxxxxxxx with the Goal ID for the goal you created.

PlayGoal.png

As mentioned previously, you could also put this code in Custom JavaScript but in that case you would have to duplicate it in the Original and each Variation, so it is better to add it only once, to the Global Experience JS.

References:

https://developers.google.com/youtube/iframe_api_reference

https://developers.google.com/youtube/youtube_player_demo

Have more questions? Submit a request

Comments