Skip to content

Commit 182ef51

Browse files
author
lfons
authored
Merge pull request #110 from Typeform/dist401
feat(DIST-401): Add tracking parameters
2 parents 168ac79 + ae03cc6 commit 182ef51

File tree

8 files changed

+90
-34
lines changed

8 files changed

+90
-34
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ typeformEmbed.makeWidget(element, url, options)
9393
| onReady | Callback function that will be executed once the typeform is ready. | `Function` | - |
9494
| onScreenChanged | Callback function that will be executed once the typeform's active screen changes. | `Function` | - |
9595
| transferableUrlParameters | Parameters that we want to transfert from the URL to the Typeform as hidden fields | `Array` | [] |
96+
| source | Domain name of the site using the SDK | `String` | null |
97+
| medium | Name of the plugin built on top of the SDK | `String` | null |
98+
| mediumVersion | Version of the plugin built on top of the SDK | `String` | null |
99+
96100
#### Example:
97101

98102
```js
@@ -147,6 +151,9 @@ typeformEmbed.makePopup(url, options)
147151
| onClose | Callback function that will be executed once the typeform is closed. | `Function` | - |
148152
| container | Element to place the popup into. Optional. Required only for `"side_panel"` mode. | `DOM element` | - |
149153
| transferableUrlParameters | Parameters that we want to transfert from the URL to the Typeform as hidden fields | `Array` | [] |
154+
| source | Domain name of the site using the SDK | `String` | null |
155+
| medium | Name of the plugin built on top of the SDK | `String` | null |
156+
| mediumVersion | Version of the plugin built on top of the SDK | `String` | null |
150157

151158
Types:
152159

demo/widget-api.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@
4141
window.typeformEmbed.makeWidget(el1, '//betatests.typeform.com/to/Z9k3bK', {
4242
hideFooter: true,
4343
hideHeaders: true,
44-
disableSubmit: true,
44+
disableSubmit: true,
45+
medium: 'embed-sdk',
46+
mediumVersion: '0.29.1',
47+
embedTrigger_type: 'on_page_load',
4548
onSubmit: function () {
4649
alert('my-embedded-typeform-1 has been submitted')
4750
}

src/core/attributes.js

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,39 @@ const getDataset = element => {
3535
return data
3636
}
3737

38-
const sanitizePopupAttributes = data => {
38+
const sanitizeCommonAttributes = data => {
3939
const obj = {}
4040

41+
if (data.hideHeaders === '' || data.hideHeaders === 'true') {
42+
obj.hideHeaders = true
43+
}
44+
45+
if (data.hideFooter === '' || data.hideFooter === 'true') {
46+
obj.hideFooter = true
47+
}
48+
49+
if (data.hideScrollbars === '' || data.hideScrollbars === 'true') {
50+
obj.hideScrollbars = true
51+
}
52+
53+
if (data.source) {
54+
obj.source = data.source
55+
}
56+
57+
if (data.medium) {
58+
obj.medium = data.medium
59+
}
60+
61+
if (data.mediumVersion) {
62+
obj.mediumVersion = data.mediumVersion
63+
}
64+
65+
return obj
66+
}
67+
68+
const sanitizePopupAttributes = data => {
69+
const obj = sanitizeCommonAttributes(data)
70+
4171
if (data.mode) {
4272
obj.mode = transformLegacyDataMode(data.mode)
4373
}
@@ -52,18 +82,6 @@ const sanitizePopupAttributes = data => {
5282
obj.autoOpen = true
5383
}
5484

55-
if (data.hideHeaders === '' || data.hideHeaders === 'true') {
56-
obj.hideHeaders = true
57-
}
58-
59-
if (data.hideFooter === '' || data.hideFooter === 'true') {
60-
obj.hideFooter = true
61-
}
62-
63-
if (data.hideScrollbars === '' || data.hideScrollbars === 'true') {
64-
obj.hideScrollbars = true
65-
}
66-
6785
if (data.open) {
6886
obj.open = data.open
6987
obj.openValue = data.openValue
@@ -89,19 +107,7 @@ const sanitizePopupAttributes = data => {
89107
}
90108

91109
const sanitizeWidgetAttributes = data => {
92-
const obj = {}
93-
94-
if (data.hideHeaders === '' || data.hideHeaders === 'true') {
95-
obj.hideHeaders = true
96-
}
97-
98-
if (data.hideFooter === '' || data.hideFooter === 'true') {
99-
obj.hideFooter = true
100-
}
101-
102-
if (data.hideScrollbars === '' || data.hideScrollbars === 'true') {
103-
obj.hideScrollbars = true
104-
}
110+
const obj = sanitizeCommonAttributes(data)
105111

106112
const transparency = parseInt(data.transparency, 10)
107113
if (data.transparency && transparency >= 0 && transparency <= 100) {

src/core/attributes.spec.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ describe('Attributes', () => {
1717
popupMockElem.setAttribute('data-hide-footer', false)
1818
popupMockElem.setAttribute('data-invalid-attribute', true)
1919
popupMockElem.setAttribute('data-size', '15')
20+
popupMockElem.setAttribute('data-source', 'example.com')
21+
popupMockElem.setAttribute('data-medium', 'embed-snippet')
22+
popupMockElem.setAttribute('data-medium-version', '0.29.1')
23+
popupMockElem.setAttribute('data-embed-trigger-type', 'on_page_load')
2024

2125
const popupOptions = {
2226
mode: 'popup',
@@ -25,7 +29,10 @@ describe('Attributes', () => {
2529
open: 'scroll',
2630
openValue: '20',
2731
hideHeaders: true,
28-
size: 15
32+
size: 15,
33+
source: 'example.com',
34+
medium: 'embed-snippet',
35+
mediumVersion: '0.29.1'
2936
}
3037

3138
expect(sanitizePopupAttributes(getDataset(popupMockElem))).toEqual(popupOptions)

src/core/make-popup.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const buildOptions = (embedId, options) => {
4646
embedType: POPUP_MODES[options.mode] || POPUP_MODES[POPUP],
4747
isModalOpen: false,
4848
autoClose: DEFAULT_AUTOCLOSE_TIMEOUT,
49+
medium: 'embed-sdk',
50+
source: window?.location?.hostname,
4951
hideFooter: false,
5052
hideHeaders: false,
5153
hideScrollbars: false,
@@ -64,6 +66,10 @@ const buildOptions = (embedId, options) => {
6466

6567
const queryStringKeys = {
6668
embedType: 'typeform-embed',
69+
source: 'typeform-source',
70+
medium: 'typeform-medium',
71+
mediumVersion: 'typeform-medium-version',
72+
open: 'typeform-embed-trigger-type',
6773
hideFooter: 'embed-hide-footer',
6874
hideHeaders: 'embed-hide-headers',
6975
disableTracking: 'disable-tracking'

src/core/make-popup.spec.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,46 @@ const instantiatePopup = (options) => {
2929
}
3030

3131
const renderPopupComponent = (autoOpen = false) => {
32-
const options = { hola: true, open: autoOpen ? 'load' : null }
32+
const options = {
33+
hola: true,
34+
open: autoOpen ? 'load' : null,
35+
source: 'example.com',
36+
medium: 'embed-snippet',
37+
mediumVersion: '0.29.1'
38+
}
3339

3440
const popup = instantiatePopup(options)
41+
const embedTriggerType = autoOpen ? '&typeform-embed-trigger-type=load' : ''
3542
if (!autoOpen) popup.open()
3643
const component = renderMock.mock.calls[0][0]
3744

3845
expect(renderMock).toHaveBeenCalledTimes(1)
3946
expect(component.type.name).toEqual('Popup')
40-
expect(component.props.url).toEqual(`${URL}?typeform-embed=popup-blank`)
47+
expect(component.props.url).toEqual(`${URL}?typeform-embed=popup-blank&typeform-source=example.com&typeform-medium=embed-snippet&typeform-medium-version=0.29.1${embedTriggerType}`)
4148
expect(component.props.options).toEqual(expect.objectContaining(options))
4249
}
4350

4451
const renderMobileModalComponent = (autoOpen = false) => {
4552
const spy = jest.fn()
46-
const options = { uid: UID, buttonText: 'hola', open: autoOpen ? 'load' : null, onSubmit: spy }
53+
const options = {
54+
uid: UID,
55+
buttonText: 'hola',
56+
open: autoOpen ? 'load' : null,
57+
onSubmit: spy,
58+
source: 'my-website.com'
59+
}
4760

4861
isMobileMock.mockImplementation(() => true)
4962
renderMock.mockClear()
5063

5164
const popup = makePopup(URL, options)
65+
const embedTriggerType = autoOpen ? '&typeform-embed-trigger-type=load' : ''
5266
if (!autoOpen) popup.open()
5367
const component = renderMock.mock.calls[0][0]
5468

5569
expect(renderMock).toHaveBeenCalledTimes(1)
5670
expect(component.type.name).toEqual('MobileModal')
57-
expect(component.props.url).toEqual(`${URL}?typeform-embed=popup-blank`)
71+
expect(component.props.url).toEqual(`${URL}?typeform-embed=popup-blank&typeform-source=my-website.com&typeform-medium=embed-sdk${embedTriggerType}`)
5872
expect(component.props.buttonText).toEqual(options.buttonText)
5973

6074
component.props.onSubmit()

src/core/make-widget.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const defaultOptions = {
2121
mode: 'embed-widget',
2222
hideFooter: false,
2323
hideHeaders: false,
24+
medium: 'embed-sdk',
25+
source: window?.location?.hostname,
2426
hideScrollbars: false,
2527
disableTracking: false,
2628
transferableUrlParameters: [],
@@ -30,6 +32,9 @@ const defaultOptions = {
3032

3133
const queryStringKeys = {
3234
mode: 'typeform-embed',
35+
source: 'typeform-source',
36+
medium: 'typeform-medium',
37+
mediumVersion: 'typeform-medium-version',
3338
hideFooter: 'embed-hide-footer',
3439
hideHeaders: 'embed-hide-headers',
3540
opacity: 'embed-opacity',

src/core/make-widget.spec.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ randomString.mockImplementation(() => EMBED_ID)
2020
describe('makeWidget', () => {
2121
it('renders a Widget component on desktop devices', () => {
2222
const element = document.createElement('div')
23-
const options = { opacity: 5, mandarina: 2 }
23+
const options = {
24+
opacity: 5,
25+
mandarina: 2,
26+
source: 'website.com',
27+
medium: 'embed-wordpress',
28+
mediumVersion: '9999'
29+
}
2430

2531
isMobileMock.mockImplementationOnce(() => false)
2632
renderMock.mockClear()
@@ -35,7 +41,9 @@ describe('makeWidget', () => {
3541
const { query } = UrlParse(widgetURL, true)
3642
expect(query['embed-opacity']).toEqual('5')
3743
expect(query['mandarina']).toBeUndefined()
38-
44+
expect(query['typeform-source']).toEqual('website.com')
45+
expect(query['typeform-medium']).toEqual('embed-wordpress')
46+
expect(query['typeform-medium-version']).toEqual('9999')
3947
expect(component.type.name).toEqual('Widget')
4048
expect(component.props.options).toEqual(expect.objectContaining(options))
4149
})

0 commit comments

Comments
 (0)