Class: GoogleAnalyticsOnRails::JavaScriptBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/google_analytics_on_rails/java_script_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(context, profile) ⇒ JavaScriptBuilder

Returns a new instance of JavaScriptBuilder.



21
22
23
24
# File 'lib/google_analytics_on_rails/java_script_builder.rb', line 21

def initialize(context, profile)
  @context = context
  @profile = profile
end

Instance Method Details

#get_trackerObject

Returns a _getTracker statement for the tracker_id stored in current @profile.

JavaScriptBuilder.new(context, GoogleAnalytics.new('ua_123456').get_tracker
# => _getTracker('ua_123456')

Raises

GoogleAnalyticsOnRails::InvalidProfile

If @profile is missing tracker_id



70
71
72
73
# File 'lib/google_analytics_on_rails/java_script_builder.rb', line 70

def get_tracker
  require_valid_profile!
  get_tracker_for(@profile.tracker_id)
end

#tracking_codeObject

Returns the Google Analytics tracking code for the tracker_id stored in current @profile.

Important: this method tried to keep the original Google Analytics tracking code unchanged as much as possibile. For this reason, this method return the tracking code already wrapped by all required <script></script> tags.

JavaScriptBuilder.new(context, GoogleAnalytics.new('ua_123456').get_tracker
# => <script> var gaJsHost = ... </script>

This method returns the new Google Analytics tracking code, the one based on ga.js inclusion. The old urchin.js file is no longer supported.

Last updated on January 16, 2009. To lean more visit analytics.blogspot.com/2009/01/short-answer-is-you-dont-have-to-change.html

TODO: update the code to be based on javascript_tag helper.

Raises

GoogleAnalyticsOnRails::InvalidProfile

If @profile is missing tracker_id



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/google_analytics_on_rails/java_script_builder.rb', line 47

def tracking_code
  <<-EOT
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat.#{get_tracker};
pageTracker._trackPageview();
} catch(err) {}</script>
  EOT
end