meta_events 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,154 @@
1
+ var MetaEvents = window.MetaEvents || {};
2
+ /**
3
+ * Defines the MetaEvents JavaScript module. This provides client-side functions that support MetaEvents'
4
+ * auto-tracking and frontend events systems; see README.md for more information.
5
+ */
6
+ MetaEvents = (function() {
7
+ var out = {}; // The public object that we'll return.
8
+ var createDomIdUniqueId = 0; // Helps us ensure we generate truly unique DOM IDs.
9
+ // See MetaEvents::Helpers.meta_events_javascript_tracking_prefix for more details; just copies that string.
10
+ var trackingPrefix = <%= MetaEvents::Helpers.meta_events_javascript_tracking_prefix.to_json %>;
11
+
12
+ /**
13
+ * Given a DOM element, ensures that it has a DOM ID on it. Returns its unique DOM ID -- either the one it already
14
+ * had, or the newly-generated synthetic ID.
15
+ *
16
+ * @param element - The DOM element that you want to make sure has an ID.
17
+ */
18
+ var getOrCreateDomId = function(element) {
19
+ var theId = element.id;
20
+
21
+ if (theId == null || theId == "") {
22
+ theId = "metaEventsSyntheticDomId" + createDomIdUniqueId++;
23
+ element.id = theId;
24
+ }
25
+
26
+ return theId;
27
+ };
28
+
29
+ /**
30
+ * Given a DOM element that should be used with auto-tracking, calls a specified function, passing in the unique
31
+ * DOM ID of the element (which will be generated if necessary), the element itself, the name of the event that
32
+ * should be fired, and the set of properties that should be fired with that event.
33
+ *
34
+ * After the specified function is called and returns successfully, the element is marked (via a jQuery data
35
+ * attribute) so that it will never again have that function called.
36
+ *
37
+ * @param element - The element that we should use
38
+ * @param applyFunction - The function that we'll call on that element, unless it has already been called
39
+ */
40
+ var registerTrackableElement = function(element, applyFunction) {
41
+ var registeredPropertyName = trackingPrefix + "_reg";
42
+ var registered = $(element).data(registeredPropertyName);
43
+
44
+ if (registered != null) {
45
+ return;
46
+ }
47
+
48
+ var theId = getOrCreateDomId(element);
49
+ var eventName = $(element).data(trackingPrefix + "_evt");
50
+ var eventProperties = $(element).data(trackingPrefix + "_prp");
51
+
52
+ if (! (eventName == null || eventName == "" || eventProperties == null)) {
53
+ applyFunction(theId, element, eventName, eventProperties);
54
+ <% if Rails.env.development? %>
55
+ } else {
56
+ $(element).data(registeredPropertyName, true);
57
+ throw "No Mixpanel event name or event properties on element that has class '." + trackingPrefix + "_trk', which tags it for tracking. Element: " + element;
58
+ <% end %>
59
+ }
60
+
61
+ $(element).data(registeredPropertyName, true);
62
+ };
63
+
64
+ /**
65
+ * Given a DOM element to start with and a function, finds all elements underneath that DOM element that have been
66
+ * enabled for auto-tracking (using #meta_events_tracking_attributes_for or #meta_events_tracked_link_to), and
67
+ * calls the given function on each one, in turn.
68
+ *
69
+ * The function will never be called twice for the same element (since the element is marked as having been passed to
70
+ * the function once it's done); this means that it's safe to call as often as you want -- for example, when the
71
+ * DOM changes.
72
+ *
73
+ * The function passed should accept the following parameters, in this order:
74
+ * id - the DOM ID of the element; one will be generated for it and assigned to it if not already present
75
+ * element - the element itself
76
+ * eventName - the name of the event that this DOM element should fire
77
+ * eventProperties - an object containing the properties that should be included with that event
78
+ *
79
+ * @param startingElement - The element under which we should search
80
+ * @param applyFunction - The function we should call on each element
81
+ */
82
+ out.forAllTrackableElements = function(startingElement, applyFunction) {
83
+ $(startingElement).find("." + trackingPrefix + "_trk").each(function(index, element) {
84
+ registerTrackableElement(element, applyFunction);
85
+ });
86
+ };
87
+
88
+ var events = { };
89
+
90
+ /**
91
+ * Called to register a frontend event that was defined server-side.
92
+ *
93
+ * @param name - The name that we'll use to access this event, using 'event', below; this is different from the name
94
+ * of the actual event that will be fired and need not have any correlation;
95
+ * @param eventData - The data for the event; this must be an object containing 'event_name', which is the name of
96
+ * the event to fire, and 'properties', the set of properties to fire with that event.
97
+ */
98
+ out.registerFrontendEvent = function(name, eventData) {
99
+ events[name] = eventData;
100
+ };
101
+
102
+ /**
103
+ * Fires a frontend event. Accepts the name (alias) that we use to access the event, and zero or more objects; all
104
+ * properties of those objects are merged together (with later objects overriding earlier ones) and then merged into
105
+ * the properties the server told us to fire with the event (again, with our properties overriding it), and then the
106
+ * event is fired by calling the current handler function set by 'setEventHandler', below (which, by default, calls
107
+ * Mixpanel).
108
+ *
109
+ * @param eventAlias - The alias for the event to fire, which must match a call to registerFrontendEvent(), above;
110
+ * this is typically created by the +meta_events_frontend_events_javascript+ method in
111
+ * MetaEvents::Helpers that's called Rails-side to generate appropriate JavaScript code in the
112
+ * page somewhere.
113
+ */
114
+ out.event = function(eventAlias) {
115
+ var eventData = events[eventAlias];
116
+
117
+ if (eventData == null) {
118
+ <% if Rails.env.development? %>
119
+ throw "No front-end event has been registered named '" + name + "'. Did you forget to call meta_events_define_frontend_event on the server?";
120
+ <% end %>
121
+ return;
122
+ }
123
+
124
+ var effectiveProperties = { };
125
+ $.extend(effectiveProperties, eventData.properties);
126
+
127
+ for (var i = 1; i < arguments.length; ++i) {
128
+ $.extend(effectiveProperties, arguments[i]);
129
+ }
130
+
131
+ this.eventHandler(eventData.event_name, effectiveProperties);
132
+ };
133
+
134
+ /**
135
+ * The default event handler that we use if you haven't called setEventHandler(), below.
136
+ */
137
+ out.mixpanelEventHandler = function(eventName, properties) {
138
+ mixpanel.track(eventName, properties);
139
+ };
140
+
141
+ /**
142
+ * When a frontend event is fired, there's a function, at the bottom, that actually fires the event; it gets passed
143
+ * two parameters -- the name of the event, and properties to pass with it. By default, we use mixpanelEventHandler,
144
+ * above, which, well, calls Mixpanel. This function allows you to change that to any function you want, in case you
145
+ * want to do something different.
146
+ */
147
+ out.setEventHandler = function(eventHandler) {
148
+ this.eventHandler = eventHandler;
149
+ };
150
+
151
+ out.setEventHandler(out.mixpanelEventHandler);
152
+
153
+ return out;
154
+ }());
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: meta_events
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Geweke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ - - <=
35
+ - !ruby/object:Gem::Version
36
+ version: 4.99.99
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '3.0'
44
+ - - <=
45
+ - !ruby/object:Gem::Version
46
+ version: 4.99.99
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.5'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ version: '1.5'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ~>
80
+ - !ruby/object:Gem::Version
81
+ version: '2.14'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ version: '2.14'
89
+ description:
90
+ email:
91
+ - ageweke@swiftype.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - .gitignore
97
+ - .travis.yml
98
+ - Gemfile
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - lib/meta_events.rb
103
+ - lib/meta_events/controller_methods.rb
104
+ - lib/meta_events/definition/category.rb
105
+ - lib/meta_events/definition/definition_set.rb
106
+ - lib/meta_events/definition/event.rb
107
+ - lib/meta_events/definition/version.rb
108
+ - lib/meta_events/engine.rb
109
+ - lib/meta_events/helpers.rb
110
+ - lib/meta_events/railtie.rb
111
+ - lib/meta_events/test_receiver.rb
112
+ - lib/meta_events/tracker.rb
113
+ - lib/meta_events/version.rb
114
+ - meta_events.gemspec
115
+ - spec/meta_events/controller_methods_and_helpers_spec.rb
116
+ - spec/meta_events/definition/category_spec.rb
117
+ - spec/meta_events/definition/definition_set_spec.rb
118
+ - spec/meta_events/definition/event_spec.rb
119
+ - spec/meta_events/definition/version_spec.rb
120
+ - spec/meta_events/tracker_spec.rb
121
+ - vendor/assets/javascripts/meta_events.js.erb
122
+ homepage: http://www.github.com/swiftype/meta_events
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.2.1
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Structured, documented, powerful event emitting library for Mixpanel and
146
+ other such systems.
147
+ test_files:
148
+ - spec/meta_events/controller_methods_and_helpers_spec.rb
149
+ - spec/meta_events/definition/category_spec.rb
150
+ - spec/meta_events/definition/definition_set_spec.rb
151
+ - spec/meta_events/definition/event_spec.rb
152
+ - spec/meta_events/definition/version_spec.rb
153
+ - spec/meta_events/tracker_spec.rb
154
+ has_rdoc: