mixpanel-browser 2.48.1 → 2.50.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -78,6 +78,20 @@ ___
78
78
  Clear the user's opt in/out status of data tracking and cookies/localstorage for this Mixpanel instance
79
79
 
80
80
 
81
+ ### Usage:
82
+
83
+ ```javascript
84
+ // clear user's opt-in/out status
85
+ mixpanel.clear_opt_in_out_tracking();
86
+
87
+ // clear user's opt-in/out status with specific cookie configuration - should match
88
+ // configuration used when opt_in_tracking/opt_out_tracking methods were called.
89
+ mixpanel.clear_opt_in_out_tracking({
90
+ cookie_expiration: 30,
91
+ secure_cookie: true
92
+ });
93
+ ```
94
+
81
95
 
82
96
 
83
97
  | Argument | Type | Description |
@@ -189,6 +203,13 @@ ___
189
203
  Check whether the user has opted in to data tracking and cookies/localstorage for this Mixpanel instance
190
204
 
191
205
 
206
+ ### Usage:
207
+
208
+ ```javascript
209
+ var has_opted_in = mixpanel.has_opted_in_tracking();
210
+ // use has_opted_in value
211
+ ```
212
+
192
213
 
193
214
 
194
215
  | Argument | Type | Description |
@@ -207,6 +228,13 @@ ___
207
228
  Check whether the user has opted out of data tracking and cookies/localstorage for this Mixpanel instance
208
229
 
209
230
 
231
+ ### Usage:
232
+
233
+ ```javascript
234
+ var has_opted_out = mixpanel.has_opted_out_tracking();
235
+ // use has_opted_out value
236
+ ```
237
+
210
238
 
211
239
 
212
240
  | Argument | Type | Description |
@@ -271,6 +299,23 @@ ___
271
299
  Opt the user in to data tracking and cookies/localstorage for this Mixpanel instance
272
300
 
273
301
 
302
+ ### Usage:
303
+
304
+ ```javascript
305
+ // opt user in
306
+ mixpanel.opt_in_tracking();
307
+
308
+ // opt user in with specific event name, properties, cookie configuration
309
+ mixpanel.opt_in_tracking({
310
+ track_event_name: 'User opted in',
311
+ track_event_properties: {
312
+ 'Email': 'jdoe@example.com'
313
+ },
314
+ cookie_expiration: 30,
315
+ secure_cookie: true
316
+ });
317
+ ```
318
+
274
319
 
275
320
 
276
321
  | Argument | Type | Description |
@@ -294,6 +339,19 @@ ___
294
339
  Opt the user out of data tracking and cookies/localstorage for this Mixpanel instance
295
340
 
296
341
 
342
+ ### Usage:
343
+
344
+ ```javascript
345
+ // opt user out
346
+ mixpanel.opt_out_tracking();
347
+
348
+ // opt user out with different cookie configuration from Mixpanel instance
349
+ mixpanel.opt_out_tracking({
350
+ cookie_expiration: 30,
351
+ secure_cookie: true
352
+ });
353
+ ```
354
+
297
355
 
298
356
 
299
357
  | Argument | Type | Description |
@@ -438,6 +496,16 @@ The default config is:
438
496
 
439
497
  ```javascript
440
498
  {
499
+ // host for requests (customizable for e.g. a local proxy)
500
+ api_host: 'https://api-js.mixpanel.com',
501
+
502
+ // endpoints for different types of requests
503
+ api_routes: {
504
+ track: 'track/',
505
+ engage: 'engage/',
506
+ groups: 'groups/',
507
+ }
508
+
441
509
  // HTTP method for tracking requests
442
510
  api_method: 'POST'
443
511
 
@@ -696,9 +764,47 @@ If you pass a function in as the properties argument, the function will receive
696
764
 
697
765
  ___
698
766
  ## mixpanel.track_pageview
699
- Track a default Mixpanel page view event, which includes extra default event properties to improve page view data. The <code>config.track_pageview</code> option for <a href="#mixpanelinit">mixpanel.init()</a> may be turned on for tracking page loads automatically.
767
+ Track a default Mixpanel page view event, which includes extra default event properties to improve page view data.
768
+
769
+
770
+ ### Usage:
771
+
772
+ ```javascript
773
+ // track a default $mp_web_page_view event
774
+ mixpanel.track_pageview();
775
+
776
+ // track a page view event with additional event properties
777
+ mixpanel.track_pageview({'ab_test_variant': 'card-layout-b'});
700
778
 
779
+ // example approach to track page views on different page types as event properties
780
+ mixpanel.track_pageview({'page': 'pricing'});
781
+ mixpanel.track_pageview({'page': 'homepage'});
701
782
 
