Permalink
Browse files

Analytics and Intercom

  • Loading branch information...
aivils authored and valdis committed Oct 13, 2017
1 parent e1c42dd commit ec207ee5f1924930f8a994e0385dbf2390435225
Showing with 959 additions and 222 deletions.
  1. +3 −0 .gitignore
  2. +1 −2 app/assets/javascripts/admin/manage_members.js
  3. +155 −0 app/assets/javascripts/analytics.js
  4. +1 −0 app/assets/javascripts/application.js
  5. +4 −23 app/assets/javascripts/kassi.js
  6. +1 −2 app/assets/javascripts/listing_form.js
  7. +1 −1 app/assets/javascripts/transaction.js
  8. +1 −1 app/controllers/accept_preauthorized_conversations_controller.rb
  9. +4 −2 app/controllers/admin/communities_controller.rb
  10. +6 −3 app/controllers/admin/community_customizations_controller.rb
  11. +1 −2 app/controllers/admin/custom_fields_controller.rb
  12. +2 −2 app/controllers/admin/listing_shapes_controller.rb
  13. +2 −2 app/controllers/admin/payment_preferences_controller.rb
  14. +13 −35 app/controllers/application_controller.rb
  15. +1 −1 app/controllers/community_memberships_controller.rb
  16. +2 −2 app/controllers/confirmations_controller.rb
  17. +1 −1 app/controllers/invitations_controller.rb
  18. +3 −2 app/controllers/listings_controller.rb
  19. +3 −3 app/controllers/people_controller.rb
  20. +2 −2 app/controllers/preauthorize_transactions_controller.rb
  21. +4 −1 app/controllers/sessions_controller.rb
  22. +2 −2 app/controllers/transactions_controller.rb
  23. +27 −0 app/helpers/analytics_helper.rb
  24. +1 −0 app/models/community.rb
  25. +3 −0 app/models/community_membership.rb
  26. +2 −0 app/models/email.rb
  27. +4 −0 app/models/marketplace_sender_email.rb
  28. +22 −0 app/services/analytic_service.rb
  29. +19 −0 app/services/analytic_service/api/api.rb
  30. +115 −0 app/services/analytic_service/api/intercom.rb
  31. +17 −0 app/services/analytic_service/community_customizations.rb
  32. +19 −0 app/services/analytic_service/community_look_and_feel.rb
  33. +29 −0 app/services/analytic_service/incremental_properties.rb
  34. +76 −0 app/services/analytic_service/person_attributes.rb
  35. +18 −5 app/utils/analytics.rb
  36. +4 −32 app/views/{layouts → analytics}/_amplitude.erb
  37. +9 −0 app/views/analytics/_bottom_scripts.haml
  38. +5 −0 app/views/analytics/_customer_analytics.haml
  39. +1 −1 app/views/{layouts → analytics}/_google_analytics_script.haml
  40. +10 −6 app/views/analytics/_google_tag_manager.haml
  41. +24 −0 app/views/analytics/_head_scripts.haml
  42. +8 −0 app/views/analytics/_intercom.erb
  43. +15 −0 app/views/analytics/_kissmetrics.haml
  44. +5 −0 app/views/analytics/_legacy_google_analytics.haml
  45. +1 −1 app/views/community_memberships/pending_consent.haml
  46. +1 −1 app/views/landing_page/landing_page.erb
  47. +1 −5 app/views/layouts/_head.haml
  48. +0 −25 app/views/layouts/_intercom.erb
  49. +0 −25 app/views/layouts/_kissmetrics.haml
  50. +0 −12 app/views/layouts/_marketplace_head.haml
  51. +2 −9 app/views/layouts/application.haml
  52. +1 −11 app/views/layouts/react_page.haml
  53. +133 −0 docs/analytics-events.md
  54. +35 −0 spec/factories.rb
  55. +32 −0 spec/services/analytics_service/api/api_spec.rb
  56. +107 −0 spec/services/analytics_service/person_attributes_spec.rb
