From 0b2933156a33332c79d7e0453cbab25e4d6aa50d Mon Sep 17 00:00:00 2001 From: Marwan Zogheib Date: Wed, 7 Jul 2021 05:50:33 +1000 Subject: [PATCH] Add load options (#66) * Support load options * Add load options type Co-authored-by: Julio Farah --- lib/index.js | 6 ++++++ test/render.test.js | 12 ++++++++++++ types.d.ts | 9 ++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 892da77..31bfe93 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 + '");'; } diff --git a/test/render.test.js b/test/render.test.js index 03f1e2f..bbcae11 100644 --- a/test/render.test.js +++ b/test/render.test.js @@ -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' }), @@ -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' }), diff --git a/types.d.ts b/types.d.ts index f1fba22..eb9e4ec 100644 --- a/types.d.ts +++ b/types.d.ts @@ -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 @@ -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 } /**