mytart 0.1.7 → 0.2.1

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/dist/index.d.mts CHANGED
@@ -9,12 +9,14 @@ interface BaseProviderConfig {
9
9
  enabled?: boolean;
10
10
  }
11
11
  type ProviderName = 'google-analytics' | 'mixpanel' | 'segment' | 'amplitude' | 'plausible' | 'posthog';
12
+ type GoogleAnalyticsAppType = 'browser' | 'server';
12
13
  interface GoogleAnalyticsConfig extends BaseProviderConfig {
13
14
  provider: 'google-analytics';
14
15
  measurementId: string;
15
- apiSecret: string;
16
+ apiSecret?: string;
16
17
  clientId?: string;
17
18
  debug?: boolean;
19
+ appType?: GoogleAnalyticsAppType;
18
20
  }
19
21
  interface MixpanelConfig extends BaseProviderConfig {
20
22
  provider: 'mixpanel';
@@ -118,12 +120,40 @@ declare abstract class BaseProvider {
118
120
  protected buildSuccess(statusCode?: number): TrackResult;
119
121
  }
120
122
 
123
+ declare global {
124
+ interface Window {
125
+ gtag: GtagFn;
126
+ dataLayer: unknown[];
127
+ }
128
+ }
129
+ interface GtagFn {
130
+ (command: 'config', measurementId: string, config?: GtagConfig): void;
131
+ (command: 'event', eventName: string, params?: Record<string, unknown>): void;
132
+ (command: 'set', config: Record<string, unknown>): void;
133
+ (command: string, ...args: unknown[]): void;
134
+ }
135
+ interface GtagConfig {
136
+ send_page_view?: boolean;
137
+ page_title?: string;
138
+ page_location?: string;
139
+ page_referrer?: string;
140
+ user_id?: string;
141
+ [key: string]: unknown;
142
+ }
121
143
  declare class GoogleAnalyticsProvider extends BaseProvider {
122
144
  readonly name = "google-analytics";
123
145
  private readonly config;
124
146
  private readonly http;
125
147
  private readonly endpoint;
148
+ private readonly isBrowser;
149
+ private gtagInitialized;
126
150
  constructor(config: GoogleAnalyticsConfig);
151
+ private ensureGtagLoaded;
152
+ private injectGtagScript;
153
+ private buildGtagResult;
154
+ private trackBrowser;
155
+ private identifyBrowser;
156
+ private pageBrowser;
127
157
  track({ event, properties, userId, anonymousId, timestamp }: TrackOptions): Promise<TrackResult>;
128
158
  identify({ userId, traits }: IdentifyOptions): Promise<TrackResult>;
129
159
  page({ name, url, referrer, userId, anonymousId }: PageOptions): Promise<TrackResult>;
@@ -186,4 +216,4 @@ declare class PostHogProvider extends BaseProvider {
186
216
  page({ name, url, userId, anonymousId, properties }: PageOptions): Promise<TrackResult>;
187
217
  }
188
218
 
189
- export { type AmplitudeConfig, AmplitudeProvider, BaseProvider, type BaseProviderConfig, type EventContext, type GoogleAnalyticsConfig, GoogleAnalyticsProvider, type IdentifyOptions, type MixpanelConfig, MixpanelProvider, Mytart, type MytartConfig, type MytartError, type PageOptions, type PlausibleConfig, PlausibleProvider, type PostHogConfig, PostHogProvider, type ProviderConfig, type ProviderName, type SegmentConfig, SegmentProvider, type TrackOptions, type TrackResult };
219
+ export { type AmplitudeConfig, AmplitudeProvider, BaseProvider, type BaseProviderConfig, type EventContext, type GoogleAnalyticsAppType, type GoogleAnalyticsConfig, GoogleAnalyticsProvider, type IdentifyOptions, type MixpanelConfig, MixpanelProvider, Mytart, type MytartConfig, type MytartError, type PageOptions, type PlausibleConfig, PlausibleProvider, type PostHogConfig, PostHogProvider, type ProviderConfig, type ProviderName, type SegmentConfig, SegmentProvider, type TrackOptions, type TrackResult };
package/dist/index.d.ts CHANGED
@@ -9,12 +9,14 @@ interface BaseProviderConfig {
9
9
  enabled?: boolean;
10
10
  }
11
11
  type ProviderName = 'google-analytics' | 'mixpanel' | 'segment' | 'amplitude' | 'plausible' | 'posthog';
12
+ type GoogleAnalyticsAppType = 'browser' | 'server';
12
13
  interface GoogleAnalyticsConfig extends BaseProviderConfig {
13
14
  provider: 'google-analytics';
14
15
  measurementId: string;
15
- apiSecret: string;
16
+ apiSecret?: string;
16
17
  clientId?: string;
17
18
  debug?: boolean;
19
+ appType?: GoogleAnalyticsAppType;
18
20
  }
19
21
  interface MixpanelConfig extends BaseProviderConfig {
20
22
  provider: 'mixpanel';
@@ -118,12 +120,40 @@ declare abstract class BaseProvider {
118
120
  protected buildSuccess(statusCode?: number): TrackResult;
119
121
  }
120
122
 
123
+ declare global {
124
+ interface Window {
125
+ gtag: GtagFn;
126
+ dataLayer: unknown[];
127
+ }
128
+ }
129
+ interface GtagFn {
130
+ (command: 'config', measurementId: string, config?: GtagConfig): void;
131
+ (command: 'event', eventName: string, params?: Record<string, unknown>): void;
132
+ (command: 'set', config: Record<string, unknown>): void;
133
+ (command: string, ...args: unknown[]): void;
134
+ }
135
+ interface GtagConfig {
136
+ send_page_view?: boolean;
137
+ page_title?: string;
138
+ page_location?: string;
139
+ page_referrer?: string;
140
+ user_id?: string;
141
+ [key: string]: unknown;
142
+ }
121
143
  declare class GoogleAnalyticsProvider extends BaseProvider {
122
144
  readonly name = "google-analytics";
123
145
  private readonly config;
124
146
  private readonly http;
125
147
  private readonly endpoint;
148
+ private readonly isBrowser;
149
+ private gtagInitialized;
126
150
  constructor(config: GoogleAnalyticsConfig);
151
+ private ensureGtagLoaded;
152
+ private injectGtagScript;
153
+ private buildGtagResult;
154
+ private trackBrowser;
155
+ private identifyBrowser;
156
+ private pageBrowser;
127
157
  track({ event, properties, userId, anonymousId, timestamp }: TrackOptions): Promise<TrackResult>;
128
158
  identify({ userId, traits }: IdentifyOptions): Promise<TrackResult>;
129
159
  page({ name, url, referrer, userId, anonymousId }: PageOptions): Promise<TrackResult>;
@@ -186,4 +216,4 @@ declare class PostHogProvider extends BaseProvider {
186
216
  page({ name, url, userId, anonymousId, properties }: PageOptions): Promise<TrackResult>;
187
217
  }
188
218
 
189
- export { type AmplitudeConfig, AmplitudeProvider, BaseProvider, type BaseProviderConfig, type EventContext, type GoogleAnalyticsConfig, GoogleAnalyticsProvider, type IdentifyOptions, type MixpanelConfig, MixpanelProvider, Mytart, type MytartConfig, type MytartError, type PageOptions, type PlausibleConfig, PlausibleProvider, type PostHogConfig, PostHogProvider, type ProviderConfig, type ProviderName, type SegmentConfig, SegmentProvider, type TrackOptions, type TrackResult };
219
+ export { type AmplitudeConfig, AmplitudeProvider, BaseProvider, type BaseProviderConfig, type EventContext, type GoogleAnalyticsAppType, type GoogleAnalyticsConfig, GoogleAnalyticsProvider, type IdentifyOptions, type MixpanelConfig, MixpanelProvider, Mytart, type MytartConfig, type MytartError, type PageOptions, type PlausibleConfig, PlausibleProvider, type PostHogConfig, PostHogProvider, type ProviderConfig, type ProviderName, type SegmentConfig, SegmentProvider, type TrackOptions, type TrackResult };
package/dist/index.js CHANGED
@@ -82,23 +82,105 @@ function isAxiosError(error) {
82
82
  // src/providers/google-analytics.ts
83
83
  var GA4_ENDPOINT = "https://www.google-analytics.com/mp/collect";
84
84
  var GA4_DEBUG_ENDPOINT = "https://www.google-analytics.com/debug/mp/collect";
85
+ var GTAG_URL = "https://www.googletagmanager.com/gtag/js";
85
86
  var GoogleAnalyticsProvider = class extends BaseProvider {
86
87
  constructor(config) {
87
88
  super();
88
89
  this.name = "google-analytics";
90
+ this.gtagInitialized = false;
89
91
  this.config = config;
90
92
  this.http = createHttpClient();
91
93
  this.endpoint = config.debug ? GA4_DEBUG_ENDPOINT : GA4_ENDPOINT;
94
+ this.isBrowser = config.appType === "browser";
95
+ }
96
+ async ensureGtagLoaded() {
97
+ if (typeof window === "undefined") {
98
+ return;
99
+ }
100
+ if (typeof window.gtag === "function") {
101
+ return;
102
+ }
103
+ await this.injectGtagScript();
104
+ }
105
+ injectGtagScript() {
106
+ return new Promise((resolve) => {
107
+ const script = document.createElement("script");
108
+ script.async = true;
109
+ script.src = `${GTAG_URL}?id=${this.config.measurementId}`;
110
+ const firstScript = document.getElementsByTagName("script")[0];
111
+ firstScript.parentNode?.insertBefore(script, firstScript);
112
+ window.dataLayer = window.dataLayer || [];
113
+ window.gtag = function gtag(...args) {
114
+ window.dataLayer.push(args);
115
+ };
116
+ window.gtag("js", /* @__PURE__ */ new Date());
117
+ window.gtag("config", this.config.measurementId, {
118
+ send_page_view: false
119
+ });
120
+ script.onload = () => resolve();
121
+ script.onerror = () => resolve();
122
+ });
123
+ }
124
+ buildGtagResult() {
125
+ return {
126
+ provider: this.name,
127
+ success: true,
128
+ statusCode: 200
129
+ };
130
+ }
131
+ async trackBrowser({ event, properties, userId }) {
132
+ if (typeof window === "undefined") {
133
+ return this.buildGtagResult();
134
+ }
135
+ await this.ensureGtagLoaded();
136
+ if (userId) {
137
+ window.gtag("set", { user_id: userId });
138
+ }
139
+ window.gtag("event", event, properties || {});
140
+ return this.buildGtagResult();
141
+ }
142
+ async identifyBrowser({ userId, traits }) {
143
+ if (typeof window === "undefined") {
144
+ return this.buildGtagResult();
145
+ }
146
+ await this.ensureGtagLoaded();
147
+ window.gtag("set", { user_id: userId });
148
+ if (traits) {
149
+ Object.entries(traits).forEach(([key, value]) => {
150
+ window.gtag("set", { [key]: value });
151
+ });
152
+ }
153
+ return this.buildGtagResult();
154
+ }
155
+ async pageBrowser({ name, url, referrer, userId }) {
156
+ if (typeof window === "undefined") {
157
+ return this.buildGtagResult();
158
+ }
159
+ await this.ensureGtagLoaded();
160
+ const config = {
161
+ page_title: name,
162
+ page_location: url,
163
+ page_referrer: referrer,
164
+ send_page_view: true
165
+ };
166
+ if (userId) {
167
+ config.user_id = userId;
168
+ }
169
+ window.gtag("config", this.config.measurementId, config);
170
+ return this.buildGtagResult();
92
171
  }
93
172
  async track({ event, properties, userId, anonymousId, timestamp }) {
173
+ if (this.isBrowser) {
174
+ return this.trackBrowser({ event, properties, userId, anonymousId, timestamp });
175
+ }
94
176
  try {
95
- const clientId = anonymousId ?? this.config.clientId ?? "anonymous";
177
+ const client_id = anonymousId ?? this.config.clientId ?? "anonymous";
96
178
  const params = { ...properties };
97
179
  if (timestamp) {
98
180
  params["timestamp_micros"] = timestamp.getTime() * 1e3;
99
181
  }
100
182
  const body = {
101
- client_id: clientId,
183
+ client_id,
102
184
  events: [{ name: event, params }]
103
185
  };
104
186
  if (userId) {
@@ -123,6 +205,9 @@ var GoogleAnalyticsProvider = class extends BaseProvider {
123
205
  }
124
206
  }
125
207
  async identify({ userId, traits }) {
208
+ if (this.isBrowser) {
209
+ return this.identifyBrowser({ userId, traits });
210
+ }
126
211
  try {
127
212
  const body = {
128
213
  client_id: userId,
@@ -151,6 +236,9 @@ var GoogleAnalyticsProvider = class extends BaseProvider {
151
236
  }
152
237
  }
153
238
  async page({ name, url, referrer, userId, anonymousId }) {
239
+ if (this.isBrowser) {
240
+ return this.pageBrowser({ name, url, referrer, userId, anonymousId });
241
+ }
154
242
  return this.track({
155
243
  event: "page_view",
156
244
  properties: {
package/dist/index.mjs CHANGED
@@ -39,23 +39,105 @@ function isAxiosError(error) {
39
39
  // src/providers/google-analytics.ts
40
40
  var GA4_ENDPOINT = "https://www.google-analytics.com/mp/collect";
41
41
  var GA4_DEBUG_ENDPOINT = "https://www.google-analytics.com/debug/mp/collect";
42
+ var GTAG_URL = "https://www.googletagmanager.com/gtag/js";
42
43
  var GoogleAnalyticsProvider = class extends BaseProvider {
43
44
  constructor(config) {
44
45
  super();
45
46
  this.name = "google-analytics";
47
+ this.gtagInitialized = false;
46
48
  this.config = config;
47
49
  this.http = createHttpClient();
48
50
  this.endpoint = config.debug ? GA4_DEBUG_ENDPOINT : GA4_ENDPOINT;
51
+ this.isBrowser = config.appType === "browser";
52
+ }
53
+ async ensureGtagLoaded() {
54
+ if (typeof window === "undefined") {
55
+ return;
56
+ }
57
+ if (typeof window.gtag === "function") {
58
+ return;
59
+ }
60
+ await this.injectGtagScript();
61
+ }
62
+ injectGtagScript() {
63
+ return new Promise((resolve) => {
64
+ const script = document.createElement("script");
65
+ script.async = true;
66
+ script.src = `${GTAG_URL}?id=${this.config.measurementId}`;
67
+ const firstScript = document.getElementsByTagName("script")[0];
68
+ firstScript.parentNode?.insertBefore(script, firstScript);
69
+ window.dataLayer = window.dataLayer || [];
70
+ window.gtag = function gtag(...args) {
71
+ window.dataLayer.push(args);
72
+ };
73
+ window.gtag("js", /* @__PURE__ */ new Date());
74
+ window.gtag("config", this.config.measurementId, {
75
+ send_page_view: false
76
+ });
77
+ script.onload = () => resolve();
78
+ script.onerror = () => resolve();
79
+ });
80
+ }
81
+ buildGtagResult() {
82
+ return {
83
+ provider: this.name,
84
+ success: true,
85
+ statusCode: 200
86
+ };
87
+ }
88
+ async trackBrowser({ event, properties, userId }) {
89
+ if (typeof window === "undefined") {
90
+ return this.buildGtagResult();
91
+ }
92
+ await this.ensureGtagLoaded();
93
+ if (userId) {
94
+ window.gtag("set", { user_id: userId });
95
+ }
96
+ window.gtag("event", event, properties || {});
97
+ return this.buildGtagResult();
98
+ }
99
+ async identifyBrowser({ userId, traits }) {
100
+ if (typeof window === "undefined") {
101
+ return this.buildGtagResult();
102
+ }
103
+ await this.ensureGtagLoaded();
104
+ window.gtag("set", { user_id: userId });
105
+ if (traits) {
106
+ Object.entries(traits).forEach(([key, value]) => {
107
+ window.gtag("set", { [key]: value });
108
+ });
109
+ }
110
+ return this.buildGtagResult();
111
+ }
112
+ async pageBrowser({ name, url, referrer, userId }) {
113
+ if (typeof window === "undefined") {
114
+ return this.buildGtagResult();
115
+ }
116
+ await this.ensureGtagLoaded();
117
+ const config = {
118
+ page_title: name,
119
+ page_location: url,
120
+ page_referrer: referrer,
121
+ send_page_view: true
122
+ };
123
+ if (userId) {
124
+ config.user_id = userId;
125
+ }
126
+ window.gtag("config", this.config.measurementId, config);
127
+ return this.buildGtagResult();
49
128
  }
50
129
  async track({ event, properties, userId, anonymousId, timestamp }) {
130
+ if (this.isBrowser) {
131
+ return this.trackBrowser({ event, properties, userId, anonymousId, timestamp });
132
+ }
51
133
  try {
52
- const clientId = anonymousId ?? this.config.clientId ?? "anonymous";
134
+ const client_id = anonymousId ?? this.config.clientId ?? "anonymous";
53
135
  const params = { ...properties };
54
136
  if (timestamp) {
55
137
  params["timestamp_micros"] = timestamp.getTime() * 1e3;
56
138
  }
57
139
  const body = {
58
- client_id: clientId,
140
+ client_id,
59
141
  events: [{ name: event, params }]
60
142
  };
61
143
  if (userId) {
@@ -80,6 +162,9 @@ var GoogleAnalyticsProvider = class extends BaseProvider {
80
162
  }
81
163
  }
82
164
  async identify({ userId, traits }) {
165
+ if (this.isBrowser) {
166
+ return this.identifyBrowser({ userId, traits });
167
+ }
83
168
  try {
84
169
  const body = {
85
170
  client_id: userId,
@@ -108,6 +193,9 @@ var GoogleAnalyticsProvider = class extends BaseProvider {
108
193
  }
109
194
  }
110
195
  async page({ name, url, referrer, userId, anonymousId }) {
196
+ if (this.isBrowser) {
197
+ return this.pageBrowser({ name, url, referrer, userId, anonymousId });
198
+ }
111
199
  return this.track({
112
200
  event: "page_view",
113
201
  properties: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mytart",
3
- "version": "0.1.7",
3
+ "version": "0.2.1",
4
4
  "description": "Multi-Yield Tracking & Analytics Relay Tool — framework-agnostic analytics for any project",
5
5
  "keywords": [
6
6
  "analytics",