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 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 http://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
  "<script type=\"text/javascript\">\nvar gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");\ndocument.write(unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));\n</script>\n<script type=\"text/javascript\">\ntry {\nvar pageTracker = _gat.\#{get_tracker};\npageTracker._trackPageview();\n} catch(err) {}</script>\n  EOT\nend\n"