Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. (function(factory) {
  3.   if (typeof define === 'function' && define.amd) {
  4.     // AMD
  5.     define(['jquery'], factory);
  6.   } else if (typeof module === 'object' && module.exports) {
  7.     // CommonJS
  8.     module.exports = factory(require('jquery'));
  9.   } else {
  10.     // Browser globals
  11.     factory(jQuery);
  12.   }
  13. }(function($) {
  14.  
  15.   /* Screentime */
  16.  
  17.   var defaults = {
  18.     fields: [],
  19.     percentOnScreen: '50%',
  20.     reportInterval: 10,
  21.     googleAnalytics: false,
  22.     callback: function(){}
  23.   };
  24.  
  25.   $.screentime = function(options) {
  26.     options = $.extend({}, defaults, options);
  27.  
  28.     // Convert perecent string to number
  29.     options.percentOnScreen = parseInt(options.percentOnScreen.replace('%', ''), 10);
  30.  
  31.     var counter = {};
  32.     var cache = {};
  33.     var log = {};
  34.     var looker = null;
  35.     var started = false;
  36.     var universalGA, classicGA;
  37.  
  38.     if (!options.fields.length) {
  39.       return;
  40.     }
  41.  
  42.     if (options.googleAnalytics) {
  43.  
  44.       if (typeof ga === "function") {
  45.         universalGA = true;
  46.       }
  47.  
  48.       if (typeof _gaq !== "undefined" && typeof _gaq.push === "function") {
  49.         classicGA = true;
  50.       }
  51.  
  52.     }
  53.  
  54.     /*
  55.      * Utilities
  56.      */
  57.  
  58.     /*!
  59.      * visibly - v0.6 Aug 2011 - Page Visibility API Polyfill
  60.      * http://github.com/addyosmani
  61.      * Copyright (c) 2011 Addy Osmani
  62.      * Dual licensed under the MIT and GPL licenses.
  63.      *
  64.      * Methods supported:
  65.      * visibly.onVisible(callback)
  66.      * visibly.onHidden(callback)
  67.      * visibly.hidden()
  68.      * visibly.visibilityState()
  69.      * visibly.visibilitychange(callback(state));
  70.      */
  71.  
  72.     (function(){window.visibly={q:document,p:undefined,prefixes:["webkit","ms","o","moz","khtml"],props:["VisibilityState","visibilitychange","Hidden"],m:["focus","blur"],visibleCallbacks:[],hiddenCallbacks:[],genericCallbacks:[],_callbacks:[],cachedPrefix:"",fn:null,onVisible:function(i){if(typeof i=="function"){this.visibleCallbacks.push(i)}},onHidden:function(i){if(typeof i=="function"){this.hiddenCallbacks.push(i)}},getPrefix:function(){if(!this.cachedPrefix){for(var i=0;b=this.prefixes[i++];){if(b+this.props[2]in this.q){this.cachedPrefix=b;return this.cachedPrefix}}}},visibilityState:function(){return this._getProp(0)},hidden:function(){return this._getProp(2)},visibilitychange:function(i){if(typeof i=="function"){this.genericCallbacks.push(i)}var t=this.genericCallbacks.length;if(t){if(this.cachedPrefix){while(t--){this.genericCallbacks[t].call(this,this.visibilityState())}}else{while(t--){this.genericCallbacks[t].call(this,arguments[0])}}}},isSupported:function(i){return this.cachedPrefix+this.props[2]in this.q},_getProp:function(i){return this.q[this.cachedPrefix+this.props[i]]},_execute:function(i){if(i){this._callbacks=i==1?this.visibleCallbacks:this.hiddenCallbacks;var t=this._callbacks.length;while(t--){this._callbacks[t]()}}},_visible:function(){window.visibly._execute(1);window.visibly.visibilitychange.call(window.visibly,"visible")},_hidden:function(){window.visibly._execute(2);window.visibly.visibilitychange.call(window.visibly,"hidden")},_nativeSwitch:function(){this[this._getProp(2)?"_hidden":"_visible"]()},_listen:function(){try{if(!this.isSupported()){if(this.q.addEventListener){window.addEventListener(this.m[0],this._visible,1);window.addEventListener(this.m[1],this._hidden,1)}else{if(this.q.attachEvent){this.q.attachEvent("onfocusin",this._visible);this.q.attachEvent("onfocusout",this._hidden)}}}else{this.q.addEventListener(this.cachedPrefix+this.props[1],function(){window.visibly._nativeSwitch.apply(window.visibly,arguments)},1)}}catch(i){}},init:function(){this.getPrefix();this._listen()}};this.visibly.init()})();
  73.  
  74.     function random() {
  75.       return Math.round(Math.random() * 2147483647);
  76.     }
  77.  
  78.     /*
  79.      * Constructors
  80.      */
  81.  
  82.     function Field(elem) {
  83.       this.selector = elem.selector;
  84.       $elem = this.$elem = $(elem.selector);
  85.       this.name = elem.name;
  86.  
  87.       this.top = $elem.offset().top;
  88.       this.height = $elem.height();
  89.       this.bottom = this.top + this.height;
  90.       this.width = $elem.width();
  91.     }
  92.  
  93.     function Viewport() {
  94.       var $window = $(window);
  95.  
  96.       this.top = $window.scrollTop();
  97.       this.height = $window.height();
  98.       this.bottom = this.top + this.height;
  99.       this.width = $window.width();
  100.     }
  101.  
  102.     /*
  103.      * Do Stuff
  104.      */
  105.  
  106.     function sendGAEvent(field, time) {
  107.  
  108.       if (universalGA) {
  109.         ga('send', 'event', 'Screentime', 'Time on Screen', field, parseInt(time, 10), {'nonInteraction': true});
  110.       }
  111.  
  112.       if (classicGA) {
  113.         _gaq.push(['_trackEvent', 'Screentime', 'Time on Screen', field, parseInt(time, 10), true]);
  114.       }
  115.  
  116.     }
  117.  
  118.     function onScreen(viewport, field) {
  119.       var cond, buffered, partialView;
  120.  
  121.       // Field entirely within viewport
  122.       if ((field.bottom <= viewport.bottom) && (field.top >= viewport.top)) {
  123.         return true;
  124.       }
  125.  
  126.        // Field bigger than viewport
  127.       if (field.height > viewport.height) {
  128.  
  129.         cond = (viewport.bottom - field.top) > (viewport.height / 2) && (field.bottom - viewport.top) > (viewport.height / 2);
  130.  
  131.         if (cond) {
  132.           return true;
  133.         }
  134.  
  135.       }
  136.  
  137.       // Partially in view
  138.       buffered = (field.height * (options.percentOnScreen/100));
  139.       partialView = ((viewport.bottom - buffered) >= field.top && (field.bottom - buffered) > viewport.top);
  140.  
  141.       return partialView;
  142.  
  143.     }
  144.  
  145.     function checkViewport() {
  146.       var viewport = new Viewport();
  147.  
  148.       $.each(cache, function(key, val) {
  149.         if (onScreen(viewport, val)) {
  150.           log[key] += 1;
  151.           counter[key] += 1;
  152.         }
  153.       });
  154.  
  155.     }
  156.  
  157.     function report() {
  158.  
  159.       var data = {};
  160.  
  161.       $.each(counter, function(key, val) {
  162.         if (val > 0) {
  163.           data[key] = val;
  164.           counter[key] = 0;
  165.  
  166.           if (options.googleAnalytics) {
  167.             sendGAEvent(key, val);
  168.           }
  169.  
  170.         }
  171.       });
  172.  
  173.       if (!$.isEmptyObject(data)) {
  174.         options.callback.call(this, data, log);
  175.       }
  176.  
  177.     }
  178.  
  179.     function startTimers() {
  180.  
  181.       if (!started) {
  182.         checkViewport();
  183.         started = true;
  184.       }
  185.  
  186.       looker = setInterval(function() {
  187.         checkViewport();
  188.       }, 1000);
  189.  
  190.       reporter = setInterval(function() {
  191.         report();
  192.       }, options.reportInterval * 1000);
  193.  
  194.     }
  195.  
  196.     function stopTimers() {
  197.       clearInterval(looker);
  198.       clearInterval(reporter);
  199.     }
  200.  
  201.     $.screentime.reset = function() {
  202.       stopTimers();
  203.  
  204.       $.each(cache, function(key, val) {
  205.         log[key] = 0;
  206.         counter[key] = 0;
  207.       });
  208.  
  209.       startTimers();
  210.     }
  211.  
  212.     function init() {
  213.  
  214.       $.each(options.fields, function(index, elem) {
  215.         if ($(elem.selector).length) {
  216.           var field = new Field(elem);
  217.           cache[field.name] = field;
  218.           counter[field.name] = 0;
  219.           log[field.name] = 0;
  220.         }
  221.       });
  222.  
  223.       startTimers();
  224.  
  225.       visibly.onHidden(function() {
  226.         stopTimers();
  227.       });
  228.  
  229.       visibly.onVisible(function() {
  230.         stopTimers();
  231.         startTimers();
  232.       });
  233.  
  234.     }
  235.  
  236.     init();
  237.  
  238.   };
  239.  
  240. }));
  241.  
  242. $.screentime({
  243.   fields: [
  244.     { selector: 'div.wysiwyg-header',
  245.       name: '1header'
  246.     },
  247.     { selector: '#tariff-switcher',
  248.       name: '2tariff-switcher'
  249.     },
  250.     { selector: '#cashback_calculator',
  251.       name: '3cashback_calculator'
  252.     },
  253.     { selector: '.section.open-slider-container',
  254.       name: '4additional'
  255.     },
  256.     { selector: '.section.section--white'[2],
  257.       name: '5tariffs'
  258.     },
  259.     { selector: '.section.section--grey',
  260.       name: '6howto'
  261.     },
  262.     { selector: '.iframe-block__container',
  263.       name: '7form'
  264.     }
  265.   ],
  266.   callback: function(data) {
  267.     console.log(data);
  268.   }
  269. });
  270. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement