From 3f1260a81f0d6a02041438456751d6dcdc97e14e Mon Sep 17 00:00:00 2001 From: Thorsten Kober Date: Fri, 2 Aug 2024 16:16:12 -0400 Subject: [PATCH] fix code generation (#62) --- src/third-parties/google-analytics/data.json | 2 +- src/third-parties/google-tag-manager/data.json | 2 +- src/utils/index.test.ts | 6 +++--- src/utils/index.ts | 4 +--- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/third-parties/google-analytics/data.json b/src/third-parties/google-analytics/data.json index 92ee4af..e032f16 100644 --- a/src/third-parties/google-analytics/data.json +++ b/src/third-parties/google-analytics/data.json @@ -15,7 +15,7 @@ "key": "gtag" }, { - "code": "window[{{l}}]=window[{{l}}]||[];window[{{l}}].push({'js':new Date()});window[{{l}}].push({'config':{{id}}})", + "code": "window['{{l}}']=window['{{l}}']||[];window['gtag-{{l}}']=function (){window['{{l}}'].push(arguments);};window['gtag-{{l}}']('js',new Date());window['gtag-{{l}}']('config','{{id}}')", "params": ["id"], "optionalParams": { "l": "dataLayer" diff --git a/src/third-parties/google-tag-manager/data.json b/src/third-parties/google-tag-manager/data.json index 50447b6..6c5667f 100644 --- a/src/third-parties/google-tag-manager/data.json +++ b/src/third-parties/google-tag-manager/data.json @@ -15,7 +15,7 @@ "key": "gtm" }, { - "code": "window[{{l}}]=window[{{l}}]||[];window[{{l}}].push({'gtm.start':new Date().getTime(),event:'gtm.js'});", + "code": "window['{{l}}']=window['{{l}}']||[];window['{{l}}'].push({'gtm.start':new Date().getTime(),event:'gtm.js'});", "optionalParams": { "l": "dataLayer" }, diff --git a/src/utils/index.test.ts b/src/utils/index.test.ts index c98fbfe..1e3038f 100644 --- a/src/utils/index.test.ts +++ b/src/utils/index.test.ts @@ -268,7 +268,7 @@ describe('Utils', () => { }, scripts: [ { - code: 'window[{{hello}}]=window[{{hello}}]||[];console.log({{world}})', + code: 'window["{{hello}}"]=window["{{hello}}"]||[];console.log("{{world}}")', optionalParams: { hello: 'hoho', }, @@ -300,7 +300,7 @@ describe('Utils', () => { const inputs = [ // string { - input: 'window[{{l}}]=window[{{l}}]||[];', + input: 'window["{{l}}"]=window["{{l}}"]||[];', params: { l: 'some-datalayer', }, @@ -356,7 +356,7 @@ describe('Utils', () => { expect( formatCode(input, { l: 'test' }, { l: 'dataLayer' }), - ).toMatchInlineSnapshot(`"window["test"]=window["test"]||[];"`); + ).toMatchInlineSnapshot(`"window[test]=window[test]||[];"`); }); }); }); diff --git a/src/utils/index.ts b/src/utils/index.ts index bfb30be..44ef357 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -56,9 +56,7 @@ export function formatCode( ) { return code.replace(/{{(.*?)}}/g, (match) => { const name = match.split(/{{|}}/).filter(Boolean)[0]; - return JSON.stringify( - args && name in args ? args?.[name] : optionalParams?.[name], - ); + return args?.[name] !== undefined ? args?.[name] : optionalParams?.[name]; }); }