Last active
April 28, 2020 23:08
-
-
Save bwedding/261927f362f3c0b21e444f30364c9117 to your computer and use it in GitHub Desktop.
Facebook custom conversion based on time such as a video playing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
const VideoLenSeconds = 300; // Modify this to the length of your video in seconds. 300 = 5 mins | |
const Divisions = 10; // 10 = every 10%, 4 = every 25%, 5 = every 20% | |
const ConversionPct = 75; // Set this to record a desired conversion percentage | |
// Don't change these! | |
const MSecsPerSecond = 1000; | |
var percentageViewed = 0; | |
var converted = false; | |
function VSLconvert() | |
{ | |
percentageViewed++; | |
str = ""; | |
var FractionViewed = Math.round(percentageViewed / Divisions * 100); | |
str = FractionViewed + "%"; | |
if(percentageViewed <= Divisions) // If they just stay on the page past video time, don't fire more events | |
{ | |
fbq('trackCustom', 'VSLViewed', {viewed: str}); | |
if(FractionViewed >= ConversionPct) | |
{ | |
if(!converted) | |
{ | |
converted = true; | |
// This is the custom conversion event to optimize for | |
fbq('trackCustom', 'VSLConverted'); | |
} | |
} | |
} | |
else // They've watched it all. Stop the timer | |
{ | |
clearInterval(vslTimer); | |
} | |
} | |
// Fires every specified % of video | |
var vslTimer = setInterval(VSLconvert, VideoLenSeconds * MSecsPerSecond / Divisions); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment