Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit d52c5e4

Browse files
author
Julio Farah
authored
Expose AJS types and update README (#173)
* Expose AJS types * improve types * add info on how to use types from npm
1 parent aa59e80 commit d52c5e4

File tree

8 files changed

+310
-21
lines changed

8 files changed

+310
-21
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ This is the core of [Analytics.js][], the open-source library that powers data c
77

88
To build this into a full, usable library, see the [Analytics.js](https://github.com/segmentio/analytics.js) repository.
99

10+
## Using Types
11+
12+
We've recently introduced Typescript support and types to Analytics.js Core. While the exposed types still need some work
13+
(pull requests are welcome!), they're ready to be used.
14+
15+
### Importing as an npm module
16+
17+
If you use analytics.js-core as an npm module, you can leverage its types out of the box:
18+
<img src="![types](https://user-images.githubusercontent.com/484013/88733944-bbcf3680-d0ec-11ea-904c-a63b68f4975e.gif)" alt="Example of using analytics js types" width="500px">
19+
1020
## License
1121

1222
Released under the [MIT license](LICENSE).

lib/analytics.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ Analytics.prototype.addIntegrationMiddleware = function(
134134
* Destination Middleware is chained after integration middleware
135135
*/
136136

137-
// TODO remove `unknown`
138137
Analytics.prototype.addDestinationMiddleware = function(
139138
integrationName: string,
140139
middlewares: Array<unknown>
@@ -339,7 +338,7 @@ Analytics.prototype.identify = function(
339338
* @return {Object}
340339
*/
341340

342-
Analytics.prototype.user = function() {
341+
Analytics.prototype.user = function(): object {
343342
return user;
344343
};
345344

@@ -399,7 +398,7 @@ Analytics.prototype.group = function(
399398
*/
400399

401400
Analytics.prototype.track = function(
402-
event: number,
401+
event: string,
403402
properties: unknown,
404403
options: unknown,
405404
fn: unknown
@@ -517,7 +516,7 @@ Analytics.prototype.trackClick = Analytics.prototype.trackLink = function(
517516
Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(
518517
forms: Element | Array<unknown>,
519518
event: any,
520-
properties: any
519+
properties?: any
521520
): SegmentAnalytics {
522521
if (!forms) return this;
523522
// always arrays, handles jquery
@@ -646,9 +645,9 @@ Analytics.prototype.pageview = function(url: string): SegmentAnalytics {
646645

647646
Analytics.prototype.alias = function(
648647
to: string,
649-
from: string,
650-
options: unknown,
651-
fn: unknown
648+
from?: string,
649+
options?: unknown,
650+
fn?: unknown
652651
): SegmentAnalytics {
653652
// Argument reshuffling.
654653
/* eslint-disable no-unused-expressions, no-sequences */
@@ -960,7 +959,7 @@ Analytics.prototype._parseQuery = function(query: string): SegmentAnalytics {
960959

961960
Analytics.prototype.normalize = function(msg: {
962961
context: { page };
963-
anonymousId;
962+
anonymousId: string;
964963
}): object {
965964
msg = normalize(msg, keys(this._integrations));
966965
if (msg.anonymousId) user.anonymousId(msg.anonymousId);
@@ -1012,7 +1011,7 @@ Analytics.prototype._mergeInitializeAndPlanIntegrations = function(
10121011
* No conflict support.
10131012
*/
10141013

1015-
Analytics.prototype.noConflict = function() {
1014+
Analytics.prototype.noConflict = function(): SegmentAnalytics {
10161015
window.analytics = _analytics;
10171016
return this;
10181017
};

lib/global-modules.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
export {};
1+
import { SegmentAnalytics } from './index.d'
22

33
declare global {
4+
namespace NodeJS {
5+
interface Global {
6+
analytics: SegmentAnalytics.AnalyticsJS
7+
}
8+
}
49
interface Window {
10+
analytics: SegmentAnalytics.AnalyticsJS
511
jQuery: any;
612
Zepto: any;
713
}
814
}
15+
16+
export {};

lib/global.d.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

lib/index.d.ts

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
export namespace SegmentAnalytics {
2+
export interface SegmentOpts {
3+
integrations?: any;
4+
anonymousId?: string;
5+
context?: object;
6+
}
7+
8+
export interface IntegrationsSettings {
9+
// TODO remove `any`
10+
[key: string]: any;
11+
}
12+
13+
export interface CookieOptions {
14+
maxage?: number;
15+
domain?: string;
16+
path?: string;
17+
secure?: boolean;
18+
}
19+
20+
export interface MetricsOptions {
21+
host?: string;
22+
sampleRate?: number;
23+
flushTimer?: number;
24+
maxQueueSize?: number;
25+
}
26+
27+
export interface StoreOptions {
28+
enabled?: boolean;
29+
}
30+
31+
export interface UserOptions {
32+
cookie?: {
33+
key: string;
34+
oldKey: string;
35+
};
36+
localStorage?: {
37+
key: string;
38+
};
39+
persist?: boolean;
40+
}
41+
42+
export interface GroupOptions {
43+
cookie?: {
44+
key: string;
45+
};
46+
localStorage?: {
47+
key: string;
48+
};
49+
persist?: boolean;
50+
}
51+
52+
export interface SegmentIntegration {
53+
All?: boolean;
54+
[integration: string]: boolean | undefined;
55+
}
56+
57+
export interface InitOptions {
58+
initialPageview?: boolean;
59+
cookie?: CookieOptions;
60+
metrics?: MetricsOptions;
61+
localStorage?: StoreOptions;
62+
user?: UserOptions;
63+
group?: GroupOptions;
64+
integrations?: SegmentIntegration;
65+
}
66+
67+
export interface AnalyticsJS {
68+
Integrations: { [name: string]: unknown };
69+
require: any;
70+
VERSION: any;
71+
72+
/**
73+
* Use a `plugin`.
74+
*/
75+
use(plugin: (analytics: AnalyticsJS) => void): AnalyticsJS;
76+
77+
/**
78+
* Define a new `Integration`.
79+
*/
80+
addIntegration(Integration: (options: SegmentOpts) => void): AnalyticsJS;
81+
82+
/**
83+
* Define a new `SourceMiddleware`
84+
*/
85+
addSourceMiddleware(middleware: Function): AnalyticsJS;
86+
87+
/**
88+
* Define a new `IntegrationMiddleware`
89+
* @deprecated Use addDestinationMiddleware instead
90+
*/
91+
addIntegrationMiddleware(middleware: Function): AnalyticsJS;
92+
93+
/**
94+
* Define a new `DestinationMiddleware`
95+
* Destination Middleware is chained after integration middleware
96+
*/
97+
addDestinationMiddleware(
98+
integrationName: string,
99+
middlewares: Array<unknown>
100+
): AnalyticsJS;
101+
102+
/**
103+
* Initialize with the given integration `settings` and `options`.
104+
*
105+
* Aliased to `init` for convenience.
106+
*/
107+
initialize(
108+
settings: IntegrationsSettings,
109+
options: InitOptions
110+
): AnalyticsJS;
111+
112+
/**
113+
* Initialize with the given integration `settings` and `options`.
114+
*
115+
* Aliased to `init` for convenience.
116+
*/
117+
init(settings: IntegrationsSettings, options: InitOptions): AnalyticsJS;
118+
119+
/**
120+
* Set the user's `id`.
121+
*/
122+
setAnonymousId(id: string): AnalyticsJS;
123+
124+
/**
125+
* Add an integration.
126+
*/
127+
add(integration: { name: string | number }): AnalyticsJS;
128+
129+
/**
130+
* Identify a user by optional `id` and `traits`.
131+
*/
132+
identify(
133+
id: string,
134+
traits: unknown,
135+
options: SegmentOpts,
136+
fn: Function | SegmentOpts
137+
): AnalyticsJS;
138+
139+
/**
140+
* Return the current user.
141+
*/
142+
user(): object;
143+
144+
/**
145+
* Identify a group by optional `id` and `traits`. Or, if no arguments are
146+
* supplied, return the current group.
147+
*/
148+
group(
149+
id: string,
150+
traits: unknown,
151+
options: unknown,
152+
fn: unknown
153+
): AnalyticsJS;
154+
155+
/**
156+
* Track an `event` that a user has triggered with optional `properties`.
157+
*/
158+
track(
159+
event: string,
160+
properties: unknown,
161+
options: unknown,
162+
fn: unknown
163+
): AnalyticsJS;
164+
165+
/**
166+
* Helper method to track an outbound link that would normally navigate away
167+
* from the page before the analytics calls were sent.
168+
*
169+
* BACKWARDS COMPATIBILITY: aliased to `trackClick`.
170+
*/
171+
trackClick(
172+
links: Element | Array<unknown>,
173+
event: any,
174+
properties?: any
175+
): AnalyticsJS;
176+
177+
/**
178+
* Helper method to track an outbound link that would normally navigate away
179+
* from the page before the analytics calls were sent.
180+
*
181+
* BACKWARDS COMPATIBILITY: aliased to `trackClick`.
182+
*/
183+
trackLink(
184+
links: Element | Array<unknown>,
185+
event: any,
186+
properties?: any
187+
): AnalyticsJS;
188+
189+
/**
190+
* Set the user's `id`.
191+
*/
192+
setAnonymousId(id: string): AnalyticsJS;
193+
194+
/**
195+
* Helper method to track an outbound form that would normally navigate away
196+
* from the page before the analytics calls were sent.
197+
*
198+
* BACKWARDS COMPATIBILITY: aliased to `trackSubmit`.
199+
*/
200+
trackSubmit(
201+
forms: Element | Array<unknown>,
202+
event: any,
203+
properties?: any
204+
): AnalyticsJS;
205+
206+
/**
207+
* Helper method to track an outbound form that would normally navigate away
208+
* from the page before the analytics calls were sent.
209+
*
210+
* BACKWARDS COMPATIBILITY: aliased to `trackSubmit`.
211+
*/
212+
trackForm(
213+
forms: Element | Array<unknown>,
214+
event: any,
215+
properties?: any
216+
): AnalyticsJS;
217+
218+
/**
219+
* Trigger a pageview, labeling the current page with an optional `category`,
220+
* `name` and `properties`.
221+
*/
222+
page(
223+
category: string,
224+
name: string,
225+
properties: any,
226+
options: any,
227+
fn: unknown
228+
): AnalyticsJS;
229+
230+
/**
231+
* Merge two previously unassociated user identities.
232+
*/
233+
alias(
234+
to: string,
235+
from?: string,
236+
options?: unknown,
237+
fn?: unknown
238+
): AnalyticsJS;
239+
240+
/**
241+
* Register a `fn` to be fired when all the analytics services are ready.
242+
*/
243+
ready(fn: Function): AnalyticsJS;
244+
245+
/**
246+
* Set the `timeout` (in milliseconds) used for callbacks.
247+
*/
248+
timeout(timeout: number): void;
249+
250+
/**
251+
* Enable or disable debug.
252+
*/
253+
debug(str: string | boolean): void;
254+
255+
/**
256+
* Reset group and user traits and id's.
257+
*/
258+
reset(): void;
259+
260+
/**
261+
* Normalize the given `msg`.
262+
*/
263+
normalize(msg: { context: { page }; anonymousId: string }): object;
264+
265+
/**
266+
* No conflict support.
267+
*/
268+
noConflict(): AnalyticsJS;
269+
}
270+
}
271+
272+
declare var analytics: SegmentAnalytics.AnalyticsJS;
273+
274+
declare module '@segment/analytics.js-core' {
275+
var analytics: SegmentAnalytics.AnalyticsJS;
276+
export default analytics;
277+
}

0 commit comments

Comments
 (0)