View
@@ -66,3 +66,6 @@ client/node_modules
# Mocha results
test-results.xml
.byebug_history
@@ -70,7 +70,6 @@ window.ST.initializeManageMembers = function() {
// Attach analytics click handler for CSV export
$(".js-users-csv-export").click(function(){
/* global report_analytics_event */
report_analytics_event('admin', 'export', 'users');
window.ST.analytics.logEvent('admin', 'export', 'users');
});
};
@@ -0,0 +1,155 @@
window.ST = window.ST || {};
window.ST.analytics = (function(){
var init = function(options) {
$(document).ready(function() {
$(document).trigger('st-analytics:setup', options.analyticsData);
if (options.events) {
for(var i = 0; i < data.length; i++) {
var _event = data[i];
logEvent( _event.event, _event.action, null, _event.props);
}
}
if (options.logout) {
logout();
}
});
};
var logEvent = function(category, action, opt_label, props) {
$(document).trigger('st-analytics:event', {category: category, action: action, opt_label: opt_label, props: props});
};
var logout = function() {
$(document).trigger('st-analytics:logout');
};
var initAmplitude = function(amplitudeApiKey) {
var ampClient;
if (window.amplitude) {
ampClient = amplitude.getInstance();
ampClient.init(amplitudeApiKey);
} else {
return;
}
$(document).on("st-analytics:setup", function(event, info) {
var userInfo = new amplitude.Identify()
.set('community_id', info.community_id)
.set('marketplace_uuid', info.community_uuid)
.set('admin', info.user_is_admin);
if (info.plan_status) {
userInfo.set('plan_status', info.plan_status);
}
if (info.user_uuid) {
ampClient.setUserId(info.user_uuid);
}
ampClient.identify(userInfo);
});
$(document).on("st-analytics:event", function(event, args){
ampClient.logEvent(args.category, args.props);
});
$(document).on("st-analytics:logout", function(event, args){
ampClient.setUserId(null);
ampClient.regenerateDeviceid();
});
};
var initKissmetrics = function(kmq) {
$(document).on("st-analytics:setup", function(event, info) {
if(info.user_id) {
kmq.push(['identify', info.user_id]);
}
if(info.community_ident) {
kmq.push(['set', {'SiteName' : info.community_ident}]);
} else {
kmq.push(['set', {'SiteName' : 'dashboard'}]);
}
});
};
var initGoogleAnalytic = function() {
$(document).on('st-analytics:event', function(event, args) {
ST.customerReportEvent(args.category, args.action, args.opt_label);
});
};
var initLegacyGoogleAnalytic = function(gaq) {
$(document).on('st-analytics:event', function(event, args) {
var params_array = [args.category, args.action, args.opt_label];
if (typeof gaq !== 'undefined' && Array.isArray(_gaq)) {
gaq.push(['_trackEvent'].concat(params_array));
}
});
};
var initGoogleTagManager= function(gtm_identify) {
$(document).on('st-analytics:event', function(event, args) {
if (args.category == 'km_record') {
var data = $.extend({}, args.props);
data.event = 'km_record';
window.ST.gtmPush(data);
} else {
window.ST.gtmPush({
'event' : 'GAEvent',
'eventCategory' : args.category,
'eventAction' : args.saction,
'eventLabel' : args.opt_label,
'eventValue' : undefined
});
}
});
$(document).on('st-analytics:setup', function(event, info) {
_gtm_identify(dataLayer, info.community_id, info.feature_flags);
});
};
var initIntercom = function(APP_ID) {
$(document).on("st-analytics:setup", function(event, info) {
window.intercomSettings = Object.assign({
// Identifier
user_id: info.user_uuid,
user_hash: info.user_hash,
// Standard Intercom fields
app_id: APP_ID,
email: info.user_email,
name: info.user_name,
// Custom attributes ('info_' prefix)
info_plan_status: info.plan_status,
info_plan_features: info.plan_features,
info_plan_member_limit: info.plan_member_limit,
info_plan_created_at: info.plan_created_at,
info_plan_updated_at: info.plan_updated_at,
info_plan_expires_at: info.plan_expires_at,
info_feature_flags: info.feature_flags,
info_trial_creation_status: "complete"
}, info.identity_information);
/*jshint ignore:start*/
(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/' + APP_ID;var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})();
/*jshint ignore:end*/
});
$(document).on("st-analytics:event", function(event, args) {
window.Intercom('trackEvent', args.category, args.props ? args.props : args);
});
};
return {
"init": init,
"logEvent": logEvent,
"logout": logout,
"initAmplitude": initAmplitude,
"initKissmetrics": initKissmetrics,
"initGoogleAnalytic": initGoogleAnalytic,
"initLegacyGoogleAnalytic": initLegacyGoogleAnalytic,
"initGoogleTagManager": initGoogleTagManager,
"initIntercom": initIntercom
};
})();
@@ -92,4 +92,5 @@
//= require radio_buttons
//= require new_layout
//= require stripe_form
//= require analytics
//= require_self
@@ -187,25 +187,6 @@ function initialize_defaults(locale) {
});
}
function report_analytics_event(category, action, opt_label) {
if (window.ST != null && typeof window.ST.gtmPush === 'function') {
window.ST.gtmPush({
'event' : 'GAEvent',
'eventCategory' : category,
'eventAction' : action,
'eventLabel' : opt_label,
'eventValue' : undefined
});
}
var params_array = [category, action, opt_label];
if (typeof _gaq !== 'undefined' && Array.isArray(_gaq)) {
_gaq.push(['_trackEvent'].concat(params_array));
}
if (typeof ST.customerReportEvent === 'function') {
ST.customerReportEvent(category, action, opt_label);
}
}
function initialize_network_defaults(required_message, email_message) {
enableSamePageScroll();
}
@@ -274,7 +255,7 @@ function initialize_send_message_form(locale) {
},
submitHandler: function(form) {
disable_and_submit(form_id, form, "false", locale);
report_analytics_event("message", "sent");
window.ST.analytics.logEvent("message", "sent");
}
});
}
@@ -289,7 +270,7 @@ function initialize_send_person_message_form(locale) {
},
submitHandler: function(form) {
disable_and_submit(form_id, form, "false", locale);
report_analytics_event("message", "sent");
window.ST.analytics.logEvent("message", "sent");
}
});
}
@@ -314,7 +295,7 @@ function initialize_listing_view(locale) {
);
$('#send_comment_button').click(function() {
report_analytics_event("listing", "commented");
window.ST.analytics.logEvent("listing", "commented");
});
}
@@ -415,7 +396,7 @@ function initialize_signup_form(locale, username_in_use_message, invalid_usernam
onkeyup: false, //Only do validations when form focus changes to avoid exessive ASI calls
submitHandler: function(form) {
disable_and_submit(form_id, form, "false", locale);
report_analytics_event('user', "signed up", "normal form");
window.ST.analytics.logEvent('user', "signed up", "normal form");
}
});
}
@@ -1,7 +1,6 @@
window.ST = window.ST || {};
(function(module) {
/* global report_analytics_event */
/* global disable_submit_button */
/* global set_textarea_maxlength */
/* global auto_resize_text_areas */
@@ -528,7 +527,7 @@ window.ST = window.ST || {};
// This handler is used when Image uploader is not loading
validFormSubmitted.filter(isLoading.not()).onValue(function(e) {
report_analytics_event("listing", "created");
window.ST.analytics.logEvent("listing", "created");
disable_submit_button(form_id, locale);
});
@@ -89,7 +89,7 @@ window.ST.transaction = window.ST.transaction || {};
.flatMapLatest(function() {
var timeout = Bacon.later(3000, "timeout");
var response = Bacon.fromCallback(function(callback) {
ampClient.logEvent(analyticsEvent[0], analyticsEvent[1], callback);
window.ST.analytics.logEvent(analyticsEvent[0], null, null, analyticsEvent[1]);
});
return timeout.merge(response).take(1);
@@ -66,7 +66,7 @@ def accepted_or_rejected
if res[:success]
flash[:notice] = success_msg(res[:flow])
Analytics.record_event(
record_event(
flash,
status == :paid ? "PreauthorizedTransactionAccepted" : "PreauthorizedTransactionRejected",
{ listing_id: tx[:listing_id],
@@ -261,6 +261,7 @@ def settings
def update_look_and_feel
@community = @current_community
@selected_left_navi_link = "tribe_look_and_feel"
analytic = AnalyticService::CommunityLookAndFeel.new(user: @current_user, community: @current_community)
params[:community][:custom_color1] = nil if params[:community][:custom_color1] == ""
params[:community][:custom_color2] = nil if params[:community][:custom_color2] == ""
@@ -273,18 +274,19 @@ def update_look_and_feel
]
permitted_params << :custom_head_script
community_params = params.require(:community).permit(*permitted_params)
analytic.process(@current_community, community_params)
update(@current_community,
community_params,
admin_look_and_feel_edit_path,
:edit_look_and_feel) { |community|
flash[:notice] = t("layouts.notifications.images_are_processing") if images_changed?(params)
analytic.send_properties
# Onboarding wizard step recording
state_changed = Admin::OnboardingWizard.new(community.id)
.update_from_event(:community_updated, community)
if state_changed
report_to_gtm({event: "km_record", km_event: "Onboarding cover photo uploaded"})
record_event(flash, "km_record", {km_event: "Onboarding cover photo uploaded"})
flash[:show_onboarding_popup] = true
end
}
@@ -20,6 +20,7 @@ def edit_details
def update_details
update_results = []
analytic = AnalyticService::CommunityCustomizations.new(user: @current_user, community: @current_community)
customizations = @current_community.locales.map do |locale|
permitted_params = [
@@ -32,7 +33,9 @@ def update_details
]
locale_params = params.require(:community_customizations).require(locale).permit(*permitted_params)
customizations = find_or_initialize_customizations_for_locale(locale)
update_results.push(customizations.update_attributes(locale_params))
customizations.assign_attributes(locale_params)
analytic.process(customizations)
update_results.push(customizations.update_attributes({}))
customizations
end
@@ -50,13 +53,14 @@ def update_details
transaction_agreement_checked = Maybe(params)[:community][:transaction_agreement_checkbox].is_some?
update_results.push(@current_community.update_attributes(transaction_agreement_in_use: transaction_agreement_checked))
analytic.send_properties
if update_results.all? && (!process_locales || enabled_locales_valid)
# Onboarding wizard step recording
state_changed = Admin::OnboardingWizard.new(@current_community.id)
.update_from_event(:community_customizations_updated, customizations)
if state_changed
report_to_gtm({event: "km_record", km_event: "Onboarding slogan/description created"})
record_event(flash, "km_record", {km_event: "Onboarding slogan/description created"})
flash[:show_onboarding_popup] = true
end
@@ -103,5 +107,4 @@ def unofficial_locales
def has_preauthorize_process?(processes)
processes.any? { |p| p[:process] == :preauthorize }
end
end
@@ -124,8 +124,7 @@ def create
state_changed = Admin::OnboardingWizard.new(@current_community.id)
.update_from_event(:custom_field_created, @custom_field)
if state_changed
report_to_gtm({event: "km_record", km_event: "Onboarding filter created"})
record_event(flash, "km_record", {km_event: "Onboarding filter created"})
flash[:show_onboarding_popup] = true
end
@@ -91,8 +91,8 @@ def update
state_changed = Admin::OnboardingWizard.new(@current_community.id)
.update_from_event(:listing_shape_updated, [update_result.data])
if state_changed
report_to_gtm([{event: "km_record", km_event: "Onboarding payments setup"},
{event: "km_record", km_event: "Onboarding payment disabled"}])
record_event(flash, "km_record", {km_event: "Onboarding payments setup"})
record_event(flash, "km_record", {km_event: "Onboarding payment disabled"})
flash[:show_onboarding_popup] = true
end
@@ -226,8 +226,8 @@ def update_payment_preferences
state_changed = Admin::OnboardingWizard.new(@current_community.id)
.update_from_event(:payment_preferences_updated, @current_community)
if state_changed
report_to_gtm([{event: "km_record", km_event: "Onboarding payments setup"},
{event: "km_record", km_event: "Onboarding paypal connected"}])
record_event(flash, "km_record", {km_event: "Onboarding payments setup"})
record_event(flash, "km_record", {km_event: "Onboarding paypal connected"})
flash[:show_onboarding_popup] = true
end
Oops, something went wrong.

0 comments on commit ec207ee

Please sign in to comment.