lib/yandex/metrika.rb in yandex_metrika-0.0.2 vs lib/yandex/metrika.rb in yandex_metrika-0.0.3
- old
+ new
@@ -67,22 +67,43 @@
# Return true if the Yandex.Metrika system is enabled and configured
# correctly for the specified format
def self.enabled?(format)
raise ConfigurationError if counter_id.blank?
- environments.include?(Rails.env) && formats.include?(format.to_sym)
+ environments.include?(Rails.env) && formats.include?(format.to_s.to_sym)
end
# Construct the javascript code to be inserted on the calling page.
def self.code
<<-EOHTML
- <!-- Yandex.Metrika -->
- <script src="//mc.yandex.ru/resource/watch.js" type="text/javascript"></script>
+ <!-- Yandex.Metrika counter -->
<script type="text/javascript">
- try { var yaCounter#{counter_id} = new Ya.Metrika(#{counter_id}); } catch(e){}
+ (function (d, w, c) {
+ (w[c] = w[c] || []).push(function() {
+ try {
+ w.yaCounter#{counter_id} = new Ya.Metrika({id:#{counter_id},
+ webvisor:true,
+ clickmap:true,
+ trackLinks:true,
+ accurateTrackBounce:true,
+ trackHash:true});
+ } catch(e) { }
+ });
+
+ var n = d.getElementsByTagName("script")[0],
+ s = d.createElement("script"),
+ f = function () { n.parentNode.insertBefore(s, n); };
+ s.type = "text/javascript";
+ s.async = true;
+ s.src = (d.location.protocol == "https:" ? "https:" : "http:") + "//mc.yandex.ru/metrika/watch.js";
+
+ if (w.opera == "[object Opera]") {
+ d.addEventListener("DOMContentLoaded", f, false);
+ } else { f(); }
+ })(document, window, "yandex_metrika_callbacks");
</script>
- <noscript><div style="position: absolute;"><img src="//mc.yandex.ru/watch/#{counter_id}" alt="" /></div></noscript>
- <!-- /Yandex.Metrika -->
+ <noscript><div><img src="//mc.yandex.ru/watch/#{counter_id}" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
+ <!-- /Yandex.Metrika counter -->
EOHTML
end
end
end