@samitouri / QOS-React-2 / commits / 6858c7120c

Remove AnalyticsEventPlugin

We only ever had a couple internal users of this, and now we have none. Removing it from the repo for cleanliness.

Ben Alpert committed Jul 15, 2015 at 15:36 UTC 6858c7120c333cb15f9973e27456e830a0083941
3 files changed -371
src/renderers/dom/client/eventPlugins/AnalyticsEventPluginFactory.js deleted
-226
@@ -1,226 +0,0 @@
1 -/**
2 - * Copyright 2013-2015, Facebook, Inc.
3 - * All rights reserved.
4 - *
5 - * This source code is licensed under the BSD-style license found in the
6 - * LICENSE file in the root directory of this source tree. An additional grant
7 - * of patent rights can be found in the PATENTS file in the same directory.
8 - *
9 - * @providesModule AnalyticsEventPluginFactory
10 - *
11 - * This module provides a factory method to create the AnalyticsEventPlugin that
12 - * can be used to track the usage of React components that are of interest to
13 - * the user.
14 - *
15 - * In order to enable a component for analytics tracking, you need to specify
16 - * two additional attributes to the component when you describe the structure of
17 - * your component in the render() method:
18 - *
19 - * 1. 'data-analytics-id': This represents a unique ID that the analytics module
20 - * will use to identify this component in all the analytics data. Note that
21 - * this is independent of the ref or the DOM id of the element. Over the
22 - * lifetime of the product, even if the component id or ref needs to be
23 - * changed, as long as you can ensure the analytics ID doesnt change, the
24 - * historical data can be correlated. Also note that React does NOT do
25 - * anything to guarantee or enforce uniqueness of this ID. If its not unique
26 - * the analytics data reported will be incorrect.
27 - *
28 - * 2. 'data-analytics-events': This is a comma separated list of DOM events that
29 - * you want analytics on. React currently supports tracking only on a
30 - * distinct set of events (See topLevelTypesToAnalyticsEvent).
31 - * If the list contains an event that React does not recognize for analytics
32 - * tracking, in __DEV__, an error will be thrown. Note that it is case
33 - * sensitive and space sensitive.
34 - *
35 - * By default the AnalyticsEventPlugin is NOT enabled in React. To use it, you
36 - * need to create the plugin using the factory method and add it to the list of
37 - * Plugins maintained in the EventPluginHub before your component is rendered.
38 - * As creation parameters you can specify two arguments:
39 - *
40 - * 1. callback: This is a required parameter. In __DEV__, an error will be
41 - * thrown if this param is missing. The callback will be called with the
42 - * analyticsData as an argument. The analyticsData will contain one property
43 - * per every React component, identified by its data-analytics-id. The value
44 - * of this property will be an object containing properties corresponding to
45 - * each of the comma separated events specified in data-analytics-events.
46 - *
47 - * For example, if you have:
48 - * <Button ...
49 - * data-analytics-id="createButton"
50 - * data-analytics-events="click"
51 - * />
52 - * and
53 - * <TextBox ...
54 - * data-analytics-id="disclaimerBox"
55 - * data-analytics-events="focus,scroll"
56 - * />
57 - * analyticsData will be something like:
58 - * '{"createButton":{"click":50}, "disclaimerBox":{"focus":15, "scroll":5}}'
59 - *
60 - * DO NOT mutate the data that you get in the callback. Mutating it will lead
61 - * to errors and unstable behavior.
62 - *
63 - * The React component will be included for analytics as long as some user
64 - * interaction has happened with that component. If no user interaction has
65 - * happened with any of the components tracked for analytics, the callback
66 - * will not be called.
67 - *
68 - * 2. interval (in milliseconds): This is an optional parameter to specify the
69 - * interval at which the callback needs to be called. It needs to be greater
70 - * than the 2 minutes (which is the default value if this parameter is not
71 - * specified or a value less than 2 minutes is specified)
72 - *
73 - * Please refer to the unit tests AnalyticsEventPlugin-test.js for details on
74 - * usage.
75 - */
76 -
77 -'use strict';
78 -
79 -var ExecutionEnvironment = require('ExecutionEnvironment');
80 -
81 -var emptyFunction = require('emptyFunction');
82 -var invariant = require('invariant');
83 -var warning = require('warning');
84 -var topLevelTypes = require('EventConstants').topLevelTypes;
85 -
86 -var ANALYTICS_ID = 'data-analytics-id';
87 -var ANALYTICS_EVENTS = 'data-analytics-events';
88 -var DEFAULT_INTERVAL_MS = 2 * 60 * 1000; // 2 minutes
89 -
90 -var analyticsData = {};
91 -
92 -// List of topLevel event types that React supports for analytics tracking
93 -var topLevelTypesToAnalyticsEvent = {
94 - topClick: 'click',
95 - topDoubleClick: 'doubleClick',
96 - wheel: 'wheel',
97 - topTouchStart: 'touchStart',
98 - topTouchEnd: 'touchEnd',
99 - topTouchMove: 'touchMove',
100 - topTouchCancel: 'touchCancel',
101 - topKeyUp: 'keyUp',
102 - topKeyPress: 'keyPress',
103 - topKeyDown: 'keyDown',
104 - topFocus: 'focus',
105 - topBlur: 'blur',
106 - topScroll: 'scroll',
107 - topChange: 'change',
108 -};
109 -
110 -if (__DEV__) {
111 - var analyticsEventNameToTopLevelType = {
112 - 'click': topLevelTypes.topClick,
113 - 'doubleClick': topLevelTypes.topDoubleClick,
114 - 'wheel': topLevelTypes.wheel,
115 - 'touchStart': topLevelTypes.topTouchStart,
116 - 'touchEnd': topLevelTypes.topTouchEnd,
117 - 'touchMove': topLevelTypes.topTouchMove,
118 - 'touchCancel': topLevelTypes.topTouchCancel,
119 - 'keyUp': topLevelTypes.topKeyUp,
120 - 'keyPress': topLevelTypes.topKeyPress,
121 - 'keyDown': topLevelTypes.topKeyDown,
122 - 'focus': topLevelTypes.topFocus,
123 - 'blur': topLevelTypes.topBlur,
124 - 'scroll': topLevelTypes.topScroll,
125 - 'change': topLevelTypes.topChange,
126 - };
127 -}
128 -
129 -/**
130 - * This plugin does not really extract any synthetic events. Rather it just
131 - * looks at the top-level event and bumps up counters as appropriate
132 - *
133 - * @param {string} topLevelType Record from `EventConstants`.
134 - * @param {DOMEventTarget} topLevelTarget The listening component root node.
135 - * @param {string} topLevelTargetID ID of `topLevelTarget`.
136 - * @param {object} nativeEvent Native browser event.
137 - * @return {*} An accumulation of synthetic events.
138 - * @see {EventPluginHub.extractEvents}
139 - */
140 -function extractEvents(
141 - topLevelType,
142 - topLevelTarget,
143 - topLevelTargetID,
144 - nativeEvent) {
145 - var currentEvent = topLevelTypesToAnalyticsEvent[topLevelType];
146 - if (!currentEvent || !topLevelTarget || !topLevelTarget.attributes) {
147 - return null;
148 - }
149 -
150 - var analyticsID = topLevelTarget.getAttribute(ANALYTICS_ID);
151 - var analyticsEventsStr = topLevelTarget.getAttribute(ANALYTICS_EVENTS);
152 - if (!analyticsID || !analyticsEventsStr) {
153 - return null;
154 - }
155 -
156 - var analyticsEventsArr = analyticsEventsStr.split(',');
157 - if (!analyticsData.hasOwnProperty(analyticsID)) {
158 - initAnalyticsDataForID(analyticsID, analyticsEventsArr);
159 - }
160 -
161 - if (analyticsEventsArr.indexOf(currentEvent) !== -1) {
162 - analyticsData[analyticsID][currentEvent]++;
163 - }
164 -
165 - return null;
166 -}
167 -
168 -/**
169 - * Initialize the analytics data for a specific element identified by the
170 - * analyticsID - Create an entry in the analyticsData object for the element and
171 - * initialize all counters for that element to 0.
172 - */
173 -function initAnalyticsDataForID(analyticsID, analyticsEventsArr) {
174 - analyticsData[analyticsID] = {};
175 - analyticsEventsArr.forEach(function(analyticsEvent) {
176 - if (__DEV__) {
177 - warning(
178 - analyticsEventNameToTopLevelType[analyticsEvent],
179 - 'Invalid analyticsEvent:%s for analyticsID:%s',
180 - analyticsEvent,
181 - analyticsID
182 - );
183 - }
184 - analyticsData[analyticsID][analyticsEvent] = 0;
185 - });
186 -}
187 -
188 -/**
189 - * Returns the analytics event plugin given the callback that needs to be
190 - * invoked for reporting analytics and the interval at which the callback needs
191 - * to be invoked. This interval has to be atleast DEFAULT_INTERVAL_MS.
192 - */
193 -var createAnalyticsPlugin = function(cb, interval) {
194 - invariant(
195 - ExecutionEnvironment.canUseDOM,
196 - 'createAnalyticsPlugin(...): The DOM is not supported in the execution ' +
197 - 'environment.'
198 - );
199 -
200 - if (__DEV__) {
201 - warning(
202 - cb,
203 - 'createAnalyticsPlugin(...): You must provide a callback.'
204 - );
205 - }
206 - cb = cb || emptyFunction;
207 -
208 - setInterval(
209 - function() {
210 - if (Object.keys(analyticsData).length) {
211 - // Invoke the callback with a clone of analyticsData, otherwise our
212 - // analyticsData will be dirtied by user changes
213 - cb(analyticsData);
214 - }
215 - },
216 - interval > DEFAULT_INTERVAL_MS ? interval : DEFAULT_INTERVAL_MS
217 - );
218 -
219 - return {extractEvents: extractEvents};
220 -};
221 -
222 -var AnalyticsEventPluginFactory = {
223 - createAnalyticsPlugin: createAnalyticsPlugin,
224 -};
225 -
226 -module.exports = AnalyticsEventPluginFactory;
src/renderers/dom/client/eventPlugins/DefaultEventPluginOrder.js
-1
@@ -30,7 +30,6 @@ var DefaultEventPluginOrder = [
30 keyOf({ChangeEventPlugin: null}),
31 keyOf({SelectEventPlugin: null}),
32 keyOf({BeforeInputEventPlugin: null}),
33 - keyOf({AnalyticsEventPlugin: null}),
33 ];
34
35 module.exports = DefaultEventPluginOrder;
src/renderers/dom/client/eventPlugins/__tests__/AnalyticsEventPlugin-test.js deleted
-144
@@ -1,144 +0,0 @@
1 -/**
2 - * Copyright 2013-2015, Facebook, Inc.
3 - * All rights reserved.
4 - *
5 - * This source code is licensed under the BSD-style license found in the
6 - * LICENSE file in the root directory of this source tree. An additional grant
7 - * of patent rights can be found in the PATENTS file in the same directory.
8 - *
9 - * @emails react-core
10 - */
11 -
12 -'use strict';
13 -
14 -var mocks = require('mocks');
15 -
16 -describe('AnalyticsEventPlugin', function() {
17 - var AnalyticsEventPluginFactory;
18 - var EventPluginHub;
19 - var EventPluginRegistry;
20 - var React;
21 - var ReactTestUtils;
22 -
23 - var DefaultEventPluginOrder;
24 - var EnterLeaveEventPlugin;
25 - var ChangeEventPlugin;
26 - var ReactInstanceHandles;
27 - var SimpleEventPlugin;
28 -
29 - beforeEach(function() {
30 - AnalyticsEventPluginFactory = require('AnalyticsEventPluginFactory');
31 - EventPluginHub = require('EventPluginHub');
32 - EventPluginRegistry = require('EventPluginRegistry');
33 - React = require('React');
34 - ReactTestUtils = require('ReactTestUtils');
35 -
36 - EventPluginRegistry._resetEventPlugins();
37 -
38 - // Re-inject default events system after resetting.
39 - DefaultEventPluginOrder = require('DefaultEventPluginOrder');
40 - EnterLeaveEventPlugin = require('EnterLeaveEventPlugin');
41 - ChangeEventPlugin = require('ChangeEventPlugin');
42 - ReactInstanceHandles = require('ReactInstanceHandles');
43 - SimpleEventPlugin = require('SimpleEventPlugin');
44 -
45 - EventPluginHub.injection.injectEventPluginOrder(DefaultEventPluginOrder);
46 - EventPluginHub.injection.injectInstanceHandle(ReactInstanceHandles);
47 -
48 - EventPluginHub.injection.injectEventPluginsByName({
49 - 'SimpleEventPlugin': SimpleEventPlugin,
50 - 'EnterLeaveEventPlugin': EnterLeaveEventPlugin,
51 - 'ChangeEventPlugin': ChangeEventPlugin,
52 - });
53 -
54 - spyOn(console, 'error');
55 - });
56 -
57 - it('should count events correctly', function() {
58 - var numClickEvents = 5;
59 - var numDoubleClickEvents = 7;
60 - var TEST_ANALYTICS_ID = 'test_analytics_id';
61 - var TestValidEvents = React.createClass({
62 - render: function() {
63 - return (
64 - <div ref="testDiv"
65 - data-analytics-id={TEST_ANALYTICS_ID}
66 - data-analytics-events="click,doubleClick">
67 - Test
68 - </div>
69 - );
70 - },
71 - });
72 - var renderedComponent =
73 - ReactTestUtils.renderIntoDocument(<TestValidEvents />);
74 -
75 - var cb = mocks.getMockFunction().mockImplementation(
76 - function(analyticsData) {
77 - expect(Object.keys(analyticsData).length).toBe(1);
78 - expect(Object.keys(analyticsData[TEST_ANALYTICS_ID]).length).toBe(2);
79 - expect(analyticsData[TEST_ANALYTICS_ID].click).toBe(numClickEvents);
80 - expect(analyticsData[TEST_ANALYTICS_ID].doubleClick).toBe(
81 - numDoubleClickEvents
82 - );
83 - }
84 - );
85 -
86 - EventPluginHub.injection.injectEventPluginsByName({
87 - AnalyticsEventPlugin:
88 - AnalyticsEventPluginFactory.createAnalyticsPlugin(cb),
89 - });
90 -
91 - // Simulate some clicks
92 - for (var i = 0; i < numClickEvents; i++) {
93 - ReactTestUtils.SimulateNative.click(renderedComponent.refs.testDiv);
94 - }
95 - // Simulate some double clicks
96 - for (i = 0; i < numDoubleClickEvents; i++) {
97 - ReactTestUtils.SimulateNative.doubleClick(renderedComponent.refs.testDiv);
98 - }
99 - // Simulate some other events not being tracked for analytics
100 - ReactTestUtils.SimulateNative.focus(renderedComponent.refs.testDiv);
101 -
102 - window.mockRunTimersOnce();
103 - expect(cb).toBeCalled();
104 - });
105 -
106 - it('warn on no callback', function() {
107 - AnalyticsEventPluginFactory.createAnalyticsPlugin(null);
108 - expect(console.error.calls.length).toBe(1);
109 - expect(console.error.argsForCall[0][0]).toBe(
110 - 'Warning: createAnalyticsPlugin(...): You must provide a callback.'
111 - );
112 - });
113 -
114 - it('warn on invalid analytics events', function() {
115 - var TestInvalidEvents = React.createClass({
116 - render: function() {
117 - return (
118 - <div ref="testDiv"
119 - data-analytics-id="test_invalid_events"
120 - data-analytics-events="click,123">
121 - Test
122 - </div>
123 - );
124 - },
125 - });
126 - var renderedComponent =
127 - ReactTestUtils.renderIntoDocument(<TestInvalidEvents />);
128 -
129 - var cb = mocks.getMockFunction();
130 -
131 - EventPluginHub.injection.injectEventPluginsByName({
132 - AnalyticsEventPlugin: AnalyticsEventPluginFactory.createAnalyticsPlugin(
133 - cb
134 - ),
135 - });
136 -
137 - ReactTestUtils.SimulateNative.click(renderedComponent.refs.testDiv);
138 -
139 - expect(console.error.calls.length).toBe(1);
140 - expect(console.error.argsForCall[0][0]).toBe(
141 - 'Warning: Invalid analyticsEvent:123 for analyticsID:test_invalid_events'
142 - );
143 - });
144 -});