Skip to content

Instantly share code, notes, and snippets.

@virusvn
Created August 8, 2017 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virusvn/73c47709b6c943eb497d8cb716701cdb to your computer and use it in GitHub Desktop.
Save virusvn/73c47709b6c943eb497d8cb716701cdb to your computer and use it in GitHub Desktop.
Execute a function to track (Facebook & Line...) iFrame's click
<!--
@Author: Nhan Nguyen
@Description: The library iframetracker is good, but because the script to generate iframe is asynchronous,
so we can't setup $(document).ready() work properly.
The solution is combined:
- Track by tagName is IFRAME for the first click
- Then, track by iframeTracker for nth times click
-->
<!-- https://github.com/vincepare/iframeTracker-jquery -->
<script src="/js/jquery.iframetracker.js"></script>
<script>
/* Start SNS Tracking */
$(document).ready(function(){
var fbTrackCode = '(share)something_to_identify_your_product_fb';
var lineTrackCode = '(share)something_to_identify_your_product_line';
//Traking first time click
var monitor = setInterval(function(){
var elem = document.activeElement;
if(elem && elem.tagName == 'IFRAME'){
//Iframe is Facebook
if(elem.src && elem.src.indexOf('facebook') !== -1){
s.trackButton(this,fbTrackCode); // Change to your code
}
//Iframe is Line
else if(elem.src && elem.src.indexOf('line') !== -1){
s.trackButton(this,lineTrackCode); // Change to your code
}
clearInterval(monitor);
//Line: Traking nth times click with iframeTracker solution
$('.line iframe').iframeTracker({
blurCallback: function(){
s.trackButton(this,lineTrackCode); // Change to your code
}
});
//Facebook: Traking nth times click with iframeTracker solution
$('.facebook iframe').iframeTracker({
blurCallback: function(){
s.trackButton(this,fbTrackCode);// Change to your code
}
});
}
}, 100);
})
/* End SNS Tracking */
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment