Browse Source

Ticket #4561 - Ads: Enhancements.

AntonLV 2 years ago
parent
commit
40b0565556
2 changed files with 90 additions and 0 deletions
  1. 84 0
      modules/boonex/ads/js/main.js
  2. 6 0
      modules/boonex/ads/template/unit.html

+ 84 - 0
modules/boonex/ads/js/main.js

@@ -0,0 +1,84 @@
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    Ads Ads
+ * @ingroup     UnaModules
+ *
+ * @{
+ */
+
+function BxAdsMain(oOptions) {
+    this._sActionsUri = oOptions.sActionUri;
+    this._sActionsUrl = oOptions.sActionUrl;
+    this._sObjName = oOptions.sObjName == undefined ? 'oBxAdsMain' : oOptions.sObjName;
+    this._sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'slide' : oOptions.sAnimationEffect;
+    this._iAnimationSpeed = oOptions.iAnimationSpeed == undefined ? 'slow' : oOptions.iAnimationSpeed;
+    this._aHtmlIds = oOptions.aHtmlIds == undefined ? {} : oOptions.aHtmlIds;
+    this._oRequestParams = oOptions.oRequestParams == undefined ? {} : oOptions.oRequestParams;
+}
+
+BxAdsMain.prototype.registerTraker = function(iId) {
+    var $this = this;
+
+    if(typeof window.glBxAdsTracked === 'undefined')
+        window.glBxAdsTracked = [];
+
+    $(window).on('load scroll', function() {
+        var sIdAd = $this._aHtmlIds['unit'] + iId;
+
+        if(glBxAdsTracked[sIdAd])
+            return;
+
+        var oElement = document.getElementById(sIdAd);
+        if(oElement && $this.isVisible(oElement)) {
+            $this.registerImpression(oElement, iId);
+
+            glBxAdsTracked[sIdAd] = true;
+        }
+    });
+};
+
+
+
+BxAdsMain.prototype.registerImpression = function(oElement, iId) {
+    jQuery.get (
+        this._sActionsUrl + 'register_impression/' + iId,
+        this._getDefaultData(),
+        function(oData) {
+            processJsonData(oData);
+        },
+        'json'
+    );
+};
+
+BxAdsMain.prototype.isVisible = function(oElement) {
+    var rect = oElement.getBoundingClientRect();
+    var vWidth = window.innerWidth || document.documentElement.clientWidth;
+    var vHeight = window.innerHeight || document.documentElement.clientHeight;
+    var fEfp = function (x, y) { 
+        return document.elementFromPoint(x, y) 
+    };
+
+    if (rect.right < 0 || rect.bottom < 0 || rect.left > vWidth || rect.top > vHeight)
+        return false;
+
+    return (
+          oElement.contains(fEfp(rect.left,  rect.top))
+      ||  oElement.contains(fEfp(rect.right, rect.top))
+      ||  oElement.contains(fEfp(rect.right, rect.bottom))
+      ||  oElement.contains(fEfp(rect.left,  rect.bottom))
+    );
+};
+
+BxAdsMain.prototype.loadingInBlock = function(e, bShow) {
+    var oParent = $(e).length ? $(e).parents('.bx-db-container:first') : $('body'); 
+    bx_loading(oParent, bShow);
+};
+
+BxAdsMain.prototype._getDefaultData = function() {
+    var oDate = new Date();
+    return jQuery.extend({}, this._oRequestParams, {_t:oDate.getTime()});
+};
+
+/** @} */

+ 6 - 0
modules/boonex/ads/template/unit.html

@@ -0,0 +1,6 @@
+<div id="bx-ads-unit-__id__" class="bx-ads-unit-wrapper">
+  <bx_include_auto_mod_text:unit.html />
+</div>
+<script>
+    __js_object__.registerTraker(__id__);
+</script>