You are on right track. As this script runs you get your results recorded at server.
- The easiest and most common way to achieve this is to place the script on a separate page(generally a thank-you.php) and redirect user to that page so after completion of his activity we can run this script and record this activity. OR
- The other way(a tricky one though) is to make this script run on the same page using ajax/javascript after user's activity.
If you want to set it without any redirection you may find this helpful.