# @segment/snippet

> Templating methods for rendering the analytics.js snippet.

Latest version **5.2.1** (published 2024-01-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install @segment/snippet
pnpm add @segment/snippet
yarn add @segment/snippet
bun add @segment/snippet
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 5.2.1 |
| Published | 2024-01-20 |
| First published | 2016-09-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 16 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 52 |
| Author | Segment.io |
| Maintainers | chenchensegmentcom, sanket.mishra, lweimersegment, rcheedhalla, jxin_twilio, odoren_segment, aditi.raveesh, dltnbrks-segment, sai-patanjali, himanshuph, alecjacobs-segment, sshaikh_segment, john.lee1100, dazu70, brian.aguirre, pooja.patil, rjheeta, segment_fan, nlubchenco, jamezhutwilio, knksmith57-segment, jsh-wu, nithin-benny, poojasegment, ebru.odok, bannapple, rodhilton_twilio, guthriesegment, srishti-nema, cdelaomartinez, miguelpdiaz8, sundareswar.jayakumar, gbatra, spencerattick, akodankiry, nanette.ranes, gsolis_segment, nitinpm432, aadityabhatt10, amigandhi, segmentsean, hmohanram_seg, jrupasinghe, myrontin.segment, devthale, smccoy-twilio, seg-leonelsanches, jibrang, sethgrid_segment, light-bringer-blr, aubreysine, ed-twilion-npm, harsh-joshi99, irfan.ali.segment, kbhargavaram-sg, needcaffeine, nat-grid, spandananune92, wlumsegment, vivek-26, moyara2, bala.singareddy, gbbastos, sprasannasastry22, akash.gautam07, preetyp, viveksainaneesegment, msaraf, kjoerres, rokatyal, ainatancinco, anton-vylushchak, sowjanyasegment, alayvora, pzawadzki, msaunders-segment, tw-dgarcia, mbreske, nsandhu-segment, parag.panda, blangtwilio, ryanrouleau-segment, twjosiah, mcullenmeyer, nina-y, david.anusontarangkul.segment, mckern_segment, segment-admin, nainy.agrawal, tdibacco, sudojatin, nageshgolem, brandonheyer-segment, teganduong, alfrimpong, dobrin.ganev, ankit.gupta.unthinkable, marinhero, benattwilio, bharath.boregowda, conniechen, sungju.jin, pooyaj, yli119, ea_segment, emilyjia, kx-segment, cvillela_segment, xinghao.huang, harsh.vardhan, joe.ayoub.segment, gkochar123, rollcode, ariel.silvestri, cherylj-segment, leifdreizler, immanoj, aaronklish, michelr, maneesh.dharma29, msolorzano-segment, brianhumphreystwilio, jfehrman.segment, joetessy, pmunin, jalexy12, jbandi-twilio, prayansh-twilio, dominicbarnes, brandon.scott-segment, bgillan, phillip.thomas, ricardo.rossi, forgetfulfellow, fauzy.yy, mayur-pitale, dbaik-twilio-segment, seg-rustybailey, tanya.gupta.segment, pmiller-twilio, nevermore2022, aishikawaki, csayuso, mcoulibali, shupadhyay, swgupta, sumit1606, jahood-twilio, saisagarkappaganthula, dostwal, rmukundan, ngoldman-twilio, apareek-twilio, arubiochavez, shuvrajit9904, s3gm3nt, ama0223, tbrennanj, dangmai-segment, shraddha-twilio, taricjain, snehauppalapati, azhaotwilio, ryder.wendt, sa-jsooter, enyi.asonye, afsha-nazim-seg, mschaszberger, lnamba, varadarajan-tw, seanhan-segment, replatero, sayan-das-in, justeen, sausingh, jgabe13, haeyoon.paik, meg1000, funlu, ashwitha.bg, whaider_twilio, tcgilbert, kevinburkesegment, felttrip, prabhnoor1997, akashyap91, connormarchand, clintz.seg, karimkeshwani, wally.tg, rhall-twilio, yash-twilio, brookstaylorjr, shayan-golafshani, lerahulram, shpankau, mugelstad, hdamani, brchristy, rrivera-segment, sethnutetwilio, fplinke, manali-bhosale, chtoombs, sethsegment, eric-hyde, elmoselyee, michaelghseg, jayakrishnannair, lateefat, maryam.sharif, wdbetts, ryanligon, sindhusegment, lfdelossantos, aramakrishnan, segment-camden, vbatanov, lluque-twilio, jair.aviles, pmaid, song4you, peterdemartini, emmy.byrne, vincen7tran, dean-huynh, cdignam-segment, abhinavsureka, arunlalam-segment, cjradek, neeharikakondipati, simpixelated, chihchun-twilio, acharles14, jyim008, seghungtran, hema-segment, oscb, krousseau, sachinwathore, fhalim-segment, cfree, hjoonpm, celine-segment, pmcanseco-segment, masira, amillet89, cholt002, av-segment, aghotikar, vikrant-segment, larryatsegment, ariel_segment, scruwys1, lew-gordon, kyliepedersen, jinapark, segmentio, rajulvadera, lpediredla, n2parko, tyson_segment, bgamwell, uditmehta, salolivares, erikdw, chenxiangzhang, mericsson, prayansh-segmentt, jeremylarkin, bsneed, danieljackins, segment-seth, james9446, priscilla.giatti, kellylu, nlsun, drew-thompson, segment-jsingh, andrius-segment, valerieernst, khinkalilover, kelcook, gilomer, marcelopv, eric.rogner, kdharaiya, jon.anderson-at-segment.com, stacy.song, niveda.balananthan, rexatsegment, nickaguilar, bradenbecker, reneewang, dan.lasky, sam.tapia, vikramkumar19, mpriyad25, jeremy.parker, smidges |

## Links

- npm: https://www.npmjs.com/package/@segment/snippet
- Repository: https://github.com/segmentio/snippet
- Homepage: https://github.com/segmentio/snippet#readme
- Issues: https://github.com/segmentio/snippet/issues
- npm.io page: https://npm.io/package/@segment/snippet

## Dependencies (1)

- [@ndhoule/map](https://npm.io/package/@ndhoule/map.md) ^2.0.1

## Recent versions

- 5.2.1 (latest) — 2024-01-20
- 5.2.0 — 2023-12-04
- 5.1.0 — 2023-12-04
- 5.0.1 — 2023-11-29
- 5.0.0 — 2023-11-28
- 4.16.2 — 2023-06-22
- 4.16.1 — 2023-06-22
- 4.16.0 — 2023-04-20
- 4.15.3 — 2021-09-20
- 4.15.2 — 2021-09-20
- 4.15.1 — 2021-09-20
- 4.15.0 — 2021-09-16
- 4.14.2 — 2021-07-06
- 4.14.1 — 2021-07-05
- 4.14.0 — 2021-06-30
- … 16 more at https://npm.io/package/@segment/snippet/versions

## README

# @segment/snippet

Render the analytics.js snippet.

The recommended way to use analytics.js is to follow the [analytics.js quickstart guide](https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/quickstart/). If you absolutely need to generate a snippet dynamically, this is an alternate solution. Note that when using this in-browser, the global `analytics` object will not be defined until the snippet is rendered and executed.

For information on browser support, see: https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/supported-browsers/

## Installation
```sh
# npm
npm install @segment/snippet

# yarn
yarn add @segment/snippet

# pnpm
pnpm add @segment/snippet
```
## Example

```js
const snippet = require('@segment/snippet');

const contents = snippet.max({
  host: 'cdn.segment.com',
  apiKey: '03fwkuu3',
  page: {
    category: 'Docs',
    name: 'Integrations',
    properties: {
      foo: 'bar'
    }
  }
});
```

## API

### snippet.max(options)

Returns the maxified version of the analytics.js snippet given a set of `options`:

* `host`: the domain name where the analytics.js script is hosted.
* `useHostForBundles`: If set to `true`, the snippet will include the `_cdn` property to tell analytics.js where to fetch bundles from.
* `apiKey`: the `apiKey` to load in the snippet.
* `page`: the options to pass to `analytics.page`. if `page` is `false`, then the `page()` call will be omitted.
* `load`: If object, these are the settings passed as the second argument to analytics.load. This can be useful if you want to override Segment.io integration behavior, or if you want dynamically control which integraions load on the client-side for things like GDPR. If set to `false` the `load()` call will be omitted.
* `ajsPath`: override the default analytics.min.js location


### snippet.min(options)

Returns the minified version of the snippet.


## Development

### Installation + QA
```
nvm use
yarn install
make lint
make test
```

### Running tests in Saucelabs
```
SAUCE=true make test
```

## Releasing
1. Publish to `npm`
```sh
git checkout master && git pull --ff-only
npm version <patch|minor|major>
git push --follow-tags
make build
npm publish
```
2. Create a [new github release](https://github.com/segmentio/snippet/releases).

3. Bump package version on [segmentio/app](https://github.com/segmentio/app/blob/main/packages/app/package.json).

4. Update _all_ example snippets on [public docs repo](https://github.com/segmentio/segment-docs) via search + replace 
- Get example snippet by runnings `yarn fixture` and observing generated `tmp.fixture.*.js` files. 
- Tip: double-check that the fixture's `SNIPPET_VERSION` refers to the new npm version.

---
_Source: https://npm.io/package/@segment/snippet · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
