norixxx
norixxx

Reputation: 3199

Video tag click event(play, stop, pause, etc...) tracking with Google Tag Manager

I would like to know how to track click event of HTML 5 video tag with Google Tag Manager(GTM).

I have no access to GTM's admin page, i was wondering if there are any ways to tell GTM via JavaScript.

I thought a code for Google Analytics will do the work, but it didn't.

// just get one click event at a page load.
var isVideoPlayed = false; 
$('#myvideo').on('play', function(){
    if(!isVideoPlayed) {
         ga('send', 'event', 'Videos', 'play', 'My Video');
         isVideoPlayed = !isVideoPlayed;
    }
    return false;
});

Any help appreciated!

Upvotes: 0

Views: 1435

Answers (2)

nyuen
nyuen

Reputation: 8907

The problem with the code ga('send', 'event', 'Videos', 'play', 'My Video'); is that it lacks the name of the GTM tracker, which is assigned when the tracking object is created through GTM. You could write a function that includes something like this:

ga.getAll()[0].get('name');

that would return the GTM tracker name. You would then be able to call your GA commands with the specific tracker name, like this:

ga('trackerName.send', 'event', 'Videos', 'play', 'My Video');

where trackerName is the name returned from aforementioned function.

Upvotes: 1

Koby Douek
Koby Douek

Reputation: 16693

Google fully supports using javascript for its Analytics API using analytics.js.

The analytics.js library is a JavaScript library for measuring how users interact with your website.

Start using it on this page.

For Google Tag Manager (GTM), Use the instructions on this site.

Upvotes: 0

Related Questions