Module: AnalyticsInstrumentation
- Includes:
- AnalyticsAttribution
- Included in:
- ApplicationController
- Defined in:
- lib/analytics_instrumentation.rb,
lib/analytics_instrumentation/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
Instance Method Summary collapse
- #analyticsAliasUser(user_id) ⇒ Object
- #analyticsApplyOriginatingPage(properties) ⇒ Object
- #analyticsCheckSessionStart ⇒ Object
- #analyticsID ⇒ Object
- #analyticsLogPageView ⇒ Object
- #analyticsSetPerson(user) ⇒ Object
- #analyticsStoreOriginatingPage(page_view_event) ⇒ Object
- #analyticsSuperProperties ⇒ Object
- #analyticsTrackEvent(name, properties = {}) ⇒ Object
- #raw_analytics_id ⇒ Object
Methods included from AnalyticsAttribution
Class Method Details
.configure {|@config| ... } ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/analytics_instrumentation.rb', line 20 def configure(&proc) @config ||= Config.new yield @config unless @config.valid? errors = @config.errors..join(', ') raise Config::Invalid.new(errors) end end |
.included(base) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/analytics_instrumentation.rb', line 8 def included(base) @segment = Segment::Analytics.new({ write_key: @config.segment_write_key, on_error: @config.error_handler }) base.class_eval do base.send(:after_filter, :analyticsLogPageView) base.send(:after_filter, :analyticsCheckSessionStart) end end |
Instance Method Details
#analyticsAliasUser(user_id) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/analytics_instrumentation.rb', line 91 def analyticsAliasUser(user_id) return if skip_analytics? aliasProperties = { previous_id: session[:analytics_id], user_id: user_id } logger.debug "Analytics.alias #{aliasProperties}" @segment.alias(aliasProperties) @segment.flush end |
#analyticsApplyOriginatingPage(properties) ⇒ Object
86 87 88 89 |
# File 'lib/analytics_instrumentation.rb', line 86 def analyticsApplyOriginatingPage(properties) properties["Originating Page Identifier"] = session["previous-page-identifier"] properties["Originating Page Type"] = session["previous-page-type"] end |
#analyticsCheckSessionStart ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/analytics_instrumentation.rb', line 31 def analyticsCheckSessionStart begin return if skip_analytics? if current_user if !session[:last_seen] || session[:last_seen] < 30.minutes.ago analyticsTrackEvent("Session Start") if @config.intercom? && Rails.env.production? Intercom.post("https://api.intercom.io/users", {user_id:current_user.id, new_session:true}) end end session[:last_seen] = Time.now else if session[:last_seen_logged_out].nil? || session[:last_seen_logged_out] < 30.minutes.ago analyticsTrackEvent("Session Start") end session[:last_seen_logged_out] = Time.now end rescue => e @config.error_handler(e, "Analytics Check Session Crash: #{request.filtered_path}") end end |
#analyticsID ⇒ Object
168 169 170 171 |
# File 'lib/analytics_instrumentation.rb', line 168 def analyticsID if current_user then return current_user.id end raw_analytics_id end |
#analyticsLogPageView ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/analytics_instrumentation.rb', line 53 def analyticsLogPageView begin return if skip_analytics? return if self.status >= 400 page_view_event = AnalyticsMapping.to_event(params, self.view_assigns) if page_view_event if current_user analyticsSetPerson(current_user) end add_attribution page_view_event[:parameters] analyticsTrackEvent page_view_event[:name], page_view_event[:parameters] analyticsStoreOriginatingPage page_view_event end properties = { page: request.path } properties.merge! analyticsSuperProperties analyticsTrackEvent "Page View", properties rescue => e @config.error_handler(e, "Analytics Crash: #{request.filtered_path}") end end |
#analyticsSetPerson(user) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/analytics_instrumentation.rb', line 104 def analyticsSetPerson(user) return if skip_analytics? properties = { user_id: user.id, traits: @config.custom_user_traits(user) } logger.debug "Analytics.identify #{JSON.pretty_generate(properties)}" @segment.identify(properties) end |
#analyticsStoreOriginatingPage(page_view_event) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/analytics_instrumentation.rb', line 79 def analyticsStoreOriginatingPage(page_view_event) if !request.xhr? session["previous-page-type"] = page_view_event[:name] session["previous-page-identifier"] = page_view_event[:page_identifier] end end |
#analyticsSuperProperties ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/analytics_instrumentation.rb', line 116 def analyticsSuperProperties superProperties = { "Raw Analytics ID" => raw_analytics_id, "Ajax" => !request.xhr?.nil? } if current_user superProperties.merge!({ "User Created At" => current_user.created_at, "Username" => current_user.try(:username), "Full name" => current_user.try(:full_name), "User ID" => current_user.id, "Login Provider" => current_user.try(:provider) || "Email" }) end superProperties end |
#analyticsTrackEvent(name, properties = {}) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/analytics_instrumentation.rb', line 133 def analyticsTrackEvent(name, properties={}) return if skip_analytics? properties ||= {} properties["logged_in"] = !!current_user properties["source"] = params[:source] if params[:source] properties.merge! analyticsSuperProperties properties.merge! @config.extra_event_properties analyticsApplyOriginatingPage properties analyticsProperties = { user_id: analyticsID, event: name, properties: properties, context: { userAgent: request.env['HTTP_USER_AGENT'], ip: request.remote_ip, 'Google Analytics' => { clientId: googleAnalyticsID } } } logger.debug "Analytics.track #{JSON.pretty_generate(analyticsProperties)}" @segment.track(analyticsProperties) end |
#raw_analytics_id ⇒ Object
163 164 165 166 |
# File 'lib/analytics_instrumentation.rb', line 163 def raw_analytics_id session[:analytics_id] ||= (rand * 1000000000000000).to_i session[:analytics_id] end |