I have a video that is 60 seconds long. I am working on adding some JavaScript to send data from my video player to Google Analytics. What I am looking to do is to launch an event no more than once every 60 seconds for a range of time.
So for example, a user watches 21 to 40 seconds into a video, I want only one event launched (rather then 19 times for the 19 seconds of time). Then the user watches 41 to 60 seconds into the video, I want only one event launched. I actually have figured this part out already.
The problem is when it comes to time. So if a user watches the 21 to 40 seconds it launches an event. They watch the rest of the video and mentally process it and decide “hey I want to watch the video again”. If they watch the video again, that one event at 21 to 40 seconds won’t launch again unless they refresh the page.
How can I edit my script so an event is launched only once every 60 seconds for a specific range?
<script type="text/javascript">
function Follow( minimum, maximum )
{
this.min = minimum;
this.max = maximum;
this.done = false;
}
var following = [
new Follow( 1, 20 ),
new Follow( 21, 40 ),
new Follow( 41, 60 )
];
player().Time(
function(event)
{
var gone = Number(event.secondspassed);
for ( var t = 0; t < following.length; ++t )
{
var Follow = following[t];
if ( gone >= Follow.min && gone < Follow.max )
{
if ( ! Follow.done )
{
var range = Follow.min + " Seconds Watched ";
_gaq.push(['_trackEvent', range, 'Play', 'Title']);
Follow.done = true;
}
break;
}
}
}
);
</script>
You can add
this.lastto your Follow objects and record the time that thatFollowwas last fired.Then in your function you can check if it’s been over a minute since it last fired:
And finally when you set
Follow.doneto true, also set the last time it fired to the current time:The code above does what you asked for, but I’m not sure how well this will deal with pausing and continuing the video over a minute later. It will probably also fire the event twice in that situation.