2016-02-26 4 views
5

मेरे पास एक रेल 4.2 ऐप है जिसे मैंने हेरोकू के साथ तैनात किया है और मैंने इसे Google Analytics जोड़ने की कोशिश की है। हालांकि, Google Analytics कोई सत्र नहीं उठा रहा है।रेल के लिए Google Analytics जोड़ना 4.2 ऐप

कोई सुझाव यह क्यों और कैसे हल करें?

कोड

/app/layouts/_footer.html.erb:

<% if Rails.env.production? %> 
    <script> 
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ 
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), 
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) 
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); 

    ga('create', 'UA-XXXXXXXX-X', 'herokuapp.com'); 
    ga('send', 'pageview'); 

    </script> 
<% end %> 

/app/layouts/application.html.erb:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Title</title> 
<%= Gon::Base.render_data({}) %> 
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> 
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> 
    <%= csrf_meta_tags %> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
</head> 
<body> 

<div class="container"> 
    <%= yield %> 
</div> 

<%= render 'layouts/footer' %> 

</body> 
</html> 

/ऐप्स/संपत्ति/जावास्क्रिप्ट/analytics.js.coffee:

$(document).on 'page:change', -> 
if window._gaq? 
    _gaq.push ['_trackPageview'] 
else if window.pageTracker? 
    pageTracker._trackPageview() 

/app/assets/javascripts/application.js:

//= require jquery 
//= require analytics 
//= require bootstrap 
//= require jquery_ujs 
//= require jquery.ui.all 
//= require Chart 
//= require jquery.turbolinks 
//= require lodash 
//= require_tree . 

Gemfile: के बाद से यह Turbolinks की वजह से काम नहीं करेगा

source 'https://rubygems.org' 

gem 'rails',    '4.2.2' 
gem 'bootstrap-sass',  '3.2.0.0' 
gem 'sass-rails',   '5.0.2' 
gem 'uglifier',    '2.5.3' 
gem 'coffee-rails',   '4.1.0' 
gem 'jquery-rails',   '4.0.3' 
gem 'jquery-ui-rails', '~> 4.2.1' 
gem 'turbolinks',   '2.3.0' 
gem 'jquery-turbolinks' 
gem 'jbuilder',    '2.2.3' 
gem 'sdoc',     '0.4.0', group: :doc 
gem 'chart-js-rails' 
gem 'gon' 
gem 'lodash-rails' 

group :development, :test do 
    gem 'sqlite3',  '1.3.9' 
    gem 'byebug',  '3.4.0' 
    gem 'web-console', '2.0.0.beta3' 
    gem 'spring',  '1.1.3' 
end 

group :production do 
    gem 'pg',    '0.17.1' 
    gem 'rails_12factor', '0.0.2' 
end 
+0

बस इतना ही आप जानते हैं, आप इस प्रश्न में Google Analytics वेब ट्रैकिंग पुस्तकालयों के दो अलग-अलग संस्करणों को मिश्रित कर रहे हैं। आपके '_footer.html.erb' में आप [analytics.js ट्रैकिंग स्निपेट] (https://developers.google.com/analytics/devguides/collection/analyticsjs/) का उपयोग कर रहे हैं, लेकिन आपके 'analytics.js में। कॉफी 'फ़ाइल आप विरासत ga.js कोड का उपयोग कर रहे हैं, जो कुछ भी नहीं करेगी क्योंकि आपने ga.js लाइब्रेरी को कभी लोड नहीं किया है, आपने analytics.js लोड किया है। –

उत्तर

7

तरह से आप अपने स्क्रिप्ट डाल रहे हैं, गलत तरीके से है , आप जानते हैं कि आपके _footer में आपके पास कोड के रूप में अपेक्षित नहीं चल रहा है क्योंकि मार्कअप के उस टुकड़े को टर्बोलिंक्स के कारण पुनः लोड नहीं किया जाएगा।

मैं आपको अपने सभी रेल ऐप्स के लिए Google Analytics का अपना कस्टम कार्यान्वयन दूंगा।

अपने अनुप्रयोग/आस्तियों/javascripts में एक नई फ़ाइल कहा जाता google_analytics.js.coffee बना सकते हैं और निम्न स्क्रिप्ट जोड़ें: आप देख सकते हैं यह Turbolinks के लिए समर्थन जोड़ता

class @GoogleAnalytics 

    @load: -> 
    # Google Analytics depends on a global _gaq array. window is the global scope. 
    window._gaq = [] 
    window._gaq.push ["_setAccount", GoogleAnalytics.analyticsId()] 

    # Create a script element and insert it in the DOM 
    ga = document.createElement("script") 
    ga.type = "text/javascript" 
    ga.async = true 
    ga.src = ((if "https:" is document.location.protocol then "https://ssl" else "http://www")) + ".google-analytics.com/ga.js" 
    firstScript = document.getElementsByTagName("script")[0] 
    firstScript.parentNode.insertBefore ga, firstScript 

    # If Turbolinks is supported, set up a callback to track pageviews on page:change. 
    # If it isn't supported, just track the pageview now. 
    if typeof Turbolinks isnt 'undefined' and Turbolinks.supported 
     document.addEventListener "page:change", (-> 
     GoogleAnalytics.trackPageview() 
    ), true 
    else 
     GoogleAnalytics.trackPageview() 

    @trackPageview: (url) -> 
    unless GoogleAnalytics.isLocalRequest() 
     if url 
     window._gaq.push ["_trackPageview", url] 
     else 
     window._gaq.push ["_trackPageview"] 
     window._gaq.push ["_trackPageLoadTime"] 

    @isLocalRequest: -> 
    GoogleAnalytics.documentDomainIncludes "local" 

    @documentDomainIncludes: (str) -> 
    document.domain.indexOf(str) isnt -1 

    @analyticsId: -> 
    # your google analytics ID(s) here... 
    'UA-xxxxxxxx-x' 

GoogleAnalytics.load() 

, यह एक स्वच्छ और बेहतर है अपने ऐप में Google Analytics जोड़ने का तरीका। आशा है कि यह आप के लिए काम करता है।

+0

धन्यवाद। और '/app/assets/javascripts/application.js में: 'फ़ाइल को' // = आवश्यकता विश्लेषण 'के साथ' // = आवश्यकता विश्लेषिकी 'को प्रतिस्थापित करना चाहिए? – Bhav

+1

आपको अपने application.js को संशोधित करने की आवश्यकता नहीं है क्योंकि आपके पास पहले से ही // // = requ_tree है। 'आपको' // = एनालिटिक्स की आवश्यकता नहीं है 'की आवश्यकता नहीं है ... – SsouLlesS

+0

@SsouLlesS बहुत अच्छा लग रहा है! क्या उत्पादन वातावरण में इसे सीमित करने का कोई तरीका है? – slehmann36