UNPKG

3.25 kBMarkdownView Raw
1# analytics.js-core
2
3[![CircleCI](https://circleci.com/gh/segmentio/analytics.js-core.svg?style=shield)](https://circleci.com/gh/segmentio/analytics.js-core)
4[![Codecov](https://img.shields.io/codecov/c/github/segmentio/analytics.js-core/master.svg)](https://codecov.io/gh/segmentio/analytics.js-core)
5
6This is the core of [Analytics.js](https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/), the open-source library that powers data collection at [Segment](https://segment.com).
7
8To build this into a full, usable library, see the [Analytics.js](https://github.com/segmentio/analytics.js) repository.
9
10## Using Types (v.4.0.0-beta.0 and later)
11
12We recently introduced Typescript support and types to Analytics.js Core. While the exposed types still need some work (pull requests are welcome!), they're ready to be used.
13
14### Importing as an npm module
15
16If you use analytics.js-core as an npm module, you can use its types out of the box:
17<img src="https://user-images.githubusercontent.com/484013/89060070-2e235f00-d317-11ea-9fd9-e1c77aaca9f9.gif" alt="Example of Types usage in Analytics JS" width="500px">
18
19### Using types with the AJS Snippet
20
21If you create a source at https://app.segment.com, Segement automatically generates a JS snippet that you can add to your website. (for more information visit our [documentation](https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/quickstart/)).
22
23To use types with the snippet, add `analytics` as part of the global module.
24Something like this:
25
26```typescript
27import { SegmentAnalytics } from '@segment/analytics.js-core';
28
29declare global {
30 interface Window {
31 analytics: SegmentAnalytics.AnalyticsJS;
32 }
33}
34```
35
36## Using as a standalone `npm` package
37We recommend using the CDN version of `analytics.js` as it offers all the project and workspace specific settings, enabled integrations, and middleware. But if you prefer to use `analytics.js-core` as a standalone npm package using your own tooling & workflow, you can do the following:
38
391- Install the dependencies
40```
41yarn add @segment/analytics.js-core
42yarn add @segment/analytics.js-integration-segmentio
43// you may need this depending on the bundler
44yarn add uuid@^3.4
45```
46
472- Import the dependencies
48```javascript
49import Analytics from "@segment/analytics.js-core/build/analytics";
50import SegmentIntegration from "@segment/analytics.js-integration-segmentio";
51```
52
533- Initialize Segment and add Segment's own integration
54```javascript
55// instantiate the library
56const analytics = new Analytics();
57
58// add Segment's own integration ( or any other device mode integration )
59analytics.use(SegmentIntegration);
60
61// define the integration settings object.
62// Since we are using only Segment integration in this example, we only have
63// "Segment.io" in the integrationSettings object
64const integrationSettings = {
65 "Segment.io": {
66 apiKey: "<YOUR SEGMENT WRITE KEY>",
67 retryQueue: true,
68 addBundledMetadata: true
69 }
70};
71
72
73// Initialize the library
74analytics.initialize(integrationSettings);
75
76// Happy tracking!
77analytics.track('🚀');
78```
79
80## License
81
82Released under the [MIT license](LICENSE).
83
84[analytics.js]: https://segment.com/docs/libraries/analytics.js/