Skip to content

Commit

Permalink
fix code generation (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
flashdesignory committed Aug 2, 2024
1 parent 289134e commit 3f1260a
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/third-parties/google-analytics/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/third-parties/google-tag-manager/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
6 changes: 3 additions & 3 deletions src/utils/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand Down Expand Up @@ -300,7 +300,7 @@ describe('Utils', () => {
const inputs = [
// string
{
input: 'window[{{l}}]=window[{{l}}]||[];',
input: 'window["{{l}}"]=window["{{l}}"]||[];',
params: {
l: 'some-datalayer',
},
Expand Down Expand Up @@ -356,7 +356,7 @@ describe('Utils', () => {

expect(
formatCode(input, { l: 'test' }, { l: 'dataLayer' }),
).toMatchInlineSnapshot(`"window["test"]=window["test"]||[];"`);
).toMatchInlineSnapshot(`"window[test]=window[test]||[];"`);
});
});
});
4 changes: 1 addition & 3 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
}

Expand Down

0 comments on commit 3f1260a

Please sign in to comment.