783
+ // UNCOMMON: Tracking a page view event with a custom event_name option. NOT expected to be used for
784
+ // individual pages on the same site or product. Use cases for custom event_name may be page
785
+ // views on different products or internal applications that are considered completely separate
786
+ mixpanel.track_pageview({'page': 'customer-search'}, {'event_name': '[internal] Admin Page View'});
787
+
788
+ ```
789
+
790
+
791
+ ### Notes:
792
+ The <code>config.track_pageview</code> option for <a href="#mixpanelinit">mixpanel.init()</a> may be turned on for tracking page loads automatically.
793
+
794
+
795
+ ```javascript
796
+ // track only page loads
797
+ mixpanel.init(PROJECT_TOKEN, {track_pageview: true});
798
+
799
+ // track when the URL changes in any manner
800
+ mixpanel.init(PROJECT_TOKEN, {track_pageview: 'full-url'});
801
+
802
+ // track when the URL changes, ignoring any changes in the hash part
803
+ mixpanel.init(PROJECT_TOKEN, {track_pageview: 'url-with-path-and-query-string'});
804
+
805
+ // track when the path changes, ignoring any query parameter or hash changes
806
+ mixpanel.init(PROJECT_TOKEN, {track_pageview: 'url-with-path'});
807
+ ```
702
808
 
703
809
 
704
810
  | Argument | Type | Description |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mixpanel-browser",
3
- "version": "2.48.1",
3
+ "version": "2.50.0",
4
4
  "description": "The official Mixpanel JavaScript browser client library",
5
5
  "main": "dist/mixpanel.cjs.js",
6
6
  "directories": {
@@ -32,6 +32,7 @@
32
32
  },
33
33
  "homepage": "https://github.com/mixpanel/mixpanel-js",
34
34
  "devDependencies": {
35
+ "@rollup/plugin-node-resolve": "15.2.3",
35
36
  "babel": "6.5.2",
36
37
  "babel-core": "6.7.2",
37
38
  "babel-preset-es2015": "6.6.0",
@@ -51,10 +52,14 @@
51
52
  "morgan": "1.9.1",
52
53
  "rdme": "7.5.0",
53
54
  "request": "2.88.0",
54
- "rollup": "0.25.8",
55
+ "rollup": "2.79.1",
56
+ "rollup-plugin-esbuild": "4.10.3",
55
57
  "rollup-plugin-npm": "1.4.0",
56
58
  "sinon": "8.1.1",
57
59
  "sinon-chai": "3.5.0",
58
60
  "webpack": "1.12.2"
61
+ },
62
+ "dependencies": {
63
+ "rrweb": "2.0.0-alpha.4"
59
64
  }
60
65
  }
package/src/config.js CHANGED
@@ -1,6 +1,6 @@
1
1
  var Config = {
2
2
  DEBUG: false,
3
- LIB_VERSION: '2.48.1'
3
+ LIB_VERSION: '2.50.0'
4
4
  };
5
5
 
6
6
  export default Config;
@@ -0,0 +1,4 @@
1
+ /* eslint camelcase: "off" */
2
+ import { init_from_snippet } from '../mixpanel-core';
3
+
4
+ init_from_snippet();
@@ -1,5 +1,5 @@
1
1
  /* eslint camelcase: "off" */
2
- import { init_as_module } from './mixpanel-core';
2
+ import { init_as_module } from '../mixpanel-core';
3
3
 
4
4
  var mixpanel = init_as_module();
5
5
 
@@ -0,0 +1,143 @@
1
+ import './mixpanel-jslib-snippet';
2
+
3
+ /*
4
+ * @see src/loaders/mixpanel-js-wrapper.md
5
+ */
6
+ (function (win, wrapper) {
7
+
8
+ // If window.mixpanel doesn't exist, return
9
+ if (!win['mixpanel'] || typeof win['mixpanel']['init'] !== 'function') return;
10
+
11
+ // Enumerate available commands
12
+ var commandEnum = [
13
+ 'add_group',
14
+ 'alias',
15
+ 'clear_opt_in_out_tracking',
16
+ 'disable',
17
+ /* Ignore getters
18
+ 'get_config',
19
+ 'get_distinct_id',
20
+ 'get_group',
21
+ 'get_property',
22
+ 'has_opted_in_tracking',
23
+ 'has_opted_out_tracking',
24
+ */
25
+ /* Ignore init
26
+ 'init'
27
+ */
28
+ 'identify',
29
+ 'opt_in_tracking',
30
+ 'opt_out_tracking',
31
+ /* Ignore push
32
+ 'push',
33
+ */
34
+ 'register',
35
+ 'register_once',
36
+ 'remove_group',
37
+ 'reset',
38
+ 'set_config',
39
+ 'set_group',
40
+ 'time_event',
41
+ 'track',
42
+ 'track_forms',
43
+ 'track_links',
44
+ 'track_pageview',
45
+ 'track_with_groups',
46
+ 'unregister',
47
+ 'people.append',
48
+ 'people.clear_charges',
49
+ 'people.delete_user',
50
+ 'people.increment',
51
+ 'people.remove',
52
+ 'people.set',
53
+ 'people.set_once',
54
+ 'people.track_charge',
55
+ 'people.union',
56
+ 'people.unset',
57
+ 'group.remove',
58
+ 'group.set',
59
+ 'group.set_once',
60
+ 'group.union',
61
+ 'group.unset'
62
+ ];
63
+
64
+ /* The people API can't be used with the .push() interface, so it requires its
65
+ * own helper method. To interact with it, simply use the _mixpanel interface
66
+ * as before.
67
+ *
68
+ * window._mixpanel('<libraryName.>people.set', 'gender', 'm');
69
+ *
70
+ */
71
+ var people = function (mp, cmd, args) {
72
+ // Extract the command
73
+ var peopleCmd = cmd.split('.').pop();
74
+
75
+ // Call the respective mixpanel method
76
+ mp['people'][peopleCmd].apply(mp['people'], args);
77
+ };
78
+
79
+ /* To utilize the group API, the command must include the group key and ID as
80
+ * an array in the second argument.
81
+ *
82
+ * window._mixpanel('<libraryName.>.group.set', ['group_key', 'group_id'], {
83
+ * someGroupProperty: 'someGroupValue'
84
+ * });
85
+ *
86
+ */
87
+ var group = function (mp, cmd, args) {
88
+ // Extract the command
89
+ var groupCmd = cmd.split('.').pop();
90
+
91
+ // Extract the group info
92
+ var groupInfo = args.shift();
93
+
94
+ // Validate the group array
95
+ if (!Array.isArray(groupInfo) || groupInfo.length !== 2) return;
96
+
97
+ // Get group reference
98
+ var group = mp['get_group'].apply(mp, groupInfo);
99
+
100
+ // Call the respective group method
101
+ group[groupCmd].apply(group, args);
102
+
103
+ };
104
+
105
+ // Build the command wrapper logic
106
+ win[wrapper] = win[wrapper] || function () {
107
+
108
+ // Build array out of arguments
109
+ var args = [].slice.call(arguments, 0);
110
+
111
+ // Pick the first argument as the command
112
+ var cmd = args.shift();
113
+
114
+ /* Commands can be passed to different namespaces with syntax:
115
+ * window._mixpanel('libraryName.command', arguments)
116
+ */
117
+ var libraryName = null;
118
+ var cmdParts = cmd.match(/^([^.]+)\.(.+)$/);
119
+ if (cmdParts && cmdParts.length === 3 && !/people|group/.test(cmdParts[1])) {
120
+ libraryName = cmdParts[1];
121
+ cmd = cmdParts[2];
122
+ }
123
+
124
+ // If libraryName is set, use that as the mixpanel interface
125
+ var mp = libraryName ? window['mixpanel'][libraryName] : window['mixpanel'];
126
+
127
+ // Return if namespace not found
128
+ if (!mp) return;
129
+
130
+ // If cmd is not one of the available ones, return
131
+ if (commandEnum.indexOf(cmd) === -1) return;
132
+
133
+ // Handle people command
134
+ if (/^people\./.test(cmd)) return people(mp, cmd, args);
135
+
136
+ // Handle group command
137
+ if (/^group\./.test(cmd)) return group(mp, cmd, args);
138
+
139
+ // Push the command to mixpanel
140
+ return mp.push.apply(mp, [[cmd].concat(args)]);
141
+
142
+ };
143
+ })(window, '_mixpanel');
@@ -0,0 +1,114 @@
1
+ # Mixpanel JavaScript SDK wrapper for Google Tag Manager
2
+ The purpose of this wrapper is to provide a JavaScript interface for interacting with the `window.mixpanel` interface.
3
+
4
+ The wrapper has been designed with [Google Tag Manager](https://tagmanager.google.com) in mind. GTM's [custom templates](https://developers.google.com/tag-manager/templates) offer a way to deploy custom JavaScript without having to resort to Custom HTML tags and with the ability to craft a user interface for the scripts within Google Tag Manager.
5
+
6
+ However, one of the defining features of custom templates is their [sandboxed JavaScript API](https://developers.google.com/tag-manager/templates/sandboxed-javascript) inventory, which severely restricts what type of browser JavaScript can be executed in the template code.
7
+
8
+ Mixpanel's [JavaScript SDK](https://developer.mixpanel.com/docs/javascript-full-api-reference) makes use of JavaScript features which are not permitted by the sandbox of GTM's custom templates (e.g. object instances initiated with the `new` keyword, `this` and `prototype`, etc.).
9
+
10
+ Thus, in order to interact with Mixpanel's JavaScript SDK via Google Tag Manager's custom templates (or any other context where the aforementioned JavaScript features cannot be used), this wrapper is required. [The template loads this wrapper](https://github.com/mixpanel/mixpanel-gtm-template/blob/2f577d826acc7d96d138367db339035b8f9df359/src/template.tpl#L1383) in the sandboxed template code.
11
+
12
+ # Deployment
13
+ The wrapper is served by the Mixpanel CDN at https://cdn.mxpnl.com/libs/mixpanel-js-wrapper.js
14
+
15
+ # How it works
16
+ When the wrapper JavaScript is loaded in the browser, the global method `window._mixpanel()` is created for interacting with the wrapper.
17
+
18
+ This namespace includes all the methods supported by the [JavaScript SDK](https://developer.mixpanel.com/docs/javascript-full-api-reference) with some exceptions (see below). Each method can be invoked by passing the command name as the first argument of the call to `window._mixpanel()`.
19
+
20
+ If this command name is prefixed with `<string>.`, then `<string>` will be used as the [library name](https://developer.mixpanel.com/docs/javascript-full-api-reference#mixpanelinit). After the command, all additional arguments are processed as arguments to the command method itself.
21
+
22
+ For example, to dispatch an event named `Add To Cart` using a custom library name, you could use this command:
23
+
24
+ ```
25
+ window._mixpanel(
26
+ 'myTracker.track', // Run the track command and utilize the library name "myTracker"
27
+ 'Add To Cart', // Event name is the first argument to the track command
28
+ {product_id: 'shirt123'} // (optional) Event parameters are the second argument to the track command
29
+ );
30
+ ```
31
+
32
+ ## `group`
33
+ [Link to specification](https://developer.mixpanel.com/docs/javascript-full-api-reference#mixpanelgroup)
34
+
35
+ Use this command to interact with group properties.
36
+
37
+ Syntax:
38
+
39
+ `window._mixpanel('<library_name.>group.<group_command>', ['<group_key>', '<group_id>'], <parameters>)`
40
+
41
+ Example:
42
+ ```
43
+ // Union a property for a group
44
+ window._mixpanel(
45
+ 'group.union',
46
+ ['my_group_key', 'my_group_id'],
47
+ 'location',
48
+ ['San Francisco', 'London']
49
+ );
50
+ ```
51
+
52
+ | Parameter name | Description |
53
+ |----------------|-------------|
54
+ | `library_name` | (Optional) target a specific library/instance name with this command |
55
+ | `group_command` | (Required) one of `set`, `set_once`, `remove`, `union`, `unset` |
56
+ | `parameters` | All the parameters you want to submit to the `group` command. |
57
+
58
+ ## `people`
59
+ [Link to specification](https://developer.mixpanel.com/docs/javascript-full-api-reference#mixpanelpeople)
60
+
61
+ Interact with the people analytics property.
62
+
63
+ Syntax:
64
+
65
+ `window._mixpanel('<library_name.>people.<people_command>', <parameters>)`
66
+
67
+ Example:
68
+ ```
69
+ // Set the "gender" property "n/a"
70
+ window._mixpanel(
71
+ 'people.set',
72
+ 'gender',
73
+ 'n/a'
74
+ );
75
+ ```
76
+
77
+ | Parameter name | Description |
78
+ |----------------|-------------|
79
+ | `library_name` | (Optional) target a specific library/instance name with this command |
80
+ | `people_command` | (Required) one of `append`, `clear_charge`, `delete_user`, `increment`, `remove`, `set`, `set_once`, `track_charge`, `union`, `unset` |
81
+ | `parameters` | All the parameters you want to submit to the `people` command. |
82
+
83
+ ## Other commands
84
+
85
+ All other commands can be sent to the `_mixpanel` interface like this:
86
+
87
+ `window._mixpanel('<library_name.><command_name>', <parameters>)`
88
+
89
+ Example:
90
+
91
+ ```
92
+ window._mixpanel(
93
+ 'my_mixpanel.register',
94
+ {'Account Type': 'Free'}
95
+ );
96
+ ```
97
+
98
+ ## Exceptions
99
+
100
+ Because the wrapper only pipes commands to the actual `window.mixpanel` interface, the wrapper cannot be used for get-methods. So the following commands **are not** supported by the wrapper:
101
+
102
+ `get_config`
103
+ `get_distinct_id`
104
+ `get_group`
105
+ `get_property`
106
+ `has_opted_in_tracking`
107
+ `has_opted_out_tracking`
108
+
109
+ The following commands are also disabled due to not being relevant with the wrapper:
110
+
111
+ `push`
112
+ `init`
113
+
114
+ Initialization is done with the `window.mixpanel` interface directly, and `push` is already used by the wrapper to forward calls to the main interface.