假设我们有Youtube嵌入的代码:
<iframe id="my-youtube-video-id" width="560" height="315" src="https://www.youtube.com/embed/tzbrKBv-khE" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>和Facebook像素事件代码:
<button id="addToCartButton">Purchase</button>
<script type="text/javascript">
$('#$my-youtube-video-id').click(function() {
fbq('track', 'ViewContent');
});
</script>但当有人点击视频时,它似乎并不能检测到。我上WordPress了。你知道我做错了什么吗?
PS:我正在遵循上的文档:https://www.facebook.com/business/help/402791146561655?id=1205376682832142和https://developers.facebook.com/docs/facebook-pixel/implementation/conversion-tracking
发布于 2020-01-31 00:07:11
我认为这不是Facebook Events的问题,而是iFrame的问题。托管在iFrame中的url接收单击事件,并且不会将该事件冒泡到iFrame元素本身。幸运的是,对于youtube视频,您可以使用嵌入式播放器API来执行基于播放器状态的操作。试试这个:
<iframe id="my-youtube-video-id" width="560" height="315" src="https://www.youtube.com/embed/tzbrKBv-khE?enablejsapi=1" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<script>
// This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('my-youtube-video-id', {
events: {
'onStateChange': onPlayerStateChange
}
});
}
var done;
function onPlayerStateChange(event){
if (event.data == YT.PlayerState.PLAYING && !done) {
//Here is where you can add your event, we also use the done variable to make sure the event will only fire once.
fbq('track', 'ViewContent');
done = true;
}
}
</script>log to console when the onPlayerStateChange() function is called:查看这个将编辑的jsFiddle
https://stackoverflow.com/questions/59987727
复制相似问题