workarea-segment_analytics 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/.eslintrc +35 -0
  3. data/.gitignore +19 -0
  4. data/.rubocop.yml +8 -0
  5. data/CHANGELOG.md +47 -0
  6. data/Gemfile +16 -0
  7. data/README.md +91 -0
  8. data/Rakefile +60 -0
  9. data/app/assets/javascripts/workarea/storefront/segment_analytics/dependencies/analytics.js.erb +3 -0
  10. data/app/assets/javascripts/workarea/storefront/segment_analytics/modules/login_events.js +40 -0
  11. data/app/assets/javascripts/workarea/storefront/segment_analytics/modules/segment_analytics.js +47 -0
  12. data/app/assets/javascripts/workarea/storefront/segment_analytics/modules/segment_analytics_adapter.js +235 -0
  13. data/app/assets/stylesheets/segment_analytics/.keep +0 -0
  14. data/app/helpers/.keep +0 -0
  15. data/app/helpers/workarea/storefront/segment_analytics_analytics_helper.rb +40 -0
  16. data/app/views/.keep +0 -0
  17. data/app/views/workarea/storefront/users/_current_user_segment_analytics.json.jbuilder +6 -0
  18. data/bin/rails +20 -0
  19. data/config/initializers/appends.rb +18 -0
  20. data/config/initializers/workarea.rb +1 -0
  21. data/config/routes.rb +2 -0
  22. data/lib/tasks/segment_analytics_tasks.rake +4 -0
  23. data/lib/workarea/segment_analytics.rb +23 -0
  24. data/lib/workarea/segment_analytics/engine.rb +14 -0
  25. data/lib/workarea/segment_analytics/version.rb +5 -0
  26. data/script/admin_ci +9 -0
  27. data/script/ci +11 -0
  28. data/script/core_ci +9 -0
  29. data/script/plugins_ci +9 -0
  30. data/script/storefront_ci +9 -0
  31. data/test/dummy/Rakefile +6 -0
  32. data/test/dummy/app/assets/config/manifest.js +4 -0
  33. data/test/dummy/app/assets/images/.keep +0 -0
  34. data/test/dummy/app/assets/javascripts/application.js +13 -0
  35. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  36. data/test/dummy/app/controllers/application_controller.rb +3 -0
  37. data/test/dummy/app/controllers/concerns/.keep +0 -0
  38. data/test/dummy/app/helpers/application_helper.rb +2 -0
  39. data/test/dummy/app/jobs/application_job.rb +2 -0
  40. data/test/dummy/app/mailers/application_mailer.rb +4 -0
  41. data/test/dummy/app/models/concerns/.keep +0 -0
  42. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  43. data/test/dummy/app/views/layouts/mailer.html.erb +13 -0
  44. data/test/dummy/app/views/layouts/mailer.text.erb +1 -0
  45. data/test/dummy/bin/bundle +3 -0
  46. data/test/dummy/bin/rails +4 -0
  47. data/test/dummy/bin/rake +4 -0
  48. data/test/dummy/bin/setup +38 -0
  49. data/test/dummy/bin/update +29 -0
  50. data/test/dummy/bin/yarn +11 -0
  51. data/test/dummy/config.ru +5 -0
  52. data/test/dummy/config/application.rb +29 -0
  53. data/test/dummy/config/boot.rb +5 -0
  54. data/test/dummy/config/cable.yml +10 -0
  55. data/test/dummy/config/environment.rb +5 -0
  56. data/test/dummy/config/environments/development.rb +54 -0
  57. data/test/dummy/config/environments/production.rb +91 -0
  58. data/test/dummy/config/environments/test.rb +44 -0
  59. data/test/dummy/config/initializers/application_controller_renderer.rb +8 -0
  60. data/test/dummy/config/initializers/assets.rb +14 -0
  61. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  62. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  63. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  64. data/test/dummy/config/initializers/inflections.rb +16 -0
  65. data/test/dummy/config/initializers/mime_types.rb +4 -0
  66. data/test/dummy/config/initializers/workarea.rb +5 -0
  67. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  68. data/test/dummy/config/locales/en.yml +33 -0
  69. data/test/dummy/config/puma.rb +56 -0
  70. data/test/dummy/config/routes.rb +5 -0
  71. data/test/dummy/config/secrets.yml +32 -0
  72. data/test/dummy/config/spring.rb +6 -0
  73. data/test/dummy/db/seeds.rb +2 -0
  74. data/test/dummy/lib/assets/.keep +0 -0
  75. data/test/dummy/log/.keep +0 -0
  76. data/test/dummy/package.json +5 -0
  77. data/test/javascripts/fixtures/payloads.js +232 -0
  78. data/test/javascripts/fixtures/stubs.js +56 -0
  79. data/test/javascripts/segment_analytics_spec.js +311 -0
  80. data/test/javascripts/spec_helper.js +12 -0
  81. data/test/teaspoon_env.rb +6 -0
  82. data/test/test_helper.rb +10 -0
  83. data/workarea-segment_analytics.gemspec +19 -0
  84. metadata +138 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b63311948bf8b091926cca0496c0502f4bd7093a94f11667a251183711b993a4
