跟踪HTML5音频元素的播放次数?

时间:2023-02-07 19:16:26

What is the best way to track how many times an HTML5 audio element is played?

跟踪HTML5音频元素播放次数的最佳方法是什么?

(we can use Google Analytics too, if that is the best approach)

(如果这是最佳方法,我们也可以使用Google Analytics)

2 个解决方案

#1


8  

HTML5 Audio elements have basic callbacks.

HTML5音频元素具有基本回调。

You can combine that with a basic event callback library like jQuery to attach these events by default:

您可以将它与基本事件回调库(如jQuery)结合使用,以默认情况下附加这些事件:

$("audio").bind("play", function(){
_gaq.push(["_trackEvent","Audio", "play", $(this).attr('src')]);
});

You can also do similar events for tracking when people finish the audio:

当人们完成音频时,您还可以执行类似的事件跟踪:

$("audio").bind("ended", function(){
_gaq.push(["_trackEvent","Audio", "ended", $(this).attr('src')]);
});

This can be made more concise by combining them into a single call:

通过将它们组合成单个调用,可以使其更简洁:

$("audio").bind("play ended", function(e){
_gaq.push(["_trackEvent","Audio", e.type, $(this).attr('src')]);
});

You can also add the events on the <audio> tag attributes as onplay and onended, but, I wouldn't recommend that approach.

您还可以将

#2


1  

If you upgraded to Universal Analytics and are not using classic analytics, then you would use a send event not a push event: ga('send', 'event', 'Audio', e.type, $(this).attr('src')); Also, if you were just testing this on your own, make sure you didn't create a filter to filter out your own IP address.

如果您升级到Universal Analytics并且未使用经典分析,那么您将使用发送事件而非推送事件:ga('发送','事件','音频',e。类型,$(此).attr( 'SRC'));此外,如果您只是自己测试,请确保您没有创建过滤器来过滤掉您自己的IP地址。

#1


8  

HTML5 Audio elements have basic callbacks.

HTML5音频元素具有基本回调。

You can combine that with a basic event callback library like jQuery to attach these events by default:

您可以将它与基本事件回调库(如jQuery)结合使用,以默认情况下附加这些事件:

$("audio").bind("play", function(){
_gaq.push(["_trackEvent","Audio", "play", $(this).attr('src')]);
});

You can also do similar events for tracking when people finish the audio:

当人们完成音频时,您还可以执行类似的事件跟踪:

$("audio").bind("ended", function(){
_gaq.push(["_trackEvent","Audio", "ended", $(this).attr('src')]);
});

This can be made more concise by combining them into a single call:

通过将它们组合成单个调用,可以使其更简洁:

$("audio").bind("play ended", function(e){
_gaq.push(["_trackEvent","Audio", e.type, $(this).attr('src')]);
});

You can also add the events on the <audio> tag attributes as onplay and onended, but, I wouldn't recommend that approach.

您还可以将

#2


1  

If you upgraded to Universal Analytics and are not using classic analytics, then you would use a send event not a push event: ga('send', 'event', 'Audio', e.type, $(this).attr('src')); Also, if you were just testing this on your own, make sure you didn't create a filter to filter out your own IP address.

如果您升级到Universal Analytics并且未使用经典分析,那么您将使用发送事件而非推送事件:ga('发送','事件','音频',e。类型,$(此).attr( 'SRC'));此外,如果您只是自己测试,请确保您没有创建过滤器来过滤掉您自己的IP地址。