Skip to content

Commit 468194b

Browse files
César ValadezMatteoGabriele
César Valadez
andauthored
feat(typescript): add missing options and methods (#63)
Co-authored-by: Matteo Gabriele <[email protected]>
1 parent 94ac27a commit 468194b

File tree

2 files changed

+142
-24
lines changed

2 files changed

+142
-24
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
dist/
22
coverage/
3+
*.ts

vue-gtag.d.ts

+141-24
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,109 @@
1-
declare module 'vue-gtag' {
2-
import _Vue, { PluginFunction } from 'vue';
3-
import VueRouter from 'vue-router';
1+
declare module "vue-gtag" {
2+
import VueRouter from "vue-router";
3+
import _Vue, { PluginFunction } from "vue";
4+
type Currency = string | number;
5+
6+
type EventNames =
7+
| "add_payment_info"
8+
| "add_to_cart"
9+
| "add_to_wishlist"
10+
| "begin_checkout"
11+
| "checkout_progress"
12+
| "exception"
13+
| "generate_lead"
14+
| "login"
15+
| "page_view"
16+
| "purchase"
17+
| "refund"
18+
| "remove_from_cart"
19+
| "screen_view"
20+
| "search"
21+
| "select_content"
22+
| "set_checkout_option"
23+
| "share"
24+
| "sign_up"
25+
| "timing_complete"
26+
| "view_item"
27+
| "view_item_list"
28+
| "view_promotion"
29+
| "view_search_results";
30+
31+
interface GtagPromotion {
32+
creative_name?: string;
33+
creative_slot?: string;
34+
id?: string;
35+
name?: string;
36+
}
37+
interface GtagItem {
38+
brand?: string;
39+
category?: string;
40+
creative_name?: string;
41+
creative_slot?: string;
42+
id?: string;
43+
location_id?: string;
44+
name?: string;
45+
price?: Currency;
46+
quantity?: number;
47+
}
48+
49+
type GtagControlParams = {
50+
groups?: string | string[];
51+
send_to?: string | string[];
52+
event_callback?: () => void;
53+
event_timeout?: number;
54+
};
55+
56+
type GtagEventParams = {
57+
checkout_option?: string;
58+
checkout_step?: number;
59+
content_id?: string;
60+
content_type?: string;
61+
coupon?: string;
62+
currency?: string;
63+
description?: string;
64+
fatal?: boolean;
65+
items?: GtagItem[];
66+
method?: string;
67+
number?: string;
68+
promotions?: GtagPromotion[];
69+
screen_name?: string;
70+
search_term?: string;
71+
shipping?: Currency;
72+
tax?: Currency;
73+
transaction_id?: string;
74+
value?: number;
75+
event_label?: string;
76+
event_category?: string;
77+
};
78+
interface CustomParams {
79+
[key: string]: any;
80+
}
81+
interface Gtag {
82+
(
83+
command: "config",
84+
targetId: string,
85+
config?: GtagControlParams | GtagEventParams | CustomParams
86+
): void;
87+
(command: "set", config: CustomParams): void;
88+
(command: "js", config: Date): void;
89+
(
90+
command: "event",
91+
eventName: EventNames | string,
92+
eventParams?: GtagControlParams | GtagEventParams | CustomParams
93+
): void;
94+
}
95+
type GtagGenericParams = GtagControlParams | GtagEventParams | CustomParams;
496

597
export interface PageView {
698
page_title: string;
799
page_location: string;
8100
page_path: string;
9101
}
10102

11-
export interface EventParams {
12-
/** string that will appear as the event category */
13-
event_category?: string;
14-
/** string that will appear as the event label */
15-
event_label?: string;
16-
/** non-negative integer that will appear as the event value */
17-
value?: number;
18-
[key: string]: any;
103+
export interface Event extends GtagEventParams {
104+
event_category: string;
105+
event_label: string;
106+
value: number;
19107
}
20108

21109
export interface ScreenView {
@@ -25,8 +113,13 @@ declare module 'vue-gtag' {
25113

26114
export interface Purchase {
27115
transaction_id: string;
28-
affiliation: string;
29-
value: number;
116+
affiliation?: string;
117+
value?: number;
118+
tax?: number;
119+
shipping?: number;
120+
items?: GtagItem[];
121+
checkout_step?: number;
122+
checkout_option?: string;
30123
}
31124

32125
export interface Refund {
@@ -37,38 +130,58 @@ declare module 'vue-gtag' {
37130

38131
export interface Linker {
39132
domains: string[];
133+
decorate_forms?: boolean;
134+
accept_incoming?: boolean;
135+
url_position?: "fragment" | "query";
40136
}
41137

42138
export interface Exception {
43139
description: string;
44140
fatal: boolean;
45141
}
46142

143+
export interface Timing {
144+
name: string;
145+
value: number;
146+
event_category?: string;
147+
event_label?: string;
148+
}
149+
47150
export type Dictionary<T> = { [key: string]: T };
48151

49152
export interface VueGtag {
153+
optIn(): void;
154+
optOut(): void;
50155
pageview(pageView: PageView): void;
51-
52156
/**
53157
* Send a Google Analytics Event.
54158
*
55159
* @see https://developers.google.com/analytics/devguides/collection/gtagjs/events
56160
*
57161
* @param action string that will appear as the event action in Google Analytics Event reports
58-
* @param eventParams
162+
* @param event
59163
*/
60-
event(action: string, eventParams?: EventParams): void;
164+
event(action: EventNames | string, event: Event): void;
61165
screenview(screenView: ScreenView): void;
62166
customMap(map: Dictionary<string>): void;
167+
purchase(puchase: Event): void;
63168
purchase(purchase: Purchase): void;
64169
refund(refund: Refund): void;
65170
linker(config: Linker): void;
66171
exception(ex: Exception): void;
172+
set(config: CustomParams): void;
173+
config(config?: GtagGenericParams);
174+
time(timing: Timing);
175+
}
176+
177+
export interface DomainConfig {
178+
id: string;
179+
params?: GtagGenericParams & { send_page_view: boolean };
67180
}
68181

69182
export interface PluginOptions {
70183
appName?: string;
71-
pageTrackerTemplate?: () => void;
184+
pageTrackerTemplate?: () => PageView;
72185
onBeforeTrack?: () => void;
73186
onAfterTrack?: () => void;
74187
onReady?: () => void;
@@ -79,20 +192,24 @@ declare module 'vue-gtag' {
79192
pageTrackerEnabled?: boolean;
80193
pageTrackerScreenviewEnabled?: boolean;
81194
defaultGroupName?: string;
82-
includes?: any;
83-
config?: {
84-
id: string,
85-
params?: Dictionary<any>
86-
};
195+
includes?: DomainConfig[];
196+
config?: DomainConfig;
87197
}
88198

89199
export class VueGtagPlugin {
90-
static install(Vue: typeof _Vue, options: PluginOptions, router?: VueRouter): void;
200+
static install(
201+
Vue: typeof _Vue,
202+
options: PluginOptions,
203+
router?: VueRouter
204+
): void;
91205
}
92206

207+
export function bootstrap(): Promise<Gtag>;
208+
export function setOptions(PluginOptions): void;
209+
93210
export default VueGtagPlugin;
94211

95-
module 'vue/types/vue' {
212+
module "vue/types/vue" {
96213
interface Vue {
97214
$gtag: VueGtag;
98215
}

0 commit comments

Comments
 (0)