@metamask-previews/analytics-controller 0.0.0-preview-d21b3dc → 0.0.0-preview-a5935709
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 +208 -105
- package/dist/AnalyticsController-method-action-types.cjs.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.cts +28 -26
- package/dist/AnalyticsController-method-action-types.d.cts.map +1 -1
- package/dist/AnalyticsController-method-action-types.d.mts +28 -26
- package/dist/AnalyticsController-method-action-types.d.mts.map +1 -1
- package/dist/AnalyticsController-method-action-types.mjs.map +1 -1
- package/dist/AnalyticsController.cjs +110 -73
- package/dist/AnalyticsController.cjs.map +1 -1
- package/dist/AnalyticsController.d.cts +50 -32
- package/dist/AnalyticsController.d.cts.map +1 -1
- package/dist/AnalyticsController.d.mts +50 -32
- package/dist/AnalyticsController.d.mts.map +1 -1
- package/dist/AnalyticsController.mjs +109 -72
- package/dist/AnalyticsController.mjs.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.cjs.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.d.cts +63 -15
- package/dist/AnalyticsPlatformAdapter.types.d.cts.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.d.mts +63 -15
- package/dist/AnalyticsPlatformAdapter.types.d.mts.map +1 -1
- package/dist/AnalyticsPlatformAdapter.types.mjs.map +1 -1
- package/dist/AnalyticsPlatformAdapterSetupError.cjs +17 -0
- package/dist/AnalyticsPlatformAdapterSetupError.cjs.map +1 -0
- package/dist/AnalyticsPlatformAdapterSetupError.d.cts +8 -0
- package/dist/AnalyticsPlatformAdapterSetupError.d.cts.map +1 -0
- package/dist/AnalyticsPlatformAdapterSetupError.d.mts +8 -0
- package/dist/AnalyticsPlatformAdapterSetupError.d.mts.map +1 -0
- package/dist/AnalyticsPlatformAdapterSetupError.mjs +13 -0
- package/dist/AnalyticsPlatformAdapterSetupError.mjs.map +1 -0
- package/dist/analyticsControllerStateValidator.cjs +34 -0
- package/dist/analyticsControllerStateValidator.cjs.map +1 -0
- package/dist/analyticsControllerStateValidator.d.cts +16 -0
- package/dist/analyticsControllerStateValidator.d.cts.map +1 -0
- package/dist/analyticsControllerStateValidator.d.mts +16 -0
- package/dist/analyticsControllerStateValidator.d.mts.map +1 -0
- package/dist/analyticsControllerStateValidator.mjs +29 -0
- package/dist/analyticsControllerStateValidator.mjs.map +1 -0
- package/dist/index.cjs +9 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +5 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +6 -3
- package/dist/index.mjs.map +1 -1
- package/dist/selectors.cjs +43 -0
- package/dist/selectors.cjs.map +1 -0
- package/dist/selectors.d.cts +12 -0
- package/dist/selectors.d.cts.map +1 -0
- package/dist/selectors.d.mts +12 -0
- package/dist/selectors.d.mts.map +1 -0
- package/dist/selectors.mjs +40 -0
- package/dist/selectors.mjs.map +1 -0
- package/package.json +2 -3
|
@@ -14,8 +14,9 @@ var _AnalyticsController_platformAdapter;
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.AnalyticsController = exports.getDefaultAnalyticsControllerState = exports.controllerName = void 0;
|
|
16
16
|
const base_controller_1 = require("@metamask/base-controller");
|
|
17
|
-
const
|
|
17
|
+
const analyticsControllerStateValidator_1 = require("./analyticsControllerStateValidator.cjs");
|
|
18
18
|
const AnalyticsLogger_1 = require("./AnalyticsLogger.cjs");
|
|
19
|
+
const selectors_1 = require("./selectors.cjs");
|
|
19
20
|
// === GENERAL ===
|
|
20
21
|
/**
|
|
21
22
|
* The name of the {@link AnalyticsController}, used to namespace the
|
|
@@ -23,54 +24,56 @@ const AnalyticsLogger_1 = require("./AnalyticsLogger.cjs");
|
|
|
23
24
|
* when composed with other controllers.
|
|
24
25
|
*/
|
|
25
26
|
exports.controllerName = 'AnalyticsController';
|
|
27
|
+
/**
|
|
28
|
+
* Returns default values for AnalyticsController state.
|
|
29
|
+
*
|
|
30
|
+
* Note: analyticsId is NOT included - it's an identity that must be
|
|
31
|
+
* provided by the platform (generated once on first run, then persisted).
|
|
32
|
+
*
|
|
33
|
+
* @returns Default state without analyticsId
|
|
34
|
+
*/
|
|
35
|
+
function getDefaultAnalyticsControllerState() {
|
|
36
|
+
return {
|
|
37
|
+
optedInForRegularAccount: false,
|
|
38
|
+
optedInForSocialAccount: false,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
exports.getDefaultAnalyticsControllerState = getDefaultAnalyticsControllerState;
|
|
26
42
|
/**
|
|
27
43
|
* The metadata for each property in {@link AnalyticsControllerState}.
|
|
44
|
+
*
|
|
45
|
+
* Note: `persist` is set to `false` for all fields because the platform
|
|
46
|
+
* is responsible for persistence via the `stateChange` event listener.
|
|
28
47
|
*/
|
|
29
48
|
const analyticsControllerMetadata = {
|
|
30
|
-
|
|
49
|
+
optedInForRegularAccount: {
|
|
31
50
|
includeInStateLogs: true,
|
|
32
|
-
persist:
|
|
51
|
+
persist: false,
|
|
33
52
|
includeInDebugSnapshot: true,
|
|
34
53
|
usedInUi: true,
|
|
35
54
|
},
|
|
36
|
-
|
|
55
|
+
optedInForSocialAccount: {
|
|
37
56
|
includeInStateLogs: true,
|
|
38
|
-
persist:
|
|
57
|
+
persist: false,
|
|
39
58
|
includeInDebugSnapshot: true,
|
|
40
59
|
usedInUi: true,
|
|
41
60
|
},
|
|
42
61
|
analyticsId: {
|
|
43
62
|
includeInStateLogs: true,
|
|
44
|
-
persist:
|
|
63
|
+
persist: false,
|
|
45
64
|
includeInDebugSnapshot: true,
|
|
46
65
|
usedInUi: false,
|
|
47
66
|
},
|
|
48
67
|
};
|
|
49
|
-
/**
|
|
50
|
-
* Constructs the default {@link AnalyticsController} state. This allows
|
|
51
|
-
* consumers to provide a partial state object when initializing the controller
|
|
52
|
-
* and also helps in constructing complete state objects for this controller in
|
|
53
|
-
* tests.
|
|
54
|
-
*
|
|
55
|
-
* @returns The default {@link AnalyticsController} state.
|
|
56
|
-
*/
|
|
57
|
-
function getDefaultAnalyticsControllerState() {
|
|
58
|
-
return {
|
|
59
|
-
enabled: true,
|
|
60
|
-
optedIn: false,
|
|
61
|
-
analyticsId: (0, uuid_1.v4)(),
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
exports.getDefaultAnalyticsControllerState = getDefaultAnalyticsControllerState;
|
|
65
68
|
// === MESSENGER ===
|
|
66
69
|
const MESSENGER_EXPOSED_METHODS = [
|
|
67
70
|
'trackEvent',
|
|
68
71
|
'identify',
|
|
69
|
-
'
|
|
70
|
-
'
|
|
71
|
-
'
|
|
72
|
-
'
|
|
73
|
-
'
|
|
72
|
+
'trackView',
|
|
73
|
+
'optInForRegularAccount',
|
|
74
|
+
'optOutForRegularAccount',
|
|
75
|
+
'optInForSocialAccount',
|
|
76
|
+
'optOutForSocialAccount',
|
|
74
77
|
];
|
|
75
78
|
/**
|
|
76
79
|
* The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).
|
|
@@ -81,115 +84,149 @@ const MESSENGER_EXPOSED_METHODS = [
|
|
|
81
84
|
* This controller follows the MetaMask controller pattern and integrates with the
|
|
82
85
|
* messenger system to allow other controllers and components to track analytics events.
|
|
83
86
|
* It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.
|
|
87
|
+
*
|
|
88
|
+
* Note: This controller does not persist state internally (`persist: false` in metadata).
|
|
89
|
+
* The platform is responsible for:
|
|
90
|
+
* - Providing the initial state (including a valid UUIDv4 analyticsId)
|
|
91
|
+
* - Subscribing to `AnalyticsController:stateChange` event to persist changes
|
|
84
92
|
*/
|
|
85
93
|
class AnalyticsController extends base_controller_1.BaseController {
|
|
86
94
|
/**
|
|
87
95
|
* Constructs an AnalyticsController instance.
|
|
88
96
|
*
|
|
89
97
|
* @param options - Controller options
|
|
90
|
-
* @param options.state - Initial controller state
|
|
98
|
+
* @param options.state - Initial controller state. Must include a valid UUIDv4 `analyticsId`.
|
|
99
|
+
* Use `getDefaultAnalyticsControllerState()` for default opt-in preferences.
|
|
91
100
|
* @param options.messenger - Messenger used to communicate with BaseController
|
|
92
101
|
* @param options.platformAdapter - Platform adapter implementation for tracking
|
|
102
|
+
* @throws Error if state.analyticsId is missing or not a valid UUIDv4
|
|
93
103
|
*/
|
|
94
|
-
constructor({ state
|
|
104
|
+
constructor({ state, messenger, platformAdapter, }) {
|
|
105
|
+
const initialState = {
|
|
106
|
+
...getDefaultAnalyticsControllerState(),
|
|
107
|
+
...state,
|
|
108
|
+
};
|
|
109
|
+
(0, analyticsControllerStateValidator_1.validateAnalyticsControllerState)(initialState);
|
|
95
110
|
super({
|
|
96
111
|
name: exports.controllerName,
|
|
97
112
|
metadata: analyticsControllerMetadata,
|
|
98
|
-
state:
|
|
99
|
-
...getDefaultAnalyticsControllerState(),
|
|
100
|
-
...state,
|
|
101
|
-
},
|
|
113
|
+
state: initialState,
|
|
102
114
|
messenger,
|
|
103
115
|
});
|
|
104
116
|
_AnalyticsController_platformAdapter.set(this, void 0);
|
|
105
117
|
__classPrivateFieldSet(this, _AnalyticsController_platformAdapter, platformAdapter, "f");
|
|
106
118
|
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
107
119
|
(0, AnalyticsLogger_1.projectLogger)('AnalyticsController initialized and ready', {
|
|
108
|
-
enabled: this.state
|
|
109
|
-
optedIn: this.state.
|
|
120
|
+
enabled: selectors_1.analyticsControllerSelectors.selectEnabled(this.state),
|
|
121
|
+
optedIn: this.state.optedInForRegularAccount,
|
|
122
|
+
socialOptedIn: this.state.optedInForSocialAccount,
|
|
110
123
|
analyticsId: this.state.analyticsId,
|
|
111
124
|
});
|
|
125
|
+
// Call onSetupCompleted lifecycle hook after initialization
|
|
126
|
+
// State is already validated, so analyticsId is guaranteed to be a valid UUIDv4
|
|
127
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f")
|
|
128
|
+
.onSetupCompleted(this.state.analyticsId)
|
|
129
|
+
.catch((error) => {
|
|
130
|
+
// Log error but don't throw - adapter setup failure shouldn't break controller
|
|
131
|
+
(0, AnalyticsLogger_1.projectLogger)('Error calling platformAdapter.onSetupCompleted', error);
|
|
132
|
+
});
|
|
112
133
|
}
|
|
113
134
|
/**
|
|
114
135
|
* Track an analytics event.
|
|
115
136
|
*
|
|
116
137
|
* Events are only tracked if analytics is enabled.
|
|
117
138
|
*
|
|
118
|
-
* @param
|
|
119
|
-
* @param properties - Event properties
|
|
139
|
+
* @param event - Analytics event with properties and sensitive properties
|
|
120
140
|
*/
|
|
121
|
-
trackEvent(
|
|
141
|
+
trackEvent(event) {
|
|
122
142
|
// Don't track if analytics is disabled
|
|
123
|
-
if (!this.state
|
|
143
|
+
if (!selectors_1.analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
124
144
|
return;
|
|
125
145
|
}
|
|
126
|
-
//
|
|
127
|
-
|
|
146
|
+
// Derive sensitivity from presence of sensitiveProperties
|
|
147
|
+
const hasSensitiveProperties = Object.keys(event.sensitiveProperties).length > 0;
|
|
148
|
+
// if event does not have properties, send event without properties
|
|
149
|
+
// and return to prevent any additional processing
|
|
150
|
+
if (!event.hasProperties) {
|
|
151
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
// Track regular properties (without isSensitive flag - it's the default)
|
|
155
|
+
// Note: Even if properties object is empty, we still send it to ensure
|
|
156
|
+
// an event with user ID is tracked. When only sensitiveProperties exist,
|
|
157
|
+
// this creates two events: one with empty props (user ID) and one with
|
|
158
|
+
// sensitive props (anonymous ID), which is the expected behavior.
|
|
159
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, {
|
|
160
|
+
...event.properties,
|
|
161
|
+
});
|
|
162
|
+
// Track sensitive properties in a separate event with isSensitive flag
|
|
163
|
+
if (hasSensitiveProperties) {
|
|
164
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").track(event.name, {
|
|
165
|
+
...event.properties,
|
|
166
|
+
...event.sensitiveProperties,
|
|
167
|
+
isSensitive: true,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
128
170
|
}
|
|
129
171
|
/**
|
|
130
172
|
* Identify a user for analytics.
|
|
131
173
|
*
|
|
132
|
-
* @param userId - The user identifier (e.g., metametrics ID)
|
|
133
174
|
* @param traits - User traits/properties
|
|
134
175
|
*/
|
|
135
|
-
identify(
|
|
136
|
-
if (!this.state
|
|
176
|
+
identify(traits) {
|
|
177
|
+
if (!selectors_1.analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
137
178
|
return;
|
|
138
179
|
}
|
|
139
|
-
//
|
|
140
|
-
this.
|
|
141
|
-
state.analyticsId = userId;
|
|
142
|
-
});
|
|
143
|
-
// Delegate to platform adapter if supported
|
|
144
|
-
if (__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify) {
|
|
145
|
-
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(userId, traits);
|
|
146
|
-
}
|
|
180
|
+
// Delegate to platform adapter using the current analytics ID
|
|
181
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").identify(this.state.analyticsId, traits);
|
|
147
182
|
}
|
|
148
183
|
/**
|
|
149
|
-
* Track a page view.
|
|
184
|
+
* Track a page or screen view.
|
|
150
185
|
*
|
|
151
|
-
* @param
|
|
152
|
-
* @param properties -
|
|
186
|
+
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
187
|
+
* @param properties - Optional properties associated with the view
|
|
153
188
|
*/
|
|
154
|
-
|
|
155
|
-
if (!this.state
|
|
189
|
+
trackView(name, properties) {
|
|
190
|
+
if (!selectors_1.analyticsControllerSelectors.selectEnabled(this.state)) {
|
|
156
191
|
return;
|
|
157
192
|
}
|
|
158
|
-
// Delegate to platform adapter
|
|
159
|
-
|
|
160
|
-
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").trackPage(pageName, properties);
|
|
161
|
-
}
|
|
193
|
+
// Delegate to platform adapter
|
|
194
|
+
__classPrivateFieldGet(this, _AnalyticsController_platformAdapter, "f").view(name, properties);
|
|
162
195
|
}
|
|
163
196
|
/**
|
|
164
|
-
*
|
|
197
|
+
* Opt in to analytics for regular account.
|
|
198
|
+
* This updates the user's opt-in status for regular account.
|
|
165
199
|
*/
|
|
166
|
-
|
|
200
|
+
optInForRegularAccount() {
|
|
167
201
|
this.update((state) => {
|
|
168
|
-
state.
|
|
202
|
+
state.optedInForRegularAccount = true;
|
|
169
203
|
});
|
|
170
204
|
}
|
|
171
205
|
/**
|
|
172
|
-
*
|
|
206
|
+
* Opt out of analytics for regular account.
|
|
207
|
+
* This updates the user's opt-in status for regular account.
|
|
173
208
|
*/
|
|
174
|
-
|
|
209
|
+
optOutForRegularAccount() {
|
|
175
210
|
this.update((state) => {
|
|
176
|
-
state.
|
|
211
|
+
state.optedInForRegularAccount = false;
|
|
177
212
|
});
|
|
178
213
|
}
|
|
179
214
|
/**
|
|
180
|
-
* Opt in to analytics.
|
|
215
|
+
* Opt in to analytics for social account.
|
|
216
|
+
* This updates the user's opt-in status for social account.
|
|
181
217
|
*/
|
|
182
|
-
|
|
218
|
+
optInForSocialAccount() {
|
|
183
219
|
this.update((state) => {
|
|
184
|
-
state.
|
|
220
|
+
state.optedInForSocialAccount = true;
|
|
185
221
|
});
|
|
186
222
|
}
|
|
187
223
|
/**
|
|
188
|
-
* Opt out of analytics.
|
|
224
|
+
* Opt out of analytics for social account.
|
|
225
|
+
* This updates the user's opt-in status for social account.
|
|
189
226
|
*/
|
|
190
|
-
|
|
227
|
+
optOutForSocialAccount() {
|
|
191
228
|
this.update((state) => {
|
|
192
|
-
state.
|
|
229
|
+
state.optedInForSocialAccount = false;
|
|
193
230
|
});
|
|
194
231
|
}
|
|
195
232
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsController.cjs","s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AAS,EAAE,CAAC;YACpC,uBAAA,IAAI,4CAAiB,CAAC,SAAS,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;QACxD,CAAC;IACH,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;QACvB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,OAAO;QACL,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC;QACxB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC;QACvB,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC;QACxB,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA3ID,kDA2IC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\nimport { v4 as uuidv4 } from 'uuid';\n\nimport type { AnalyticsControllerMethodActions } from './AnalyticsController-method-action-types';\nimport { projectLogger } from './AnalyticsLogger';\nimport type {\n AnalyticsPlatformAdapter,\n AnalyticsEventProperties,\n} from './AnalyticsPlatformAdapter.types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link AnalyticsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'AnalyticsController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link AnalyticsController}.\n */\nexport type AnalyticsControllerState = {\n /**\n * Whether analytics tracking is enabled\n */\n enabled: boolean;\n\n /**\n * Whether the user has opted in to analytics\n */\n optedIn: boolean;\n\n /**\n * User's UUIDv4 analytics identifier\n */\n analyticsId: string;\n};\n\n/**\n * The metadata for each property in {@link AnalyticsControllerState}.\n */\nconst analyticsControllerMetadata = {\n enabled: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n optedIn: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n analyticsId: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<AnalyticsControllerState>;\n\n/**\n * Constructs the default {@link AnalyticsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link AnalyticsController} state.\n */\nexport function getDefaultAnalyticsControllerState(): AnalyticsControllerState {\n return {\n enabled: true,\n optedIn: false,\n analyticsId: uuidv4(),\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'trackEvent',\n 'identify',\n 'trackPage',\n 'enable',\n 'disable',\n 'optIn',\n 'optOut',\n] as const;\n\n/**\n * Returns the state of the {@link AnalyticsController}.\n */\nexport type AnalyticsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Actions that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerActions =\n | AnalyticsControllerGetStateAction\n | AnalyticsControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link AnalyticsControllerMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Event emitted when the state of the {@link AnalyticsController} changes.\n */\nexport type AnalyticsControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Events that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerEvents = AnalyticsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link AnalyticsControllerMessenger} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link AnalyticsController}.\n */\nexport type AnalyticsControllerMessenger = Messenger<\n typeof controllerName,\n AnalyticsControllerActions | AllowedActions,\n AnalyticsControllerEvents | AllowedEvents\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * The options that AnalyticsController takes.\n */\nexport type AnalyticsControllerOptions = {\n state?: Partial<AnalyticsControllerState>;\n messenger: AnalyticsControllerMessenger;\n /**\n * Platform adapter implementation for tracking events\n */\n platformAdapter: AnalyticsPlatformAdapter;\n};\n\n/**\n * The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).\n * It provides a unified interface for tracking events, identifying users, and managing\n * analytics preferences while delegating platform-specific implementation to an\n * {@link AnalyticsPlatformAdapter}.\n *\n * This controller follows the MetaMask controller pattern and integrates with the\n * messenger system to allow other controllers and components to track analytics events.\n * It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.\n */\nexport class AnalyticsController extends BaseController<\n 'AnalyticsController',\n AnalyticsControllerState,\n AnalyticsControllerMessenger\n> {\n readonly #platformAdapter: AnalyticsPlatformAdapter;\n\n /**\n * Constructs an AnalyticsController instance.\n *\n * @param options - Controller options\n * @param options.state - Initial controller state (defaults from getDefaultAnalyticsControllerState)\n * @param options.messenger - Messenger used to communicate with BaseController\n * @param options.platformAdapter - Platform adapter implementation for tracking\n */\n constructor({\n state = {},\n messenger,\n platformAdapter,\n }: AnalyticsControllerOptions) {\n super({\n name: controllerName,\n metadata: analyticsControllerMetadata,\n state: {\n ...getDefaultAnalyticsControllerState(),\n ...state,\n },\n messenger,\n });\n\n this.#platformAdapter = platformAdapter;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n projectLogger('AnalyticsController initialized and ready', {\n enabled: this.state.enabled,\n optedIn: this.state.optedIn,\n analyticsId: this.state.analyticsId,\n });\n }\n\n /**\n * Track an analytics event.\n *\n * Events are only tracked if analytics is enabled.\n *\n * @param eventName - The name of the event\n * @param properties - Event properties\n */\n trackEvent(\n eventName: string,\n properties: AnalyticsEventProperties = {},\n ): void {\n // Don't track if analytics is disabled\n if (!this.state.enabled) {\n return;\n }\n\n // Delegate to platform adapter\n this.#platformAdapter.trackEvent(eventName, properties);\n }\n\n /**\n * Identify a user for analytics.\n *\n * @param userId - The user identifier (e.g., metametrics ID)\n * @param traits - User traits/properties\n */\n identify(userId: string, traits?: AnalyticsEventProperties): void {\n if (!this.state.enabled) {\n return;\n }\n\n // Update state with analytics ID\n this.update((state) => {\n state.analyticsId = userId;\n });\n\n // Delegate to platform adapter if supported\n if (this.#platformAdapter.identify) {\n this.#platformAdapter.identify(userId, traits);\n }\n }\n\n /**\n * Track a page view.\n *\n * @param pageName - The name of the page\n * @param properties - Page properties\n */\n trackPage(pageName: string, properties?: AnalyticsEventProperties): void {\n if (!this.state.enabled) {\n return;\n }\n\n // Delegate to platform adapter if supported\n if (this.#platformAdapter.trackPage) {\n this.#platformAdapter.trackPage(pageName, properties);\n }\n }\n\n /**\n * Enable analytics tracking.\n */\n enable(): void {\n this.update((state) => {\n state.enabled = true;\n });\n }\n\n /**\n * Disable analytics tracking.\n */\n disable(): void {\n this.update((state) => {\n state.enabled = false;\n });\n }\n\n /**\n * Opt in to analytics.\n */\n optIn(): void {\n this.update((state) => {\n state.optedIn = true;\n });\n }\n\n /**\n * Opt out of analytics.\n */\n optOut(): void {\n this.update((state) => {\n state.optedIn = false;\n });\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"AnalyticsController.cjs","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,+DAA2D;AAI3D,+FAAuF;AACvF,2DAAkD;AAOlD,+CAA2D;AAE3D,kBAAkB;AAElB;;;;GAIG;AACU,QAA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QACvC,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;OAGG;IACH,sBAAsB;QACpB,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,uBAAuB,GAAG,KAAK,CAAC;QACxC,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA3KD,kDA2KC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\n\nimport type { AnalyticsControllerMethodActions } from './AnalyticsController-method-action-types';\nimport { validateAnalyticsControllerState } from './analyticsControllerStateValidator';\nimport { projectLogger } from './AnalyticsLogger';\nimport type {\n AnalyticsPlatformAdapter,\n AnalyticsEventProperties,\n AnalyticsUserTraits,\n AnalyticsTrackingEvent,\n} from './AnalyticsPlatformAdapter.types';\nimport { analyticsControllerSelectors } from './selectors';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link AnalyticsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'AnalyticsController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link AnalyticsController}.\n */\nexport type AnalyticsControllerState = {\n /**\n * Whether the user has opted in to analytics for regular account.\n */\n optedInForRegularAccount: boolean;\n\n /**\n * Whether the user has opted in to analytics for social account.\n */\n optedInForSocialAccount: boolean;\n\n /**\n * User's UUIDv4 analytics identifier.\n * This is an identity (unique per user), not a preference.\n * Must be provided by the platform - the controller does not generate it.\n */\n analyticsId: string;\n};\n\n/**\n * Returns default values for AnalyticsController state.\n *\n * Note: analyticsId is NOT included - it's an identity that must be\n * provided by the platform (generated once on first run, then persisted).\n *\n * @returns Default state without analyticsId\n */\nexport function getDefaultAnalyticsControllerState(): Omit<\n AnalyticsControllerState,\n 'analyticsId'\n> {\n return {\n optedInForRegularAccount: false,\n optedInForSocialAccount: false,\n };\n}\n\n/**\n * The metadata for each property in {@link AnalyticsControllerState}.\n *\n * Note: `persist` is set to `false` for all fields because the platform\n * is responsible for persistence via the `stateChange` event listener.\n */\nconst analyticsControllerMetadata = {\n optedInForRegularAccount: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n optedInForSocialAccount: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n analyticsId: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<AnalyticsControllerState>;\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'trackEvent',\n 'identify',\n 'trackView',\n 'optInForRegularAccount',\n 'optOutForRegularAccount',\n 'optInForSocialAccount',\n 'optOutForSocialAccount',\n] as const;\n\n/**\n * Returns the state of the {@link AnalyticsController}.\n */\nexport type AnalyticsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Actions that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerActions =\n | AnalyticsControllerGetStateAction\n | AnalyticsControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link AnalyticsControllerMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Event emitted when the state of the {@link AnalyticsController} changes.\n */\nexport type AnalyticsControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AnalyticsControllerState\n>;\n\n/**\n * Events that {@link AnalyticsControllerMessenger} exposes to other consumers.\n */\nexport type AnalyticsControllerEvents = AnalyticsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link AnalyticsControllerMessenger} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link AnalyticsController}.\n */\nexport type AnalyticsControllerMessenger = Messenger<\n typeof controllerName,\n AnalyticsControllerActions | AllowedActions,\n AnalyticsControllerEvents | AllowedEvents\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * The options that AnalyticsController takes.\n */\nexport type AnalyticsControllerOptions = {\n /**\n * Initial controller state. Must include a valid UUIDv4 `analyticsId`.\n * The platform is responsible for generating and persisting the analyticsId.\n */\n state: AnalyticsControllerState;\n /**\n * Messenger used to communicate with BaseController and other controllers.\n */\n messenger: AnalyticsControllerMessenger;\n /**\n * Platform adapter implementation for tracking events.\n */\n platformAdapter: AnalyticsPlatformAdapter;\n};\n\n/**\n * The AnalyticsController manages analytics tracking across platforms (Mobile/Extension).\n * It provides a unified interface for tracking events, identifying users, and managing\n * analytics preferences while delegating platform-specific implementation to an\n * {@link AnalyticsPlatformAdapter}.\n *\n * This controller follows the MetaMask controller pattern and integrates with the\n * messenger system to allow other controllers and components to track analytics events.\n * It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.\n *\n * Note: This controller does not persist state internally (`persist: false` in metadata).\n * The platform is responsible for:\n * - Providing the initial state (including a valid UUIDv4 analyticsId)\n * - Subscribing to `AnalyticsController:stateChange` event to persist changes\n */\nexport class AnalyticsController extends BaseController<\n 'AnalyticsController',\n AnalyticsControllerState,\n AnalyticsControllerMessenger\n> {\n readonly #platformAdapter: AnalyticsPlatformAdapter;\n\n /**\n * Constructs an AnalyticsController instance.\n *\n * @param options - Controller options\n * @param options.state - Initial controller state. Must include a valid UUIDv4 `analyticsId`.\n * Use `getDefaultAnalyticsControllerState()` for default opt-in preferences.\n * @param options.messenger - Messenger used to communicate with BaseController\n * @param options.platformAdapter - Platform adapter implementation for tracking\n * @throws Error if state.analyticsId is missing or not a valid UUIDv4\n */\n constructor({\n state,\n messenger,\n platformAdapter,\n }: AnalyticsControllerOptions) {\n const initialState: AnalyticsControllerState = {\n ...getDefaultAnalyticsControllerState(),\n ...state,\n };\n\n validateAnalyticsControllerState(initialState);\n\n super({\n name: controllerName,\n metadata: analyticsControllerMetadata,\n state: initialState,\n messenger,\n });\n\n this.#platformAdapter = platformAdapter;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n projectLogger('AnalyticsController initialized and ready', {\n enabled: analyticsControllerSelectors.selectEnabled(this.state),\n optedIn: this.state.optedInForRegularAccount,\n socialOptedIn: this.state.optedInForSocialAccount,\n analyticsId: this.state.analyticsId,\n });\n\n // Call onSetupCompleted lifecycle hook after initialization\n // State is already validated, so analyticsId is guaranteed to be a valid UUIDv4\n this.#platformAdapter\n .onSetupCompleted(this.state.analyticsId)\n .catch((error) => {\n // Log error but don't throw - adapter setup failure shouldn't break controller\n projectLogger('Error calling platformAdapter.onSetupCompleted', error);\n });\n }\n\n /**\n * Track an analytics event.\n *\n * Events are only tracked if analytics is enabled.\n *\n * @param event - Analytics event with properties and sensitive properties\n */\n trackEvent(event: AnalyticsTrackingEvent): void {\n // Don't track if analytics is disabled\n if (!analyticsControllerSelectors.selectEnabled(this.state)) {\n return;\n }\n\n // Derive sensitivity from presence of sensitiveProperties\n const hasSensitiveProperties =\n Object.keys(event.sensitiveProperties).length > 0;\n\n // if event does not have properties, send event without properties\n // and return to prevent any additional processing\n if (!event.hasProperties) {\n this.#platformAdapter.track(event.name);\n return;\n }\n\n // Track regular properties (without isSensitive flag - it's the default)\n // Note: Even if properties object is empty, we still send it to ensure\n // an event with user ID is tracked. When only sensitiveProperties exist,\n // this creates two events: one with empty props (user ID) and one with\n // sensitive props (anonymous ID), which is the expected behavior.\n this.#platformAdapter.track(event.name, {\n ...event.properties,\n });\n\n // Track sensitive properties in a separate event with isSensitive flag\n if (hasSensitiveProperties) {\n this.#platformAdapter.track(event.name, {\n ...event.properties,\n ...event.sensitiveProperties,\n isSensitive: true,\n });\n }\n }\n\n /**\n * Identify a user for analytics.\n *\n * @param traits - User traits/properties\n */\n identify(traits?: AnalyticsUserTraits): void {\n if (!analyticsControllerSelectors.selectEnabled(this.state)) {\n return;\n }\n\n // Delegate to platform adapter using the current analytics ID\n this.#platformAdapter.identify(this.state.analyticsId, traits);\n }\n\n /**\n * Track a page or screen view.\n *\n * @param name - The identifier/name of the page or screen being viewed (e.g., \"home\", \"settings\", \"wallet\")\n * @param properties - Optional properties associated with the view\n */\n trackView(name: string, properties?: AnalyticsEventProperties): void {\n if (!analyticsControllerSelectors.selectEnabled(this.state)) {\n return;\n }\n\n // Delegate to platform adapter\n this.#platformAdapter.view(name, properties);\n }\n\n /**\n * Opt in to analytics for regular account.\n * This updates the user's opt-in status for regular account.\n */\n optInForRegularAccount(): void {\n this.update((state) => {\n state.optedInForRegularAccount = true;\n });\n }\n\n /**\n * Opt out of analytics for regular account.\n * This updates the user's opt-in status for regular account.\n */\n optOutForRegularAccount(): void {\n this.update((state) => {\n state.optedInForRegularAccount = false;\n });\n }\n\n /**\n * Opt in to analytics for social account.\n * This updates the user's opt-in status for social account.\n */\n optInForSocialAccount(): void {\n this.update((state) => {\n state.optedInForSocialAccount = true;\n });\n }\n\n /**\n * Opt out of analytics for social account.\n * This updates the user's opt-in status for social account.\n */\n optOutForSocialAccount(): void {\n this.update((state) => {\n state.optedInForSocialAccount = false;\n });\n }\n}\n"]}
|
|
@@ -2,7 +2,7 @@ import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@meta
|
|
|
2
2
|
import { BaseController } from "@metamask/base-controller";
|
|
3
3
|
import type { Messenger } from "@metamask/messenger";
|
|
4
4
|
import type { AnalyticsControllerMethodActions } from "./AnalyticsController-method-action-types.cjs";
|
|
5
|
-
import type { AnalyticsPlatformAdapter, AnalyticsEventProperties } from "./AnalyticsPlatformAdapter.types.cjs";
|
|
5
|
+
import type { AnalyticsPlatformAdapter, AnalyticsEventProperties, AnalyticsUserTraits, AnalyticsTrackingEvent } from "./AnalyticsPlatformAdapter.types.cjs";
|
|
6
6
|
/**
|
|
7
7
|
* The name of the {@link AnalyticsController}, used to namespace the
|
|
8
8
|
* controller's actions and events and to namespace the controller's state data
|
|
@@ -14,27 +14,29 @@ export declare const controllerName = "AnalyticsController";
|
|
|
14
14
|
*/
|
|
15
15
|
export type AnalyticsControllerState = {
|
|
16
16
|
/**
|
|
17
|
-
* Whether analytics
|
|
17
|
+
* Whether the user has opted in to analytics for regular account.
|
|
18
18
|
*/
|
|
19
|
-
|
|
19
|
+
optedInForRegularAccount: boolean;
|
|
20
20
|
/**
|
|
21
|
-
* Whether the user has opted in to analytics
|
|
21
|
+
* Whether the user has opted in to analytics for social account.
|
|
22
22
|
*/
|
|
23
|
-
|
|
23
|
+
optedInForSocialAccount: boolean;
|
|
24
24
|
/**
|
|
25
|
-
* User's UUIDv4 analytics identifier
|
|
25
|
+
* User's UUIDv4 analytics identifier.
|
|
26
|
+
* This is an identity (unique per user), not a preference.
|
|
27
|
+
* Must be provided by the platform - the controller does not generate it.
|
|
26
28
|
*/
|
|
27
29
|
analyticsId: string;
|
|
28
30
|
};
|
|
29
31
|
/**
|
|
30
|
-
*
|
|
31
|
-
* consumers to provide a partial state object when initializing the controller
|
|
32
|
-
* and also helps in constructing complete state objects for this controller in
|
|
33
|
-
* tests.
|
|
32
|
+
* Returns default values for AnalyticsController state.
|
|
34
33
|
*
|
|
35
|
-
*
|
|
34
|
+
* Note: analyticsId is NOT included - it's an identity that must be
|
|
35
|
+
* provided by the platform (generated once on first run, then persisted).
|
|
36
|
+
*
|
|
37
|
+
* @returns Default state without analyticsId
|
|
36
38
|
*/
|
|
37
|
-
export declare function getDefaultAnalyticsControllerState(): AnalyticsControllerState
|
|
39
|
+
export declare function getDefaultAnalyticsControllerState(): Omit<AnalyticsControllerState, 'analyticsId'>;
|
|
38
40
|
/**
|
|
39
41
|
* Returns the state of the {@link AnalyticsController}.
|
|
40
42
|
*/
|
|
@@ -68,10 +70,17 @@ export type AnalyticsControllerMessenger = Messenger<typeof controllerName, Anal
|
|
|
68
70
|
* The options that AnalyticsController takes.
|
|
69
71
|
*/
|
|
70
72
|
export type AnalyticsControllerOptions = {
|
|
71
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Initial controller state. Must include a valid UUIDv4 `analyticsId`.
|
|
75
|
+
* The platform is responsible for generating and persisting the analyticsId.
|
|
76
|
+
*/
|
|
77
|
+
state: AnalyticsControllerState;
|
|
78
|
+
/**
|
|
79
|
+
* Messenger used to communicate with BaseController and other controllers.
|
|
80
|
+
*/
|
|
72
81
|
messenger: AnalyticsControllerMessenger;
|
|
73
82
|
/**
|
|
74
|
-
* Platform adapter implementation for tracking events
|
|
83
|
+
* Platform adapter implementation for tracking events.
|
|
75
84
|
*/
|
|
76
85
|
platformAdapter: AnalyticsPlatformAdapter;
|
|
77
86
|
};
|
|
@@ -84,6 +93,11 @@ export type AnalyticsControllerOptions = {
|
|
|
84
93
|
* This controller follows the MetaMask controller pattern and integrates with the
|
|
85
94
|
* messenger system to allow other controllers and components to track analytics events.
|
|
86
95
|
* It delegates platform-specific implementation to an {@link AnalyticsPlatformAdapter}.
|
|
96
|
+
*
|
|
97
|
+
* Note: This controller does not persist state internally (`persist: false` in metadata).
|
|
98
|
+
* The platform is responsible for:
|
|
99
|
+
* - Providing the initial state (including a valid UUIDv4 analyticsId)
|
|
100
|
+
* - Subscribing to `AnalyticsController:stateChange` event to persist changes
|
|
87
101
|
*/
|
|
88
102
|
export declare class AnalyticsController extends BaseController<'AnalyticsController', AnalyticsControllerState, AnalyticsControllerMessenger> {
|
|
89
103
|
#private;
|
|
@@ -91,9 +105,11 @@ export declare class AnalyticsController extends BaseController<'AnalyticsContro
|
|
|
91
105
|
* Constructs an AnalyticsController instance.
|
|
92
106
|
*
|
|
93
107
|
* @param options - Controller options
|
|
94
|
-
* @param options.state - Initial controller state
|
|
108
|
+
* @param options.state - Initial controller state. Must include a valid UUIDv4 `analyticsId`.
|
|
109
|
+
* Use `getDefaultAnalyticsControllerState()` for default opt-in preferences.
|
|
95
110
|
* @param options.messenger - Messenger used to communicate with BaseController
|
|
96
111
|
* @param options.platformAdapter - Platform adapter implementation for tracking
|
|
112
|
+
* @throws Error if state.analyticsId is missing or not a valid UUIDv4
|
|
97
113
|
*/
|
|
98
114
|
constructor({ state, messenger, platformAdapter, }: AnalyticsControllerOptions);
|
|
99
115
|
/**
|
|
@@ -101,40 +117,42 @@ export declare class AnalyticsController extends BaseController<'AnalyticsContro
|
|
|
101
117
|
*
|
|
102
118
|
* Events are only tracked if analytics is enabled.
|
|
103
119
|
*
|
|
104
|
-
* @param
|
|
105
|
-
* @param properties - Event properties
|
|
120
|
+
* @param event - Analytics event with properties and sensitive properties
|
|
106
121
|
*/
|
|
107
|
-
trackEvent(
|
|
122
|
+
trackEvent(event: AnalyticsTrackingEvent): void;
|
|
108
123
|
/**
|
|
109
124
|
* Identify a user for analytics.
|
|
110
125
|
*
|
|
111
|
-
* @param userId - The user identifier (e.g., metametrics ID)
|
|
112
126
|
* @param traits - User traits/properties
|
|
113
127
|
*/
|
|
114
|
-
identify(
|
|
128
|
+
identify(traits?: AnalyticsUserTraits): void;
|
|
115
129
|
/**
|
|
116
|
-
* Track a page view.
|
|
130
|
+
* Track a page or screen view.
|
|
117
131
|
*
|
|
118
|
-
* @param
|
|
119
|
-
* @param properties -
|
|
132
|
+
* @param name - The identifier/name of the page or screen being viewed (e.g., "home", "settings", "wallet")
|
|
133
|
+
* @param properties - Optional properties associated with the view
|
|
120
134
|
*/
|
|
121
|
-
|
|
135
|
+
trackView(name: string, properties?: AnalyticsEventProperties): void;
|
|
122
136
|
/**
|
|
123
|
-
*
|
|
137
|
+
* Opt in to analytics for regular account.
|
|
138
|
+
* This updates the user's opt-in status for regular account.
|
|
124
139
|
*/
|
|
125
|
-
|
|
140
|
+
optInForRegularAccount(): void;
|
|
126
141
|
/**
|
|
127
|
-
*
|
|
142
|
+
* Opt out of analytics for regular account.
|
|
143
|
+
* This updates the user's opt-in status for regular account.
|
|
128
144
|
*/
|
|
129
|
-
|
|
145
|
+
optOutForRegularAccount(): void;
|
|
130
146
|
/**
|
|
131
|
-
* Opt in to analytics.
|
|
147
|
+
* Opt in to analytics for social account.
|
|
148
|
+
* This updates the user's opt-in status for social account.
|
|
132
149
|
*/
|
|
133
|
-
|
|
150
|
+
optInForSocialAccount(): void;
|
|
134
151
|
/**
|
|
135
|
-
* Opt out of analytics.
|
|
152
|
+
* Opt out of analytics for social account.
|
|
153
|
+
* This updates the user's opt-in status for social account.
|
|
136
154
|
*/
|
|
137
|
-
|
|
155
|
+
optOutForSocialAccount(): void;
|
|
138
156
|
}
|
|
139
157
|
export {};
|
|
140
158
|
//# sourceMappingURL=AnalyticsController.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsController.d.cts","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;
|
|
1
|
+
{"version":3,"file":"AnalyticsController.d.cts","sourceRoot":"","sources":["../src/AnalyticsController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,gCAAgC,EAAE,sDAAkD;AAGlG,OAAO,KAAK,EACV,wBAAwB,EACxB,wBAAwB,EACxB,mBAAmB,EACnB,sBAAsB,EACvB,6CAAyC;AAK1C;;;;GAIG;AACH,eAAO,MAAM,cAAc,wBAAwB,CAAC;AAIpD;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC;;OAEG;IACH,wBAAwB,EAAE,OAAO,CAAC;IAElC;;OAEG;IACH,uBAAuB,EAAE,OAAO,CAAC;IAEjC;;;;OAIG;IACH,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,kCAAkC,IAAI,IAAI,CACxD,wBAAwB,EACxB,aAAa,CACd,CAKA;AAyCD;;G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