Skip to content

Commit 807c252

Browse files
author
Matej Lednicky
committed
feat(DIST-699): Pass default value for source and medium
Default source is domain (without the www) and medium is 'embed-sdk'.
1 parent 3f3ea64 commit 807c252

14 files changed

+79
-65
lines changed

packages/demo-html/public/popup-html.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</style>
1313
</head>
1414
<body>
15-
<button id="button" data-tf-popup="moe6aa" data-tf-source="localhost">toggle</button>
15+
<button id="button" data-tf-popup="moe6aa" data-tf-medium="unit-test">toggle</button>
1616
<script src="./lib/embed-next.js"></script>
1717
</body>
1818
</html>

packages/demo-html/public/popup-js.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
<button id="button">toggle</button>
1717
<script src="./lib/embed-next.js"></script>
1818
<script>
19-
const { open, close } = window.tf.createPopup("moe6aa", { source: "localhost" });
20-
let isOpen = false;
19+
const { open, close } = window.tf.createPopup("moe6aa", { medium: "unit-test" });
2120
document.getElementById("button").onclick = function () {
2221
const isOpen = !!document.querySelector(".typeform-popup");
2322
isOpen ? close() : open();

packages/demo-html/public/widget-html.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</style>
1313
</head>
1414
<body>
15-
<div id="wrapper" data-tf-widget="moe6aa" data-tf-source="localhost"></div>
15+
<div id="wrapper" data-tf-widget="moe6aa" data-tf-medium="unit-test"></div>
1616
<script src="./lib/embed-next.js"></script>
1717
</body>
1818
</html>

packages/demo-html/public/widget-js.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<div id="wrapper"></div>
1717
<script src="./lib/embed-next.js"></script>
1818
<script>
19-
window.tf.createWidget("moe6aa", { container: document.getElementById("wrapper"), source: "localhost" });
19+
window.tf.createWidget("moe6aa", { container: document.getElementById("wrapper"), medium: "unit-test" });
2020
</script>
2121
</body>
2222
</html>

packages/embed/e2e/spec/functional/popup-window.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Popup', () => {
2323
it('should pass options as query param', () => {
2424
cy.get('.typeform-popup iframe')
2525
.invoke('attr', 'src')
26-
.should('contain', 'typeform-embed=popup-blank&typeform-source=localhost')
26+
.should('contain', 'typeform-embed=popup-blank&typeform-source=localhost&typeform-medium=unit-test')
2727
})
2828

2929
it('should close popup', () => {

packages/embed/e2e/spec/functional/popup.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('Popup', () => {
1616
it('should pass options as query param', () => {
1717
cy.get('.typeform-popup iframe')
1818
.invoke('attr', 'src')
19-
.should('contain', 'typeform-embed=popup-blank&typeform-source=localhost')
19+
.should('contain', 'typeform-embed=popup-blank&typeform-source=localhost&typeform-medium=unit-test')
2020
})
2121

2222
it('should close popup', () => {

packages/embed/e2e/spec/functional/widget-window.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ describe('Widget Window', () => {
1818
it('should pass options as query param', () => {
1919
cy.get('.typeform-widget iframe')
2020
.invoke('attr', 'src')
21-
.should('contain', 'typeform-embed=embed-widget&typeform-source=localhost')
21+
.should('contain', 'typeform-embed=embed-widget&typeform-source=localhost&typeform-medium=unit-test')
2222
})
2323
})

packages/embed/e2e/spec/functional/widget.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ describe('Widget', () => {
1111
it('should pass options as query param', () => {
1212
cy.get('.typeform-widget iframe')
1313
.invoke('attr', 'src')
14-
.should('contain', 'typeform-embed=embed-widget&typeform-source=localhost')
14+
.should('contain', 'typeform-embed=embed-widget&typeform-source=localhost&typeform-medium=unit-test')
1515
})
1616
})

packages/embed/src/utils/build-iframe-src.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
import { EmbedType, UrlOptions } from '../base'
22

3+
import { removeUndefinedKeys } from './remove-undefined-keys'
4+
5+
const defaultUrlOptions: UrlOptions = {
6+
source: window?.location?.hostname.replace(/^www\./, ''),
7+
medium: 'embed-sdk',
8+
}
9+
10+
const addDefaultUrlOptions = (options: UrlOptions): UrlOptions => {
11+
return { ...defaultUrlOptions, ...removeUndefinedKeys(options) }
12+
}
13+
314
const typesToEmbed: Record<EmbedType, string> = {
415
widget: 'embed-widget', // TODO: when widget is full page use 'embed-fullpage'
516
popup: 'popup-blank',
@@ -8,7 +19,8 @@ const typesToEmbed: Record<EmbedType, string> = {
819
'side-tab': 'popup-side-panel',
920
}
1021

11-
const mapOptionsToQueryParams = (type: EmbedType, options: UrlOptions): Record<string, any> => ({
22+
const mapOptionsToQueryParams = (type: EmbedType, embedId: string, options: UrlOptions): Record<string, any> => ({
23+
'typeform-embed-id': embedId,
1224
'typeform-embed': typesToEmbed[type],
1325
'typeform-source': options.source,
1426
'typeform-medium': options.medium,
@@ -22,19 +34,14 @@ const mapOptionsToQueryParams = (type: EmbedType, options: UrlOptions): Record<s
2234
export const buildIframeSrc = (params: BuildIframeSrcOptions): string => {
2335
const { formId, type, embedId, options } = params
2436

25-
const url = new URL(`https://form.typeform.com/to/${formId}`)
37+
const queryParams = mapOptionsToQueryParams(type, embedId, addDefaultUrlOptions(options))
2638

27-
if (embedId) {
28-
url.searchParams.set('typeform-embed-id', embedId)
29-
}
30-
31-
const queryParams = mapOptionsToQueryParams(type, options)
32-
Object.entries(queryParams).forEach(([paramName, paramValue]) => {
33-
if (!paramValue) {
34-
return
35-
}
36-
url.searchParams.set(paramName, paramValue)
37-
})
39+
const url = new URL(`https://form.typeform.com/to/${formId}`)
40+
Object.entries(queryParams)
41+
.filter(([, paramValue]) => paramValue != null)
42+
.forEach(([paramName, paramValue]) => {
43+
url.searchParams.set(paramName, paramValue)
44+
})
3845

3946
return url.href
4047
}

packages/embed/src/utils/build-iframe.src.spec.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,41 @@ import { buildIframeSrc } from './build-iframe-src'
33
describe('build-iframe-src', () => {
44
describe('#buildIframeSrc', () => {
55
it('should return iframe src', () => {
6-
expect(buildIframeSrc({ formId: 'some-id', type: 'widget', embedId: '', options: {} })).toBe(
7-
'https://form.typeform.com/to/some-id?typeform-embed=embed-widget'
6+
expect(buildIframeSrc({ formId: 'some-id', type: 'widget', embedId: '', options: {} })).toMatch(
7+
'https://form.typeform.com/to/some-id'
88
)
99
})
1010

11-
it('should include url options', () => {
12-
expect(
13-
buildIframeSrc({ formId: 'some-id', type: 'widget', embedId: '', options: { source: 'unit-test-source' } })
14-
).toBe('https://form.typeform.com/to/some-id?typeform-embed=embed-widget&typeform-source=unit-test-source')
11+
it('should include default url options', () => {
12+
expect(buildIframeSrc({ formId: 'some-id', type: 'widget', embedId: 'embed-id', options: {} })).toBe(
13+
'https://form.typeform.com/to/some-id' +
14+
'?typeform-embed-id=embed-id' +
15+
'&typeform-embed=embed-widget' +
16+
'&typeform-source=localhost' +
17+
'&typeform-medium=embed-sdk'
18+
)
19+
})
20+
21+
it('should override default url options if value is explicitly supplied', () => {
22+
const src = buildIframeSrc({
23+
formId: 'some-id',
24+
type: 'widget',
25+
embedId: 'id',
26+
options: { medium: 'unit-test-medium', source: 'unit-test-source' },
27+
})
28+
expect(src).toMatch('typeform-medium=unit-test-medium')
29+
expect(src).toMatch('typeform-source=unit-test-source')
1530
})
1631

1732
it('should omit false url options', () => {
18-
expect(
19-
buildIframeSrc({
20-
formId: 'some-id',
21-
type: 'widget',
22-
embedId: '',
23-
options: { hideFooter: true, hideHeaders: false },
24-
})
25-
).toBe('https://form.typeform.com/to/some-id?typeform-embed=embed-widget&embed-hide-footer=true')
33+
const src = buildIframeSrc({
34+
formId: 'some-id',
35+
type: 'widget',
36+
embedId: '',
37+
options: { hideFooter: true, hideHeaders: false },
38+
})
39+
expect(src).toMatch('embed-hide-footer=true')
40+
expect(src).not.toMatch('embed-hide-headers')
2641
})
2742

2843
it('should include all url options', () => {
@@ -35,8 +50,10 @@ describe('build-iframe-src', () => {
3550
opacity: 50,
3651
disableTracking: true,
3752
}
38-
expect(buildIframeSrc({ formId: 'some-id', type: 'widget', embedId: '', options })).toBe(
39-
'https://form.typeform.com/to/some-id?typeform-embed=embed-widget' +
53+
expect(buildIframeSrc({ formId: 'some-id', type: 'widget', embedId: 'embed-id', options })).toBe(
54+
'https://form.typeform.com/to/some-id' +
55+
'?typeform-embed-id=embed-id' +
56+
'&typeform-embed=embed-widget' +
4057
'&typeform-source=unit-test-source' +
4158
'&typeform-medium=unit-test-medium' +
4259
'&typeform-medium-version=unit-test-version' +

0 commit comments

Comments
 (0)