How to avoid function being called several times at the end of interactive video


I have an interactive video with a couple of fill in the blank activities and a function call at the end of an interactive video. However, the function is called several times.
My issues is that   IsAnswered && contextIsIV (see code below) is returning true for every IV activity and not just at the end of the video.

How do I only call the function after the user has ended the video and has completed at least one activity.

Here is my code:

H5P.externalDispatcher.on('xAPI', function (event) {
   // had to add a space in the URL below or the text editor would have rendered it as a link
   var IsAnswered = === 'http:  //';
   var contextIsIV;

      if( > 0){
            contextIsIV =[0].id.indexOf('H5P.InteractiveVideo') !== -1;

   if (IsAnswered && contextIsIV) {
         //function call here



What I observed:
When the interactive video doesn't have a summary task:
The function is called multiple times (for each completed activity plus one other time). Each time (e.g. contains the data of the total (of all activities combined).

When the interactive video has a summary task:
The function is called  multiple times (for each completed activity). Each time (e.g. contains only the result data of the last activity in the set (the summary task).

I also tried to remove the summary screen (because I would prefer that), but then the function is never called and is undefined.

Any help would be much appreciated.

Content types: