Yoon Chang & Joel Tillman
Twitch is arguably one of the largest, if not the largest, platforms for streaming video game content. As more companies and independent content creators make streaming content a core part of their business and engagement marketing strategies, they will need a functioning analytics solution.
Here, we will cover how to track streaming video views from embedded Twitch players on your site by using their API to send GTM-compatible dataLayer custom events.
A client wanting to embed their own Brand's Twitch channel on their site realized there was no built-in functionality to send viewership data on-site to their Google Analytics property.
Thankfully, Twitch has their JavaScript-based API documentation here:
https://dev.twitch.tv/docs/embed/video-and-clips/
However, it is understandably difficult to parse through the instructions if you are not familiar with using Javascript or APIs. Here’s what you can do:
Take the default embed script for Interactive Frames/VODs from: https://dev.twitch.tv/docs/embed/video-and-clips/
<script src= "https://player.twitch.tv/js/embed/v1.js"></script> <div id="<player div ID>"></div> <script type="text/javascript"> var options = { width: <width>, height: <height>, channel: "<channel ID>", video: "<video ID>", collection: "<collection ID>", // only needed if your site is also embedded on embed.example.com and othersite.example.com parent: ["embed.example.com", "othersite.example.com"] }; var player = new Twitch.Player("<player div ID>", options); player.setVolume(0.5); </script>
There are a plethora of options to potentially add to this base code, but for tracking purposes, we must add the event listener shown in the documentation.
So, for our use case, the event is Twitch.Player.PLAYING and the function we are wanting to call is:
dataLayer.push({'event': 'twitch-play'});
to push the event 'twitch-play' into the dataLayer.
The resulting code to add is then:
player.addEventListener(Twitch.Player.PLAYING, function(){ dataLayer.push({'event': 'twitch-play'}); } );
Note: You can add useful dataLayer variables to this event push using the API such as the video ID.
<script src= "https://player.twitch.tv/js/embed/v1.js"></script> <div id="SamplePlayerDivID"></div> <script type="text/javascript"> var options = { width: 400, height: 240, channel: "yourchannel", // Only needed if this page is going to be embedded on other websites parent: ["adswerve.com"] }; var player = new Twitch.Player("SamplePlayerDivID", options); player.setVolume(0.5); player.addEventListener(Twitch.Player.PLAYING, function(){ dataLayer.push({'event': 'twitch-play'}); } ); </script>
Now it is time to add a GTM trigger that works off of the resulting "twitch-play" event that will get pushed to the dataLayer!
You can repeat this across all events (event:String) supported by the API:
If you followed all the instructions, you should be ready to go!
Curious about other innovative data solutions to meet your unique needs? Contact us today!