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

Forums: 

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 = event.data.statement.verb.id === 'http:  //adlnet.gov/expapi/verbs/completed';
   var contextIsIV;


   if(event.data.statement.context.contextActivities.category){
      if(event.data.statement.context.contextActivities.category.length > 0){
         if(event.data.statement.context.contextActivities.category[0].id){
            contextIsIV = event.data.statement.context.contextActivities.category[0].id.indexOf('H5P.InteractiveVideo') !== -1;
         }
      }
   }

   if (IsAnswered && contextIsIV) {
      if(event.data.statement.result){
         //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 event.data.statement.result (e.g. event.data.statement.result.score.raw) 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 event.data.statement.result (e.g. event.data.statement.result.score.raw) 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 event.data.statement.result is undefined.

Any help would be much appreciated.

Content types: