Nondeterministic Programming Methodology

    @segment/analytics.js-core
    TypeScript icon, indicating that this package has built-in type declarations

    4.1.11 • Public • Published

    analytics.js-core

    CircleCI Codecov

    This is the core of Analytics.js, the open-source library that powers data collection at Segment.

    To build this into a full, usable library, see the Analytics.js repository.

    Using Types (v.4.0.0-beta.0 and later)

    We 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.

    Importing as an npm module

    If you use analytics.js-core as an npm module, you can use its types out of the box: Example of Types usage in Analytics JS

    Using types with the AJS Snippet

    If 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).

    To use types with the snippet, add analytics as part of the global module. Something like this:

    import { SegmentAnalytics } from '@segment/analytics.js-core';
    
    declare global {
      interface Window {
        analytics: SegmentAnalytics.AnalyticsJS;
      }
    }

    Using as a standalone npm package

    We 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:

    1- Install the dependencies

    yarn add @segment/analytics.js-core
    yarn add @segment/analytics.js-integration-segmentio
    // you may need this depending on the bundler
    yarn add uuid@^3.4 
    

    2- Import the dependencies

    import Analytics from "@segment/analytics.js-core/build/analytics";
    import SegmentIntegration from "@segment/analytics.js-integration-segmentio";

    3- Initialize Segment and add Segment's own integration

    // instantiate the library
    const analytics = new Analytics();
    
    // add Segment's own integration ( or any other device mode integration ) 
    analytics.use(SegmentIntegration);
    
    // define the integration settings object. 
    // Since we are using only Segment integration in this example, we only have 
    // "Segment.io" in the integrationSettings object
    const integrationSettings = {
      "Segment.io": {
        apiKey: "<YOUR SEGMENT WRITE KEY>",
        retryQueue: true,
        addBundledMetadata: true
      }
    };
    
    
    // Initialize the library
    analytics.initialize(integrationSettings);
    
    // Happy tracking! 
    analytics.track('🚀');

    License

    Released under the MIT license.

    Install

    npm i @segment/analytics.js-core

    DownloadsWeekly Downloads

    33,776

    Version

    4.1.11

    License

    SEE LICENSE IN LICENSE

    Unpacked Size

    189 kB

    Total Files

    56

    Last publish

    Collaborators

    • song4you
    • peterdemartini
    • emmy.byrne
    • ashkon
    • vincen7tran
    • ariel.silvestri
    • xinghaohuang
    • dean-huynh
    • cdignam-segment
    • bkambo
    • abhinavsureka
    • rikezatsegment
    • arunlalam-segment
    • cjradek
    • neeharikakondipati
    • simpixelated
    • chihchun-twilio
    • sangeeta.singh
    • acharles14
    • jyim008
    • seghungtran
    • alayvora
    • tv0
    • pchen-twilio
    • hema-segment
    • sudojatin
    • mkhan-twilio
    • mayberex
    • anandjha
    • pmunin
    • oscb
    • krousseau
    • brandongregoryscott
    • knksmith57
    • sachinwathore
    • fhalim-segment
    • cfree
    • mettledrum
    • 5t0k4st1k
    • maerf0x0
    • gnijor
    • ifonseka1
    • aniket.gupta
    • hjoonpm
    • lauramunozjimenez
    • celine-segment
    • parsa-segment
    • hmorgan94
    • pmcanseco-segment
    • ynguyen
    • mshwery
    • debajitr
    • masira
    • gpsamson
    • amillet89
    • cholt002
    • av-segment
    • aghotikar
    • vikrant-segment
    • ankur.agarwal
    • larryatsegment
    • lbrink
    • shivpoojan-segment
    • ariel_segment
    • zkuzmic-segment
    • scruwys1
    • sowjanyaedara
    • chrischalstrom
    • rossedfort
    • eesegment
    • lew-gordon
    • kyliepedersen
    • jinapark
    • skntwilio
    • segmentio
    • segment-admin
    • dominicbarnes
    • shobhita-agarwal
    • deanhuynh
    • jlineaweaver
    • ladanazita
    • anoonan
    • peripheral
    • achille-roussel
    • rajulvadera
    • nettofarah
    • dalchandc
    • lpediredla
    • albert.segment
    • segment-danielstjules
    • n2parko
    • segment-andy-yeo
    • sahilp
    • psankaranarayana
    • vdemedes
    • emilio-gomez-lavin
    • andreiko_ru
    • xagos
    • leifdreizler
    • alan-segment
    • tyson_segment
    • bgamwell
    • jfabre-segment
    • uditmehta
    • hellooimkat
    • brienne.mcnally
    • sanscontext
    • eculver
    • aultimus
    • salolivares
    • erikdw
    • chenxiangzhang
    • fauzyy
    • jaimal
    • davidbirdsong
    • ktrinh
    • calthomson
    • yunqiaohuang
    • dk1027
    • nielssegment
    • cdrycroft
    • yabrira
    • julee05
    • kamebkj
    • ryan_segment
    • laurenmreeder
    • tidothegreat
    • mericsson
    • prayansh-segmentt
    • stargaesser
    • andyguwc
    • jeremylarkin
    • bsneed
    • danieljackins
    • tomeliaz
    • segment-seth
    • bihl
    • shamil.ataev
    • james9446
    • priscilla.giatti
    • kellylu
    • smokeybears
    • jmbuckner
    • benhorowitz
    • yolken-segment
    • nlsun
    • thomas-pelletier-segment
    • brian-segment
    • segment-michael
    • cohara87
    • drew-thompson
    • segment-jsingh
    • vanesng
    • cvillela
    • pauljaeinyoo
    • mniehe
    • stephment
    • sharadbhadouria
    • emilyc
    • maggieyu
    • anoonan16
    • durgani
    • julio.farah
    • hblanks-segment
    • rsata
    • shyaan
    • juhaelee
    • akleiner2
    • kathryn-taylor
    • andrius-segment
    • pooyaj
    • llbsgmt
    • lab176
    • valerieernst
    • ucarion
    • solon.aguiar
    • rjenkinsseg
    • segment-ulysse
    • khinkalilover
    • maloneya
    • zackurey
    • kelcook
    • arta.razavi
    • odoren
    • alistairbarrell
    • anna.choi
    • whamo12
    • amaloney
    • gilomer
    • kiara.daswani
    • marcelopv
    • yannieyip
    • eric.rogner
    • marinhero
    • lcamposg
    • corey.ching
    • steve_at_segment
    • tepahk
    • sorrel.j
    • cjo2
    • fc-segment
    • kdharaiya
    • warrengreen
    • jon.anderson-at-segment.com
    • markzegarelli
    • stysegment
    • ajhenry
    • stacy.song
    • niveda.balananthan
    • collinvandyck
    • krlv
    • rexatsegment
    • nickaguilar
    • tri.truong
    • csayuso
    • joyce-shi
    • bradenbecker
    • neha.sanghrajka
    • reneewang
    • dan.lasky
    • sam.tapia
    • gbbastos
    • vikramkumar19
    • mpriyad25
    • peter.walker
    • jeremy.parker
    • khamidou-segment
    • smidges
    • daltonscharff