UNPKG

20.4 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2019 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18import { FirebaseApp } from '@firebase/app-types';
19
20export type GtagCommand = 'event' | 'set' | 'config';
21
22export type DataLayer = Array<IArguments>;
23
24/**
25 * Additional options that can be passed to Firebase Analytics method
26 * calls such as `logEvent`, etc.
27 */
28export interface AnalyticsCallOptions {
29 /**
30 * If true, this config or event call applies globally to all
31 * analytics properties on the page.
32 */
33 global: boolean;
34}
35
36export interface FirebaseAnalytics {
37 app: FirebaseApp;
38
39 /**
40 * Sends analytics event with given `eventParams`. This method
41 * automatically associates this logged event with this Firebase web
42 * app instance on this device.
43 * List of recommended event parameters can be found in
44 * {@link https://developers.google.com/gtagjs/reference/ga4-events
45 * | the GA4 reference documentation}.
46 */
47 logEvent(
48 eventName: 'add_payment_info',
49 eventParams?: {
50 coupon?: EventParams['coupon'];
51 currency?: EventParams['currency'];
52 items?: EventParams['items'];
53 payment_type?: EventParams['payment_type'];
54 value?: EventParams['value'];
55 [key: string]: any;
56 },
57 options?: AnalyticsCallOptions
58 ): void;
59
60 /**
61 * Sends analytics event with given `eventParams`. This method
62 * automatically associates this logged event with this Firebase web
63 * app instance on this device.
64 * List of recommended event parameters can be found in
65 * {@link https://developers.google.com/gtagjs/reference/ga4-events
66 * | the GA4 reference documentation}.
67 */
68 logEvent(
69 eventName: 'add_shipping_info',
70 eventParams?: {
71 coupon?: EventParams['coupon'];
72 currency?: EventParams['currency'];
73 items?: EventParams['items'];
74 shipping_tier?: EventParams['shipping_tier'];
75 value?: EventParams['value'];
76 [key: string]: any;
77 },
78 options?: AnalyticsCallOptions
79 ): void;
80
81 /**
82 * Sends analytics event with given `eventParams`. This method
83 * automatically associates this logged event with this Firebase web
84 * app instance on this device.
85 * List of recommended event parameters can be found in
86 * {@link https://developers.google.com/gtagjs/reference/ga4-events
87 * | the GA4 reference documentation}.
88 */
89 logEvent(
90 eventName: 'add_to_cart' | 'add_to_wishlist' | 'remove_from_cart',
91 eventParams?: {
92 currency?: EventParams['currency'];
93 value?: EventParams['value'];
94 items?: EventParams['items'];
95 [key: string]: any;
96 },
97 options?: AnalyticsCallOptions
98 ): void;
99
100 /**
101 * Sends analytics event with given `eventParams`. This method
102 * automatically associates this logged event with this Firebase web
103 * app instance on this device.
104 * List of recommended event parameters can be found in
105 * {@link https://developers.google.com/gtagjs/reference/ga4-events
106 * | the GA4 reference documentation}.
107 */
108 logEvent(
109 eventName: 'begin_checkout',
110 eventParams?: {
111 currency?: EventParams['currency'];
112 coupon?: EventParams['coupon'];
113 value?: EventParams['value'];
114 items?: EventParams['items'];
115 [key: string]: any;
116 },
117 options?: AnalyticsCallOptions
118 ): void;
119
120 /**
121 * Sends analytics event with given `eventParams`. This method
122 * automatically associates this logged event with this Firebase web
123 * app instance on this device.
124 * List of recommended event parameters can be found in
125 * {@link https://developers.google.com/gtagjs/reference/ga4-events
126 * | the GA4 reference documentation}.
127 */
128 logEvent(
129 eventName: 'checkout_progress',
130 eventParams?: {
131 currency?: EventParams['currency'];
132 coupon?: EventParams['coupon'];
133 value?: EventParams['value'];
134 items?: EventParams['items'];
135 checkout_step?: EventParams['checkout_step'];
136 checkout_option?: EventParams['checkout_option'];
137 [key: string]: any;
138 },
139 options?: AnalyticsCallOptions
140 ): void;
141
142 /**
143 * Sends analytics event with given `eventParams`. This method
144 * automatically associates this logged event with this Firebase web
145 * app instance on this device.
146 * See
147 * {@link https://developers.google.com/analytics/devguides/collection/ga4/exceptions
148 * | Measure exceptions}.
149 */
150 logEvent(
151 eventName: 'exception',
152 eventParams?: {
153 description?: EventParams['description'];
154 fatal?: EventParams['fatal'];
155 [key: string]: any;
156 },
157 options?: AnalyticsCallOptions
158 ): void;
159
160 /**
161 * Sends analytics event with given `eventParams`. This method
162 * automatically associates this logged event with this Firebase web
163 * app instance on this device.
164 * List of recommended event parameters can be found in
165 * {@link https://developers.google.com/gtagjs/reference/ga4-events
166 * | the GA4 reference documentation}.
167 */
168 logEvent(
169 eventName: 'generate_lead',
170 eventParams?: {
171 value?: EventParams['value'];
172 currency?: EventParams['currency'];
173 [key: string]: any;
174 },
175 options?: AnalyticsCallOptions
176 ): void;
177
178 /**
179 * Sends analytics event with given `eventParams`. This method
180 * automatically associates this logged event with this Firebase web
181 * app instance on this device.
182 * List of recommended event parameters can be found in
183 * {@link https://developers.google.com/gtagjs/reference/ga4-events
184 * | the GA4 reference documentation}.
185 */
186 logEvent(
187 eventName: 'login',
188 eventParams?: {
189 method?: EventParams['method'];
190 [key: string]: any;
191 },
192 options?: AnalyticsCallOptions
193 ): void;
194
195 /**
196 * Sends analytics event with given `eventParams`. This method
197 * automatically associates this logged event with this Firebase web
198 * app instance on this device.
199 * See
200 * {@link https://developers.google.com/analytics/devguides/collection/ga4/page-view
201 * | Page views}.
202 */
203 logEvent(
204 eventName: 'page_view',
205 eventParams?: {
206 page_title?: string;
207 page_location?: string;
208 page_path?: string;
209 [key: string]: any;
210 },
211 options?: AnalyticsCallOptions
212 ): void;
213
214 /**
215 * Sends analytics event with given `eventParams`. This method
216 * automatically associates this logged event with this Firebase web
217 * app instance on this device.
218 * List of recommended event parameters can be found in
219 * {@link https://developers.google.com/gtagjs/reference/ga4-events
220 * | the GA4 reference documentation}.
221 */
222 logEvent(
223 eventName: 'purchase' | 'refund',
224 eventParams?: {
225 value?: EventParams['value'];
226 currency?: EventParams['currency'];
227 transaction_id: EventParams['transaction_id'];
228 tax?: EventParams['tax'];
229 shipping?: EventParams['shipping'];
230 items?: EventParams['items'];
231 coupon?: EventParams['coupon'];
232 affiliation?: EventParams['affiliation'];
233 [key: string]: any;
234 },
235 options?: AnalyticsCallOptions
236 ): void;
237
238 /**
239 * Sends analytics event with given `eventParams`. This method
240 * automatically associates this logged event with this Firebase web
241 * app instance on this device.
242 * See {@link https://firebase.google.com/docs/analytics/screenviews
243 * | Track Screenviews}.
244 */
245 logEvent(
246 eventName: 'screen_view',
247 eventParams?: {
248 firebase_screen: EventParams['firebase_screen'];
249 firebase_screen_class: EventParams['firebase_screen_class'];
250 [key: string]: any;
251 },
252 options?: AnalyticsCallOptions
253 ): void;
254
255 /**
256 * Sends analytics event with given `eventParams`. This method
257 * automatically associates this logged event with this Firebase web
258 * app instance on this device.
259 * List of recommended event parameters can be found in
260 * {@link https://developers.google.com/gtagjs/reference/ga4-events
261 * | the GA4 reference documentation}.
262 */
263 logEvent(
264 eventName: 'search' | 'view_search_results',
265 eventParams?: {
266 search_term?: EventParams['search_term'];
267 [key: string]: any;
268 },
269 options?: AnalyticsCallOptions
270 ): void;
271
272 /**
273 * Sends analytics event with given `eventParams`. This method
274 * automatically associates this logged event with this Firebase web
275 * app instance on this device.
276 * List of recommended event parameters can be found in
277 * {@link https://developers.google.com/gtagjs/reference/ga4-events
278 * | the GA4 reference documentation}.
279 */
280 logEvent(
281 eventName: 'select_content',
282 eventParams?: {
283 content_type?: EventParams['content_type'];
284 item_id?: EventParams['item_id'];
285 [key: string]: any;
286 },
287 options?: AnalyticsCallOptions
288 ): void;
289
290 /**
291 * Sends analytics event with given `eventParams`. This method
292 * automatically associates this logged event with this Firebase web
293 * app instance on this device.
294 * List of recommended event parameters can be found in
295 * {@link https://developers.google.com/gtagjs/reference/ga4-events
296 * | the GA4 reference documentation}.
297 */
298 logEvent(
299 eventName: 'select_item',
300 eventParams?: {
301 items?: EventParams['items'];
302 item_list_name?: EventParams['item_list_name'];
303 item_list_id?: EventParams['item_list_id'];
304 [key: string]: any;
305 },
306 options?: AnalyticsCallOptions
307 ): void;
308
309 /**
310 * Sends analytics event with given `eventParams`. This method
311 * automatically associates this logged event with this Firebase web
312 * app instance on this device.
313 * List of recommended event parameters can be found in
314 * {@link https://developers.google.com/gtagjs/reference/ga4-events
315 * | the GA4 reference documentation}.
316 */
317 logEvent(
318 eventName: 'select_promotion' | 'view_promotion',
319 eventParams?: {
320 items?: EventParams['items'];
321 promotion_id?: EventParams['promotion_id'];
322 promotion_name?: EventParams['promotion_name'];
323 [key: string]: any;
324 },
325 options?: AnalyticsCallOptions
326 ): void;
327
328 /**
329 * Sends analytics event with given `eventParams`. This method
330 * automatically associates this logged event with this Firebase web
331 * app instance on this device.
332 * List of recommended event parameters can be found in
333 * {@link https://developers.google.com/gtagjs/reference/ga4-events
334 * | the GA4 reference documentation}.
335 */
336 logEvent(
337 eventName: 'set_checkout_option',
338 eventParams?: {
339 checkout_step?: EventParams['checkout_step'];
340 checkout_option?: EventParams['checkout_option'];
341 [key: string]: any;
342 },
343 options?: AnalyticsCallOptions
344 ): void;
345
346 /**
347 * Sends analytics event with given `eventParams`. This method
348 * automatically associates this logged event with this Firebase web
349 * app instance on this device.
350 * List of recommended event parameters can be found in
351 * {@link https://developers.google.com/gtagjs/reference/ga4-events
352 * | the GA4 reference documentation}.
353 */
354 logEvent(
355 eventName: 'share',
356 eventParams?: {
357 method?: EventParams['method'];
358 content_type?: EventParams['content_type'];
359 item_id?: EventParams['item_id'];
360 [key: string]: any;
361 },
362 options?: AnalyticsCallOptions
363 ): void;
364
365 /**
366 * Sends analytics event with given `eventParams`. This method
367 * automatically associates this logged event with this Firebase web
368 * app instance on this device.
369 * List of recommended event parameters can be found in
370 * {@link https://developers.google.com/gtagjs/reference/ga4-events
371 * | the GA4 reference documentation}.
372 */
373 logEvent(
374 eventName: 'sign_up',
375 eventParams?: {
376 method?: EventParams['method'];
377 [key: string]: any;
378 },
379 options?: AnalyticsCallOptions
380 ): void;
381
382 /**
383 * Sends analytics event with given `eventParams`. This method
384 * automatically associates this logged event with this Firebase web
385 * app instance on this device.
386 * List of recommended event parameters can be found in
387 * {@link https://developers.google.com/gtagjs/reference/ga4-events
388 * | the GA4 reference documentation}.
389 */
390 logEvent(
391 eventName: 'timing_complete',
392 eventParams?: {
393 name: string;
394 value: number;
395 event_category?: string;
396 event_label?: string;
397 [key: string]: any;
398 },
399 options?: AnalyticsCallOptions
400 ): void;
401
402 /**
403 * Sends analytics event with given `eventParams`. This method
404 * automatically associates this logged event with this Firebase web
405 * app instance on this device.
406 * List of recommended event parameters can be found in
407 * {@link https://developers.google.com/gtagjs/reference/ga4-events
408 * | the GA4 reference documentation}.
409 */
410 logEvent(
411 eventName: 'view_cart' | 'view_item',
412 eventParams?: {
413 currency?: EventParams['currency'];
414 items?: EventParams['items'];
415 value?: EventParams['value'];
416 [key: string]: any;
417 },
418 options?: AnalyticsCallOptions
419 ): void;
420
421 /**
422 * Sends analytics event with given `eventParams`. This method
423 * automatically associates this logged event with this Firebase web
424 * app instance on this device.
425 * List of recommended event parameters can be found in
426 * {@link https://developers.google.com/gtagjs/reference/ga4-events
427 * | the GA4 reference documentation}.
428 */
429 logEvent(
430 eventName: 'view_item_list',
431 eventParams?: {
432 items?: EventParams['items'];
433 item_list_name?: EventParams['item_list_name'];
434 item_list_id?: EventParams['item_list_id'];
435 [key: string]: any;
436 },
437 options?: AnalyticsCallOptions
438 ): void;
439
440 /**
441 * Sends analytics event with given `eventParams`. This method
442 * automatically associates this logged event with this Firebase web
443 * app instance on this device.
444 * List of recommended event parameters can be found in
445 * {@link https://developers.google.com/gtagjs/reference/ga4-events
446 * | the GA4 reference documentation}.
447 */
448 logEvent<T extends string>(
449 eventName: CustomEventName<T>,
450 eventParams?: { [key: string]: any },
451 options?: AnalyticsCallOptions
452 ): void;
453
454 /**
455 * Use gtag 'config' command to set 'screen_name'.
456 *
457 * @deprecated Use {@link logEvent} with `eventName` as 'screen_view' and add relevant `eventParams`.
458 * See {@link https://firebase.google.com/docs/analytics/screenviews | Track Screenviews}.
459 */
460 setCurrentScreen(screenName: string, options?: AnalyticsCallOptions): void;
461
462 /**
463 * Use gtag 'config' command to set 'user_id'.
464 */
465 setUserId(id: string, options?: AnalyticsCallOptions): void;
466
467 /**
468 * Use gtag 'config' command to set all params specified.
469 */
470 setUserProperties(
471 properties: { [key: string]: any },
472 options?: AnalyticsCallOptions
473 ): void;
474
475 /**
476 * Sets whether analytics collection is enabled for this app on this device.
477 * window['ga-disable-analyticsId'] = true;
478 */
479 setAnalyticsCollectionEnabled(enabled: boolean): void;
480}
481
482export type CustomEventName<T> = T extends EventNameString ? never : T;
483
484/**
485 * Specifies custom options for your Firebase Analytics instance.
486 * You must set these before initializing `firebase.analytics()`.
487 */
488export interface SettingsOptions {
489 /** Sets custom name for `gtag` function. */
490 gtagName?: string;
491 /** Sets custom name for `dataLayer` array used by gtag. */
492 dataLayerName?: string;
493}
494
495/**
496 * Standard `gtag` function provided by gtag.js.
497 */
498export interface Gtag {
499 (
500 command: 'config',
501 targetId: string,
502 config?: ControlParams | EventParams | CustomParams
503 ): void;
504 (command: 'set', config: CustomParams): void;
505 (
506 command: 'event',
507 eventName: string,
508 eventParams?: ControlParams | EventParams | CustomParams
509 ): void;
510}
511
512/**
513 * Standard gtag.js control parameters.
514 * For more information, see
515 * {@link https://developers.google.com/gtagjs/reference/parameter
516 * the gtag.js documentation on parameters}.
517 */
518export interface ControlParams {
519 groups?: string | string[];
520 send_to?: string | string[];
521 event_callback?: () => void;
522 event_timeout?: number;
523}
524
525/**
526 * Standard gtag.js event parameters.
527 * For more information, see
528 * {@link https://developers.google.com/gtagjs/reference/parameter
529 * the gtag.js documentation on parameters}.
530 */
531export interface EventParams {
532 checkout_option?: string;
533 checkout_step?: number;
534 item_id?: string;
535 content_type?: string;
536 coupon?: string;
537 currency?: string;
538 description?: string;
539 fatal?: boolean;
540 items?: Item[];
541 method?: string;
542 number?: string;
543 promotions?: Promotion[];
544 screen_name?: string;
545 /**
546 * Firebase-specific. Use to log a `screen_name` to Firebase Analytics.
547 */
548 firebase_screen?: string;
549 /**
550 * Firebase-specific. Use to log a `screen_class` to Firebase Analytics.
551 */
552 firebase_screen_class?: string;
553 search_term?: string;
554 shipping?: Currency;
555 tax?: Currency;
556 transaction_id?: string;
557 value?: number;
558 event_label?: string;
559 event_category?: string;
560 shipping_tier?: string;
561 item_list_id?: string;
562 item_list_name?: string;
563 promotion_id?: string;
564 promotion_name?: string;
565 payment_type?: string;
566 affiliation?: string;
567}
568
569/**
570 * Any custom params the user may pass to gtag.js.
571 */
572export interface CustomParams {
573 [key: string]: any;
574}
575
576/**
577 * Type for standard gtag.js event names. `logEvent` also accepts any
578 * custom string and interprets it as a custom event name.
579 */
580export type EventNameString =
581 | 'add_payment_info'
582 | 'add_shipping_info'
583 | 'add_to_cart'
584 | 'add_to_wishlist'
585 | 'begin_checkout'
586 | 'checkout_progress'
587 | 'exception'
588 | 'generate_lead'
589 | 'login'
590 | 'page_view'
591 | 'purchase'
592 | 'refund'
593 | 'remove_from_cart'
594 | 'screen_view'
595 | 'search'
596 | 'select_content'
597 | 'select_item'
598 | 'select_promotion'
599 | 'set_checkout_option'
600 | 'share'
601 | 'sign_up'
602 | 'timing_complete'
603 | 'view_cart'
604 | 'view_item'
605 | 'view_item_list'
606 | 'view_promotion'
607 | 'view_search_results';
608
609export type Currency = string | number;
610
611export interface Item {
612 item_id?: string;
613 item_name?: string;
614 item_brand?: string;
615 item_category?: string;
616 item_category2?: string;
617 item_category3?: string;
618 item_category4?: string;
619 item_category5?: string;
620 item_variant?: string;
621 price?: Currency;
622 quantity?: number;
623 index?: number;
624 coupon?: string;
625 item_list_name?: string;
626 item_list_id?: string;
627 discount?: Currency;
628 affiliation?: string;
629 creative_name?: string;
630 creative_slot?: string;
631 promotion_id?: string;
632 promotion_name?: string;
633 location_id?: string;
634 /** @deprecated Use item_brand instead. */
635 brand?: string;
636 /** @deprecated Use item_category instead. */
637 category?: string;
638 /** @deprecated Use item_id instead. */
639 id?: string;
640 /** @deprecated Use item_name instead. */
641 name?: string;
642}
643
644/** @deprecated Use Item instead. */
645export interface Promotion {
646 creative_name?: string;
647 creative_slot?: string;
648 id?: string;
649 name?: string;
650}
651
652/**
653 * Dynamic configuration fetched from server.
654 * See https://firebase.google.com/docs/projects/api/reference/rest/v1beta1/projects.webApps/getConfig
655 */
656interface DynamicConfig {
657 projectId: string;
658 appId: string;
659 databaseURL: string;
660 storageBucket: string;
661 locationId: string;
662 apiKey: string;
663 authDomain: string;
664 messagingSenderId: string;
665 measurementId: string;
666}
667
668interface MinimalDynamicConfig {
669 appId: string;
670 measurementId: string;
671}
672
673/**
674 * Encapsulates metadata concerning throttled fetch requests.
675 */
676export interface ThrottleMetadata {
677 // The number of times fetch has backed off. Used for resuming backoff after a timeout.
678 backoffCount: number;
679 // The Unix timestamp in milliseconds when callers can retry a request.
680 throttleEndTimeMillis: number;
681}
682
683declare module '@firebase/component' {
684 interface NameServiceMapping {
685 'analytics-compat': FirebaseAnalytics;
686 }
687}