rozmova-analytics 1.0.6 → 1.0.8

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.
package/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # rozmova-analytics
2
+
3
+ [![npm-version](https://img.shields.io/npm/v/rozmova-analytics.svg)](https://www.npmjs.com/package/rozmova-analytics)
4
+ [![minified-size](https://img.shields.io/bundlephobia/minzip/rozmova-analytics@1.0.7)](https://bundlephobia.com/package/rozmova-analytics)
5
+
6
+ A lightweight JavaScript analytics library designed to streamline event tracking, user identification, and common analytics parameter management. This package integrates with Mixpanel and Google Analytics to provide seamless data collection and insights.
7
+
8
+ ## Installation
9
+
10
+ Install the package using npm:
11
+
12
+ ```bash
13
+ npm install rozmova-analytics
14
+ ```
15
+
16
+ Alternatively, you can include the library via jsDelivr:
17
+
18
+ ```bash
19
+ <script src="https://cdn.jsdelivr.net/npm/rozmova-analytics/dist/index.js"></script>
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ Import the library in your project and initialize it:
25
+
26
+ ```javascript
27
+ import {
28
+ generateUserId,
29
+ getCommonParams,
30
+ getUserId,
31
+ init,
32
+ resetUser,
33
+ setUser,
34
+ trackEvent,
35
+ setLocale,
36
+ } from 'rozmova-analytics';
37
+
38
+ // Initialize the library
39
+ init('en', 'web');
40
+
41
+ // Set user
42
+ setUser('user-id-123', { email: 'user@example.com', name: 'John Doe' });
43
+
44
+ // Track an event
45
+ trackEvent('button_click', { button_name: 'Sign Up' });
46
+ ```
47
+
48
+ ## API
49
+
50
+ ### `init(locale?: string, platform?: string)`
51
+ Initializes the analytics library, setting up Mixpanel and Google Analytics integrations. Optionally, set a locale and platform.
52
+
53
+ ### `generateUserId()`
54
+ Generates a unique user ID, stores it in cookies and localStorage, and returns the ID.
55
+
56
+ ### `getUserId()`
57
+ Retrieves the user ID from cookies, localStorage, or URL query parameters. If not found, generates a new one.
58
+
59
+ ### `getCommonParams()`
60
+ Returns an object with common analytics parameters such as device, browser, referrer, and UTM parameters.
61
+
62
+ ### `setLocale(newLocale: string)`
63
+ Sets the locale for analytics data.
64
+
65
+ ### `setUser(userId: string, userParams: { email: string; name: string })`
66
+ Associates a user with the provided user ID and sets user properties in Mixpanel and Google Analytics.
67
+
68
+ ### `resetUser()`
69
+ Resets the current user, generating a new user ID and reinitializing the library.
70
+
71
+ ### `trackEvent(eventName: string, properties?: EventParams)`
72
+ Tracks an event with the specified name and optional properties.
73
+
74
+ ## Browser Support
75
+ The package works on modern browsers and supports the following:
76
+ - Chrome
77
+ - Firefox
78
+ - Safari
79
+ - Edge
80
+
81
+ ## Example Integration
82
+
83
+ ```javascript
84
+ import { init, trackEvent, setUser, resetUser } from 'rozmova-analytics';
85
+
86
+ // Initialize analytics
87
+ init('en', 'ios');
88
+
89
+ // Track a page view event
90
+ trackEvent('page_view', { page: 'Home' });
91
+
92
+ // Set user details
93
+ setUser('user-456', { email: 'user456@example.com', name: 'Jane Doe' });
94
+
95
+ // Reset the user
96
+ resetUser();
97
+ ```
98
+
99
+ ## Contributing
100
+ Contributions are welcome! Please fork the repository and create a pull request with your changes.
101
+
102
+ ## License
103
+ This project is licensed under the MIT License.
104
+
@@ -1,8 +1,12 @@
1
1
  import { AnalyticsCommonParams, EventParams } from "./types";
2
2
  export declare const generateUserId: () => string;
3
3
  export declare const getUserId: () => string;
4
+ export declare const setLocale: (newLocale: string) => void;
5
+ export declare const getLocale: () => string;
6
+ export declare const setPlatform: (newPlatform: string) => void;
7
+ export declare const getPlatform: () => string;
4
8
  export declare const getCommonParams: () => AnalyticsCommonParams;
5
- export declare const init: (additionalParams?: EventParams) => void;
9
+ export declare const init: (locale?: string, platform?: string) => void;
6
10
  export declare const trackEvent: (eventName: string, properties?: EventParams) => void;
7
11
  export declare const setUser: (userId: string, userParams: {
8
12
  email: string;
package/dist/analytics.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.trackEvent = exports.setUser = exports.resetUser = exports.init = exports.getUserId = exports.getCommonParams = exports.generateUserId = void 0;
6
+ exports.trackEvent = exports.setUser = exports.setPlatform = exports.setLocale = exports.resetUser = exports.init = exports.getUserId = exports.getPlatform = exports.getLocale = exports.getCommonParams = exports.generateUserId = void 0;
7
7
  var _jsCookie = _interopRequireDefault(require("js-cookie"));
8
8
  var _mixpanelBrowser = _interopRequireDefault(require("mixpanel-browser"));
9
9
  var _nanoid = require("nanoid");
@@ -50,6 +50,18 @@ var getCurrentParams = function getCurrentParams() {
50
50
  });
51
51
  return currentParams;
52
52
  };
53
+ var setLocale = exports.setLocale = function setLocale(newLocale) {
54
+ window.analyticsLocale = newLocale;
55
+ };
56
+ var getLocale = exports.getLocale = function getLocale() {
57
+ return window.analyticsLocale;
58
+ };
59
+ var setPlatform = exports.setPlatform = function setPlatform(newPlatform) {
60
+ window.analyticsPlatform = newPlatform;
61
+ };
62
+ var getPlatform = exports.getPlatform = function getPlatform() {
63
+ return window.analyticsPlatform || "web";
64
+ };
53
65
  var getCommonParams = exports.getCommonParams = function getCommonParams() {
54
66
  var persistedParams = getCurrentParams();
55
67
  return _objectSpread(_objectSpread({}, persistedParams), {}, {
@@ -61,55 +73,55 @@ var getCommonParams = exports.getCommonParams = function getCommonParams() {
61
73
  funnel_name: _jsCookie["default"].get("funnel_name"),
62
74
  funnel_type: _jsCookie["default"].get("funnel_type"),
63
75
  language: (0, _helpers.getBrowserLanguage)(),
64
- platform: "web",
76
+ platform: getPlatform(),
65
77
  url: window.location.href,
66
78
  device: (0, _helpers.detectDeviceType)(),
67
79
  browser: (0, _helpers.detectBrowser)(),
68
- system: (0, _helpers.detectOS)()
80
+ system: (0, _helpers.detectOS)(),
81
+ locale: getLocale()
69
82
  });
70
83
  };
71
- var init = exports.init = function init() {
72
- var additionalParams = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
84
+ var init = exports.init = function init(locale, platform) {
73
85
  var userId = getUserId();
74
86
  if ((0, _helpers.isMetaBrowser)()) {
75
87
  (0, _helpers.setQueryParam)(_constants.USER_ID_KEY, userId);
76
88
  }
77
- (0, _helpers.waitForGA)(function () {
78
- _mixpanelBrowser["default"].init(_constants.MIXPANEL_TOKEN, {
79
- debug: false,
80
- track_pageview: "url-with-path",
81
- persistence: "cookie"
82
- });
83
- var userProperties = _objectSpread(_objectSpread({}, getCommonParams()), additionalParams);
84
- _mixpanelBrowser["default"].register(userProperties);
85
- gtag("config", _constants.GOOGLE_ANALYTICS_ID, {
86
- user_properties: userProperties,
87
- server_container_url: _constants.GA_SERVER_CONTAINER_URL
88
- });
89
+ _mixpanelBrowser["default"].init(_constants.MIXPANEL_TOKEN, {
90
+ debug: false,
91
+ track_pageview: "url-with-path",
92
+ persistence: "cookie"
93
+ });
94
+ if (locale) setLocale(locale);
95
+ if (platform) setPlatform(platform);
96
+ var userProperties = getCommonParams();
97
+ _mixpanelBrowser["default"].register(userProperties);
98
+ gtag("config", _constants.GOOGLE_ANALYTICS_ID, {
99
+ user_properties: userProperties,
100
+ server_container_url: _constants.GA_SERVER_CONTAINER_URL
89
101
  });
90
102
  };
91
103
  var trackEvent = exports.trackEvent = function trackEvent(eventName) {
92
104
  var properties = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
93
- (0, _helpers.waitForGA)(function () {
94
- var commonParams = getCommonParams();
95
- var params = _objectSpread(_objectSpread({}, commonParams), properties);
96
- try {
97
- _mixpanelBrowser["default"].track(eventName, params);
98
- } catch (e) {
99
- console.log(e);
100
- }
101
- try {
102
- gtag("event", eventName, params);
103
- } catch (e) {
104
- console.log(e);
105
- }
106
- });
105
+ var commonParams = getCommonParams();
106
+ var params = _objectSpread(_objectSpread({}, commonParams), properties);
107
+ try {
108
+ _mixpanelBrowser["default"].track(eventName, params);
109
+ } catch (e) {
110
+ console.log(e);
111
+ }
112
+ try {
113
+ gtag("event", eventName, params);
114
+ } catch (e) {
115
+ console.log(e);
116
+ }
107
117
  };
108
118
  var setUser = exports.setUser = function setUser(userId, userParams) {
119
+ var email = userParams.email,
120
+ name = userParams.name;
109
121
  _mixpanelBrowser["default"].identify(userId);
110
122
  _mixpanelBrowser["default"].people.set({
111
- $email: userParams.email,
112
- name: userParams.name
123
+ $email: email,
124
+ name: name
113
125
  });
114
126
  gtag("config", _constants.GOOGLE_ANALYTICS_ID, {
115
127
  user_properties: getCommonParams(),