Skip to content

Commit

Permalink
Add load options (#66)
Browse files Browse the repository at this point in the history
* Support load options

* Add load options type

Co-authored-by: Julio Farah <julio.farah@segment.com>
  • Loading branch information
mzogheib and Julio Farah authored Jul 6, 2021
1 parent 7013654 commit 0b29331
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,11 @@ function renderPage(page) {
function renderLoad(settings) {
if (!settings.load) return '';

if (typeof settings.load !== 'boolean') {
// eslint-disable-next-line no-restricted-globals
var loadOptions = JSON.stringify(settings.load);
return 'analytics.load("' + settings.apiKey + '", ' + loadOptions + ');';
}

return 'analytics.load("' + settings.apiKey + '");';
}
12 changes: 12 additions & 0 deletions test/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ describe('snippet', function() {
'analytics.load("key")');
});

it('should set the load options', function() {
assertContains(
snippet.max({ apiKey: 'key', load: { integrations: { All: false } } }),
'analytics.load("key", {"integrations":{"All":false}})');
});

it('should set the _writekey', function() {
assertContains(
snippet.max({ apiKey: 'foo' }),
Expand Down Expand Up @@ -101,6 +107,12 @@ describe('snippet', function() {
'analytics.load("key")');
});

it('should set the load options', function() {
assertContains(
snippet.max({ apiKey: 'key', load: { integrations: { All: false } } }),
'analytics.load("key", {"integrations":{"All":false}})');
});

it('should set the _writekey', function() {
assertContains(
snippet.min({ apiKey: 'foo' }),
Expand Down
9 changes: 8 additions & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ declare module '@segment/snippet' {
}
}

interface LoadOptions {
integrations?: {
All?: boolean
[key: string]: boolean
}
}

interface Options {
/** The domain name where the analytics.js script is hosted. */
host?: string
Expand All @@ -23,7 +30,7 @@ declare module '@segment/snippet' {
* you want dynamically control the load process on the client-side for
* things like GDPR.
*/
load?: boolean
load?: boolean | LoadOptions
}

/**
Expand Down

0 comments on commit 0b29331

Please sign in to comment.