UNPKG

20.2 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`, `setCurrentScreen`, 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 setCurrentScreen(screenName: string, options?: AnalyticsCallOptions): void;
458
459 /**
460 * Use gtag 'config' command to set 'user_id'.
461 */
462 setUserId(id: string, options?: AnalyticsCallOptions): void;
463
464 /**
465 * Use gtag 'config' command to set all params specified.
466 */
467 setUserProperties(
468 properties: { [key: string]: any },
469 options?: AnalyticsCallOptions
470 ): void;
471
472 /**
473 * Sets whether analytics collection is enabled for this app on this device.
474 * window['ga-disable-analyticsId'] = true;
475 */
476 setAnalyticsCollectionEnabled(enabled: boolean): void;
477}
478
479export type CustomEventName<T> = T extends EventNameString ? never : T;
480
481/**
482 * Specifies custom options for your Firebase Analytics instance.
483 * You must set these before initializing `firebase.analytics()`.
484 */
485export interface SettingsOptions {
486 /** Sets custom name for `gtag` function. */
487 gtagName?: string;
488 /** Sets custom name for `dataLayer` array used by gtag. */
489 dataLayerName?: string;
490}
491
492/**
493 * Standard `gtag` function provided by gtag.js.
494 */
495export interface Gtag {
496 (
497 command: 'config',
498 targetId: string,
499 config?: ControlParams | EventParams | CustomParams
500 ): void;
501 (command: 'set', config: CustomParams): void;
502 (
503 command: 'event',
504 eventName: string,
505 eventParams?: ControlParams | EventParams | CustomParams
506 ): void;
507}
508
509/**
510 * Standard gtag.js control parameters.
511 * For more information, see
512 * {@link https://developers.google.com/gtagjs/reference/parameter
513 * the gtag.js documentation on parameters}.
514 */
515export interface ControlParams {
516 groups?: string | string[];
517 send_to?: string | string[];
518 event_callback?: () => void;
519 event_timeout?: number;
520}
521
522/**
523 * Standard gtag.js event parameters.
524 * For more information, see
525 * {@link https://developers.google.com/gtagjs/reference/parameter
526 * the gtag.js documentation on parameters}.
527 */
528export interface EventParams {
529 checkout_option?: string;
530 checkout_step?: number;
531 item_id?: string;
532 content_type?: string;
533 coupon?: string;
534 currency?: string;
535 description?: string;
536 fatal?: boolean;
537 items?: Item[];
538 method?: string;
539 number?: string;
540 promotions?: Promotion[];
541 screen_name?: string;
542 /**
543 * Firebase-specific. Use to log a `screen_name` to Firebase Analytics.
544 */
545 firebase_screen?: string;
546 /**
547 * Firebase-specific. Use to log a `screen_class` to Firebase Analytics.
548 */
549 firebase_screen_class?: string;
550 search_term?: string;
551 shipping?: Currency;
552 tax?: Currency;
553 transaction_id?: string;
554 value?: number;
555 event_label?: string;
556 event_category?: string;
557 shipping_tier?: string;
558 item_list_id?: string;
559 item_list_name?: string;
560 promotion_id?: string;
561 promotion_name?: string;
562 payment_type?: string;
563 affiliation?: string;
564}
565
566/**
567 * Any custom params the user may pass to gtag.js.
568 */
569export interface CustomParams {
570 [key: string]: any;
571}
572
573/**
574 * Type for standard gtag.js event names. `logEvent` also accepts any
575 * custom string and interprets it as a custom event name.
576 */
577export type EventNameString =
578 | 'add_payment_info'
579 | 'add_shipping_info'
580 | 'add_to_cart'
581 | 'add_to_wishlist'
582 | 'begin_checkout'
583 | 'checkout_progress'
584 | 'exception'
585 | 'generate_lead'
586 | 'login'
587 | 'page_view'
588 | 'purchase'
589 | 'refund'
590 | 'remove_from_cart'
591 | 'screen_view'
592 | 'search'
593 | 'select_content'
594 | 'select_item'
595 | 'select_promotion'
596 | 'set_checkout_option'
597 | 'share'
598 | 'sign_up'
599 | 'timing_complete'
600 | 'view_cart'
601 | 'view_item'
602 | 'view_item_list'
603 | 'view_promotion'
604 | 'view_search_results';
605
606export type Currency = string | number;
607
608export interface Item {
609 item_id?: string;
610 item_name?: string;
611 item_brand?: string;
612 item_category?: string;
613 item_category2?: string;
614 item_category3?: string;
615 item_category4?: string;
616 item_category5?: string;
617 item_variant?: string;
618 price?: Currency;
619 quantity?: number;
620 index?: number;
621 coupon?: string;
622 item_list_name?: string;
623 item_list_id?: string;
624 discount?: Currency;
625 affiliation?: string;
626 creative_name?: string;
627 creative_slot?: string;
628 promotion_id?: string;
629 promotion_name?: string;
630 location_id?: string;
631 /** @deprecated Use item_brand instead. */
632 brand?: string;
633 /** @deprecated Use item_category instead. */
634 category?: string;
635 /** @deprecated Use item_id instead. */
636 id?: string;
637 /** @deprecated Use item_name instead. */
638 name?: string;
639}
640
641/** @deprecated Use Item instead. */
642export interface Promotion {
643 creative_name?: string;
644 creative_slot?: string;
645 id?: string;
646 name?: string;
647}
648
649/**
650 * Dynamic configuration fetched from server.
651 * See https://firebase.google.com/docs/projects/api/reference/rest/v1beta1/projects.webApps/getConfig
652 */
653interface DynamicConfig {
654 projectId: string;
655 appId: string;
656 databaseURL: string;
657 storageBucket: string;
658 locationId: string;
659 apiKey: string;
660 authDomain: string;
661 messagingSenderId: string;
662 measurementId: string;
663}
664
665interface MinimalDynamicConfig {
666 appId: string;
667 measurementId: string;
668}
669
670/**
671 * Encapsulates metadata concerning throttled fetch requests.
672 */
673export interface ThrottleMetadata {
674 // The number of times fetch has backed off. Used for resuming backoff after a timeout.
675 backoffCount: number;
676 // The Unix timestamp in milliseconds when callers can retry a request.
677 throttleEndTimeMillis: number;
678}
679
680declare module '@firebase/component' {
681 interface NameServiceMapping {
682 'analytics-compat': FirebaseAnalytics;
683 }
684}