4
+ data.tar.gz: f7739b2422619785b8934267bf3c39ae53a146a3b481484c2c95900fd19c7215
5
+ SHA512:
6
+ metadata.gz: 8f03ef7cbae1463b91c7e2d545530310d4e551362f8da36584fdc05bf015b73eed4c3328b8033e4752d85ccc61e0714cd9ce9ff3a1e3e2670085171146179713
7
+ data.tar.gz: 3ff6a1baaac585224810578e11d5a38f5a01924dc579513af50576163f608aeda66ac8f0603d8ac1232ef79685310361d274796d8faf1ff65b0e72f8bb3152b5
data/.eslintrc ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "extends": "eslint:recommended",
3
+ "rules": {
4
+ "semi": ["error", "always"],
5
+ "eqeqeq": ["error", "always"]
6
+ },
7
+ "globals": {
8
+ "window": true,
9
+ "document": true,
10
+ "WORKAREA": true,
11
+ "$": true,
12
+ "jQuery": true,
13
+ "_": true,
14
+ "feature": true,
15
+ "JST": true,
16
+ "Turbolinks": true,
17
+ "I18n": true,
18
+ "Chart": true,
19
+ "Dropzone": true,
20
+ "strftime": true,
21
+ "Waypoint": true,
22
+ "wysihtml": true,
23
+ "LocalTime": true,
24
+ "describe": true,
25
+ "after": true,
26
+ "afterEach": true,
27
+ "before": true,
28
+ "beforeEach": true,
29
+ "it": true,
30
+ "expect": true,
31
+ "sinon": true,
32
+ "fixture": true,
33
+ "chai": true
34
+ }
35
+ }
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ test/dummy/db/*.sqlite3
5
+ test/dummy/db/*.sqlite3-journal
6
+ test/dummy/log/*.log
7
+ test/dummy/tmp/
8
+ .DS_Store
9
+ .byebug_history
10
+ .bundle/
11
+ .sass-cache/
12
+ Gemfile.lock
13
+ pkg/
14
+ test/dummy/tmp/
15
+ test/dummy/public/
16
+ log/*.log
17
+ test/dummy/log/*.log
18
+ test/dummy/db/*.sqlite3
19
+ test/dummy/db/*.sqlite3-journal
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ inherit_gem:
2
+ workarea-ci: rubocop.yml
3
+
4
+ AllCops:
5
+ Exclude:
6
+ - 'test/dummy/**/*'
7
+ - 'lib/workarea/segment_analytics.rb'
8
+ - 'lib/workarea/segment_analytics/**/*'
data/CHANGELOG.md ADDED
@@ -0,0 +1,47 @@
1
+ Workarea Segment Analytics 1.0.2 (2019-08-21)
2
+ --------------------------------------------------------------------------------
3
+
4
+ * Open Source!
5
+
6
+
7
+
8
+ Workarea Segment Analytics 1.0.1 (2019-03-19)
9
+ --------------------------------------------------------------------------------
10
+
11
+ * Update for v3.4 compatibility
12
+
13
+ * Add CI build scripts
14
+ * Update gemfile
15
+ * Add default rubocop.yml configuration
16
+ * Update ESLint configuration and fix some linting errors
17
+ * Add keepfile for test/dummy/tmp/screenshots
18
+
19
+ SEGMENT-6
20
+ Jake Beresford
21
+
22
+
23
+
24
+ Workarea Segment Analytics 1.0.0 (2018-01-11)
25
+ --------------------------------------------------------------------------------
26
+
27
+ To clean up your release notes, add your project's Jira key to the Changelog Rake task!
28
+ * Bump version for v1.0.0 release
29
+ Jake Beresford
30
+
31
+ To clean up your release notes, add your project's Jira key to the Changelog Rake task!
32
+ * Fetch teaspoon stubs after renaming plugin
33
+
34
+ SEGMENT-5
35
+ Jake Beresford
36
+
37
+ To clean up your release notes, add your project's Jira key to the Changelog Rake task!
38
+ * Rename plugin to workarea-segment_analytics
39
+
40
+ This plugin was renamed due to a name conflict with the Workarea::Segment class in workarea-segmentation.
41
+
42
+ SEGMENT-4
43
+ Jake Beresford
44
+
45
+
46
+ Workarea Segment 0.1.0 (2017-10-13)
47
+ --------------------------------------------------------------------------------
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Declare your gem's dependencies in segment_analytics.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use a debugger
14
+ # gem 'byebug', group: [:development, :test]
15
+
16
+ gem 'workarea', source: 'https://gems.weblinc.com'
data/README.md ADDED
@@ -0,0 +1,91 @@
1
+ Workarea Segment Analytics
2
+ ================================================================================
3
+
4
+ Segment plugin for the Workarea platform.
5
+
6
+ Integration with segment https://segment.com/
7
+ API docs: https://segment.com/docs/sources/
8
+
9
+ This plugin implements segment's client side tracking API via their 'analytics.js' script.
10
+ Before installing and configuring this plugin you should set up & configure your segment account with a Website / Javascript source.
11
+
12
+ Configuration
13
+ --------------------------------------------------------------------------------
14
+
15
+ To configure segment in your application you must add the segment write key to your application secrets.
16
+ You can find the write key by logging in to the segment admin and visiting this URL https://segment.com/weblinc/sources/javascript/settings/keys
17
+
18
+ segment_analytics:
19
+ write_key: alongstringgoeshere
20
+
21
+ You will also need to update your application views to include an additional analytics payload for checkout started. To do this add the checkoutStarted event & payload to any links to checkout_path, for example in your cart/show.html.haml view.
22
+
23
+ snippet to add:
24
+ data: { analytics: checkout_started_analytics_data(@cart).to_json
25
+
26
+ #### Compatability with other plugins
27
+
28
+ If you are using the workarea-share plugin you should also add the following to your share links in workarea/storefront/shares/_share_buttons.html.haml
29
+
30
+ data: { analytics: share_analytics_data('*share service name goes here*', share_url, message).to_json }
31
+
32
+ example:
33
+
34
+ data: { analytics: share_analytics_data('facebook', share_url, message).to_json }
35
+
36
+ Testing and debugging
37
+ --------------------------------------------------------------------------------
38
+ To confirm this plugin is functioning you should check the JS console in development mode, the
39
+ adapter for this plugin includes a logger which should show multiple console logs in your browser's JS console.
40
+ All segment logger events begin with 'Segment:'
41
+
42
+ Once you have verified there are events firing in the console you should confirm that the data received by segment is error-free.
43
+ Segment provide an excellent debugger via their admin console, log in and visit this URL: https://segment.com/weblinc/sources/javascript/debugger
44
+
45
+
46
+ Getting Started
47
+ --------------------------------------------------------------------------------
48
+
49
+ This gem contains a rails engine that must be mounted onto a host Rails application.
50
+
51
+ To access Workarea gems and source code, you must be an employee of WebLinc or a licensed retailer or partner.
52
+
53
+ Workarea gems are hosted privately at https://gems.weblinc.com/.
54
+ You must have individual or team credentials to install gems from this server. Add your gems server credentials to Bundler:
55
+
56
+ bundle config gems.weblinc.com my_username:my_password
57
+
58
+ Or set the appropriate environment variable in a shell startup file:
59
+
60
+ export BUNDLE_GEMS__WEBLINC__COM='my_username:my_password'
61
+
62
+ Then add the gem to your application's Gemfile specifying the source:
63
+
64
+ # ...
65
+ gem 'workarea-segment', source: 'https://gems.weblinc.com'
66
+ # ...
67
+
68
+ Or use a source block:
69
+
70
+ # ...
71
+ source 'https://gems.weblinc.com' do
72
+ gem 'workarea-segment'
73
+ end
74
+ # ...
75
+
76
+ Update your application's bundle.
77
+
78
+ cd path/to/application
79
+ bundle
80
+
81
+ Workarea Platform Documentation
82
+ --------------------------------------------------------------------------------
83
+
84
+ See [http://developer.weblinc.com](http://developer.weblinc.com) for Workarea platform documentation.
85
+
86
+ Copyright & Licensing
87
+ --------------------------------------------------------------------------------
88
+
89
+ Copyright WebLinc 2017. All rights reserved.
90
+
91
+ For licensing, contact sales@workarea.com.
data/Rakefile ADDED
@@ -0,0 +1,60 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+ RDoc::Task.new(:rdoc) do |rdoc|
9
+ rdoc.rdoc_dir = 'rdoc'
10
+ rdoc.title = 'Segment Analytics'
11
+ rdoc.options << '--line-numbers'
12
+ rdoc.rdoc_files.include('README.md')
13
+ rdoc.rdoc_files.include('lib/**/*.rb')
14
+ end
15
+
16
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
17
+ load 'rails/tasks/engine.rake'
18
+ load 'rails/tasks/statistics.rake'
19
+ load 'workarea/changelog.rake'
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |t|
23
+ t.libs << 'lib'
24
+ t.libs << 'test'
25
+ t.pattern = 'test/**/*_test.rb'
26
+ t.verbose = false
27
+ end
28
+ task default: :test
29
+
30
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
31
+ require 'workarea/segment_analytics/version'
32
+
33
+ desc "Release version #{Workarea::SegmentAnalytics::VERSION} of the gem"
34
+ task :release do
35
+ host = "https://#{ENV['BUNDLE_GEMS__WEBLINC__COM']}@gems.weblinc.com"
36
+
37
+ #Rake::Task['workarea:changelog'].execute
38
+ #system 'git add CHANGELOG.md'
39
+ #system 'git commit -m "Update CHANGELOG"'
40
+ #system 'git push origin HEAD'
41
+
42
+ system "git tag -a v#{Workarea::SegmentAnalytics::VERSION} -m 'Tagging #{Workarea::SegmentAnalytics::VERSION}'"
43
+ system 'git push --tags'
44
+
45
+ system "gem build workarea-segment_analytics.gemspec"
46
+ system "gem push workarea-segment_analytics-#{Workarea::SegmentAnalytics::VERSION}.gem"
47
+ system "gem push workarea-segment_analytics-#{Workarea::SegmentAnalytics::VERSION}.gem --host #{host}"
48
+ system "rm workarea-segment_analytics-#{Workarea::SegmentAnalytics::VERSION}.gem"
49
+ end
50
+
51
+ desc 'Run the JavaScript tests'
52
+ ENV['TEASPOON_RAILS_ENV'] = File.expand_path('../test/dummy/config/environment', __FILE__)
53
+ task teaspoon: 'app:teaspoon'
54
+
55
+ desc 'Start a server at http://localhost:3000/teaspoon for JavaScript tests'
56
+ task :teaspoon_server do
57
+ Dir.chdir("test/dummy")
58
+ teaspoon_env = File.expand_path('../test/teaspoon_env.rb', __FILE__)
59
+ system "RAILS_ENV=test TEASPOON_ENV=#{teaspoon_env} rails s"
60
+ end
@@ -0,0 +1,3 @@
1
+ !function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("SegmentAnalytics snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment_analytics.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="4.0.0";
2
+ analytics.load('<%= Workarea::SegmentAnalytics.write_key %>');
3
+ }}();
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @namespace WORKAREA.loginEvents
3
+ */
4
+ WORKAREA.registerModule('loginEvents', (function () {
5
+ 'use strict';
6
+
7
+ var userStateIsSet = function () {
8
+ return !_.isEmpty(window.sessionStorage.getItem('segmentAnalyticsLoginState'));
9
+ },
10
+
11
+ setUserState = function (userData) {
12
+ window.sessionStorage.setItem('segmentAnalyticsLoginState', userData.logged_in);
13
+ },
14
+
15
+ compare = function (userData) {
16
+ if (userData.logged_in.toString() !== window.sessionStorage.getItem('segmentAnalyticsLoginState')) {
17
+ if ( userData.logged_in ) {
18
+ WORKAREA.analytics.fireCallback('justLoggedIn');
19
+ } else {
20
+ WORKAREA.analytics.fireCallback('justLoggedOut');
21
+ }
22
+
23
+ setUserState(userData);
24
+ }
25
+ },
26
+
27
+ setupCallbacks = function () {
28
+ if ( userStateIsSet() ) {
29
+ WORKAREA.currentUser.gettingUserData.done(compare);
30
+ } else {
31
+ WORKAREA.currentUser.gettingUserData.done(setUserState);
32
+ }
33
+ },
34
+
35
+ init = _.once(setupCallbacks);
36
+
37
+ return {
38
+ init: init
39
+ };
40
+ }()));
@@ -0,0 +1,47 @@
1
+ /**
2
+ * @namespace WORKAREA.segmentAnalytics
3
+ */
4
+ WORKAREA.registerModule('segmentAnalytics', (function () {
5
+ 'use strict';
6
+ /*global analytics*/
7
+
8
+ var logger = function () {
9
+ if (WORKAREA.analytics.debug && window.console) {
10
+ window.console.log('SegmentAnalytics: ', Array.prototype.slice.call(arguments));
11
+ }
12
+ },
13
+
14
+ track = function (eventType, data) {
15
+ analytics.track(eventType, data);
16
+ logger('Track', eventType, data);
17
+ },
18
+
19
+ reset = function () {
20
+ analytics.reset();
21
+ logger('Reset');
22
+ },
23
+
24
+ identify = function (userId, data) {
25
+ analytics.identify(userId, data);
26
+ logger('Identify', userId, data);
27
+ },
28
+
29
+ page = function (payload) {
30
+ if (_.isUndefined(payload)) {
31
+ analytics.page();
32
+ } else if (payload.category) {
33
+ analytics.page(payload.category, payload.name);
34
+ } else {
35
+ analytics.page(payload.name);
36
+ }
37
+
38
+ logger('Page', payload);
39
+ };
40
+
41
+ return {
42
+ track: track,
43
+ page: page,
44
+ reset: reset,
45
+ identify: identify
46
+ };
47
+ }()));
@@ -0,0 +1,235 @@
1
+ /**
2
+ * @namespace WORKAREA.segmentAnalytics
3
+ * @requires WORKAREA.currentUser
4
+ *
5
+ * 1. userData.checkout_id and order.id are the same number from
6
+ * different sources. interchangable with cart_id in workarea
7
+ *
8
+ * 2. analytics_helper refers to these differently depending on whether
9
+ * order_item_analytics_data or product_analytics_data is used.
10
+ */
11
+ WORKAREA.analytics.registerAdapter('segmentAnalyticsAdapter', function () {
12
+ 'use strict';
13
+
14
+ var pageViewSent = false,
15
+
16
+ /**
17
+ * PageView event functions
18
+ */
19
+
20
+ isLoaded = function () {
21
+ var scripts = _.toArray(document.scripts),
22
+
23
+ loaded = _.some(scripts, function (script) {
24
+ return script.src.match(/segment_analytics\.com.+analytics.+\.js/i);
25
+ });
26
+
27
+ return loaded;
28
+ },
29
+
30
+ tryPageView = function (payload) {
31
+ if (pageViewSent) { return; }
32
+
33
+ if (isLoaded()) {
34
+ WORKAREA.segmentAnalytics.page(payload);
35
+ pageViewSent = true;
36
+ } else {
37
+ window.setTimeout(tryPageView(payload), 500);
38
+ }
39
+ },
40
+
41
+ /**
42
+ * Data manipulation functions
43
+ */
44
+
45
+ productData = function(product) {
46
+ var data = {
47
+ category: product.category,
48
+ name: product.name || product.product_name, /* [2] */
49
+ position: product.position,
50
+ price: product.price,
51
+ product_id: product.product_id || product.id, /* [2] */
52
+ quantity: product.quantity,
53
+ variant: product.sku
54
+ };
55
+
56
+ return _.omit(data, _.isEmpty);
57
+ },
58
+
59
+ joinPromoCodes = function(promo_codes) {
60
+ if (Array.isArray(promo_codes)){
61
+ return promo_codes.join(',');
62
+ }
63
+ return promo_codes;
64
+ },
65
+
66
+ orderData = function (order) {
67
+ var data = {
68
+ affiliation: order.site_name,
69
+ checkout_id: order.id, /* [1] */
70
+ discount: joinPromoCodes(order.promo_codes),
71
+ payment_method: order.tenders,
72
+ revenue: order.total_price,
73
+ shipping: order.shipping_total,
74
+ tax: order.tax_total
75
+ };
76
+
77
+
78
+ if ( !_.isEmpty(order.items) ) { /* [2] */
79
+ data.products = _.map(order.items, productData);
80
+ } else if ( !_.isEmpty(order.products) ) { /* [2] */
81
+ data.products = _.map(order.products, productData);
82
+ }
83
+
84
+ return _.omit(data, _.isEmpty);
85
+ };
86
+
87
+ return {
88
+ 'pageView': tryPageView,
89
+
90
+ 'categoryView': function (payload) {
91
+ // Send pageview with additional payload data
92
+ tryPageView({
93
+ category: payload.name
94
+ });
95
+
96
+ WORKAREA.segmentAnalytics.track('Product Category Viewed', {
97
+ filters: payload.filters,
98
+ name: payload.name,
99
+ page: payload.page,
100
+ sort: payload.sort
101
+ });
102
+ },
103
+
104
+ 'searchResultsView': function (payload) {
105
+ WORKAREA.segmentAnalytics.track('Products Searched', {
106
+ query: payload.terms
107
+ });
108
+ },
109
+
110
+ 'productList': function (payload) {
111
+ var data = {
112
+ list_id: payload.name,
113
+ products: _.map(payload.impressions, productData)
114
+ };
115
+
116
+ WORKAREA.segmentAnalytics.track('Product List Viewed', data);
117
+ },
118
+
119
+ 'productClick': function (payload) {
120
+ WORKAREA.segmentAnalytics.track('Product Clicked', productData(payload));
121
+ },
122
+
123
+ 'productView': function (payload) {
124
+ // Send pageview with additional payload data
125
+ tryPageView({
126
+ category: payload.category
127
+ });
128
+
129
+ WORKAREA.segmentAnalytics.track('Product Viewed', productData(payload));
130
+ },
131
+
132
+ 'addToCart': function (payload) {
133
+ WORKAREA.currentUser.gettingUserData.done(function(userData){
134
+ var data = {
135
+ cart_id: userData.checkout_id /* [1] */
136
+ };
137
+
138
+ _.assign(data, productData(payload));
139
+
140
+ WORKAREA.segmentAnalytics.track('Product Added', data);
141
+ });
142
+ },
143
+
144
+ 'removeFromCart': function (payload) {
145
+ WORKAREA.currentUser.gettingUserData.done(function(userData){
146
+ var data = {
147
+ cart_id: userData.checkout_id /* [1] */
148
+ };
149
+
150
+ _.assign(data, productData(payload));
151
+
152
+ WORKAREA.segmentAnalytics.track('Product Removed', data);
153
+ });
154
+ },
155
+
156
+ 'cartView': function (payload) {
157
+ WORKAREA.currentUser.gettingUserData.done(function(userData){
158
+ var data = {
159
+ cart_id: userData.checkout_id, /* [1] */
160
+ products: _.map(payload.items, productData)
161
+ };
162
+
163
+ WORKAREA.segmentAnalytics.track('Cart Viewed', data);
164
+ WORKAREA.segmentAnalytics.track('Viewed Checkout Step', _.assign({ step: 1 }, data));
165
+ });
166
+ },
167
+
168
+ 'checkoutStarted': function (payload) {
169
+ var data = orderData(payload);
170
+
171
+ WORKAREA.segmentAnalytics.track('Checkout Started', data);
172
+ },
173
+
174
+ 'checkoutAddressesView': function (payload) {
175
+ var data = orderData(payload);
176
+
177
+ WORKAREA.segmentAnalytics.track('Checkout Step Completed', _.assign({ step: 1 }, data));
178
+ WORKAREA.segmentAnalytics.track('Checkout Step Viewed', _.assign({ step: 2 }, data));
179
+ },
180
+
181
+ 'checkoutShippingView': function (payload) {
182
+ var data = orderData(payload);
183
+
184
+ WORKAREA.segmentAnalytics.track('Checkout Step Completed', _.assign({ step: 2 }, data));
185
+ WORKAREA.segmentAnalytics.track('Checkout Step Viewed', _.assign({ step: 3 }, data));
186
+ },
187
+
188
+ 'checkoutPaymentView': function (payload) {
189
+ var data = orderData(payload);
190
+
191
+ WORKAREA.segmentAnalytics.track('Checkout Step Completed', _.assign({ step: 3 }, data));
192
+ WORKAREA.segmentAnalytics.track('Checkout Step Viewed', _.assign({ step: 4 }, data));
193
+ },
194
+
195
+ 'checkoutPaymentSelected': function (payload) {
196
+ var data = orderData(payload);
197
+
198
+ WORKAREA.segmentAnalytics.track('Payment Info Entered', _.assign({ step: 4 }, data));
199
+ },
200
+
201
+ 'checkoutOrderPlaced': function (payload) {
202
+ var data = orderData(payload);
203
+
204
+ WORKAREA.segmentAnalytics.track('Checkout Step Completed', _.assign({ step: 4 }, data));
205
+ WORKAREA.segmentAnalytics.track('Order Completed', data);
206
+ },
207
+
208
+ 'justLoggedOut': function () {
209
+ WORKAREA.segmentAnalytics.reset();
210
+ },
211
+
212
+ 'justLoggedIn': function () {
213
+ WORKAREA.currentUser.gettingUserData.done(function (userData){
214
+ var data = {
215
+ email: userData.user_email,
216
+ name: userData.user_name
217
+ };
218
+
219
+ WORKAREA.segmentAnalytics.identify(userData.user_id, data);
220
+ });
221
+ },
222
+
223
+ 'share': function (payload) {
224
+ var data = {
225
+ recipient: payload.recipient,
226
+ share_via: payload.type,
227
+ share_message: payload.message
228
+ };
229
+
230
+ _.assign(data, productData(payload));
231
+
232
+ WORKAREA.segmentAnalytics.track('Product Shared', data);
233
+ }
234
+ };
235
+ });