Skip to content
This repository has been archived by the owner on Oct 11, 2019. It is now read-only.

Wiselinks and Google Adsense #96

Open
Chmarusso opened this issue Feb 28, 2015 · 0 comments
Open

Wiselinks and Google Adsense #96

Chmarusso opened this issue Feb 28, 2015 · 0 comments

Comments

@Chmarusso
Copy link

I would like to integrate Adsense on my webpage. Problem is that GA is not supporting AJAX requests.
I've found great article covering this problem for turbolinks:
http://reed.github.io/turbolinks-compatibility/doubleclick_for_publishers.html

I tried to apply this to my app, but without success.
In the mentioned article coffee script is used, below please find example of what I tried in JS.
Console returns error:
image

#app.js 

var GooglePublisherTag;

GooglePublisherTag = (function() {
  function GooglePublisherTag() {
    $(document).on('page:loading', this.clearAds);
    $(document).on('page:done', (function(_this) {
      return function() {
        _this.evaluate();
        return _this.refreshAds();
      };
    })(this));
    this.evaluate();
  }

  GooglePublisherTag.prototype.evaluate = function() {
    var ad, _i, _len, _ref, _results;
    if (typeof googletag === "undefined" || googletag === null) {
      return;
    }
    _ref = $('.gpt-ad');
    _results = [];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      ad = _ref[_i];
      _results.push(this.initAd($(ad)));
    }
    return _results;
  };

  GooglePublisherTag.prototype.initAd = function($ad) {
    var id, path, size;
    path = $ad.data('gpt-path');
    size = $ad.data('gpt-size');
    id = $ad.attr('id');
    return googletag.cmd.push(function() {
      googletag.defineSlot(path, size, id).addService(googletag.pubads());
      googletag.enableServices();
      return googletag.display(id);
    });
  };

  GooglePublisherTag.prototype.clearAds = function() {
    if ((typeof googletag !== "undefined" && googletag !== null ? googletag.pubads : void 0) != null) {
      return googletag.pubads().clear();
    }
  };

  GooglePublisherTag.prototype.refreshAds = function() {
    if ((typeof googletag !== "undefined" && googletag !== null ? googletag.pubads : void 0) != null) {
      return googletag.pubads().refresh();
    }
  };

  return GooglePublisherTag;

})();


#application.js 

//= require app
$(document).ready(function() {
  window.wiselinks = new Wiselinks($('#main-content'));
  (function() {
    return this.googlePublisherTag || (this.googlePublisherTag = new GooglePublisherTag());
  });

  $(document).off('page:done').on('page:done', function(event, $target, status, url, data) {
  });

  $(document).off('page:loading').on('page:loading', function(event, $target, render, url) {
  });

  $(document).off('page:redirected').on('page:redirected', function(event, $target, render, url) {
  });

  $(document).off('page:always').on('page:always', function(event, xhr, settings) {

  });

});
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant