Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 + ');';
}
Comment on lines +90 to +94
Copy link
Contributor

@juliofarah juliofarah Jun 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to be fully compliant with the typescript definitions added on #55 it'd be great if we could change the type of load?: boolean to something like

type LoadOptions = boolean | { ... }

// ...
load?: LoadOptions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added LoadOptions. Here is my IDE suggesting the All member. I added this one explicitly as it's a known property but can be omitted if preferred.

image


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