Skip to content

Commit

Permalink
Merge pull request #30 from yoriiis/fix/variation-switch-2
Browse files Browse the repository at this point in the history
Fix variation change
  • Loading branch information
yoriiis authored May 21, 2024
2 parents 405a2d7 + 1e217d0 commit 1113347
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 41 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## 1.3.3

### Fixes

- Fix variation changes with cookie domain and new `ABTastyForcedVariations` browser storage ([#30](https://github.com/yoriiis/abtasty-debugger/pull/30))

## 1.3.2

### Updates
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "abtasty-debugger",
"version": "1.3.2",
"version": "1.3.3",
"private": true,
"description": "Browser extension that simplifies the debugging of A/B tests from AB Tasty and adds useful informations",
"homepage": "https://github.com/yoriiis/abtasty-debugger",
Expand Down
24 changes: 12 additions & 12 deletions src/popup/components/detail/__tests__/detail.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,11 @@ describe('Detail', () => {
callback: expect.any(Function)
})
expect(sendMessage).toHaveBeenNthCalledWith(2, {
action: 'setStorage',
action: 'changeVariation',
data: {
name: 'ABTasty',
value: `uid=zed18spa36wefrnq&fst=1632216663697&pst=-1&cst=1632216663697&ns=1&pvt=1&pvis=1&th=661111.200001.1.1.1.1.1632216664066.1632216664066.1.2_${testId}.${newVariationId}.1.1.1.1.1632216664068.1632216664068.1.2`,
syncWithLocalStorage: true
testId,
variationId: parseInt(newVariationId),
cookieValue: `uid=zed18spa36wefrnq&fst=1632216663697&pst=-1&cst=1632216663697&ns=1&pvt=1&pvis=1&th=661111.200001.1.1.1.1.1632216664066.1632216664066.1.2_${testId}.${newVariationId}.1.1.1.1.1632216664068.1632216664068.1.2`
}
})
expect(namespace.tabs.reload).toHaveBeenCalled()
Expand All @@ -274,11 +274,11 @@ describe('Detail', () => {
callback: expect.any(Function)
})
expect(sendMessage).toHaveBeenNthCalledWith(2, {
action: 'setStorage',
action: 'changeVariation',
data: {
name: 'ABTasty',
value: `uid=zed18spa36wefrnq&fst=1632216663697&pst=-1&cst=1632216663697&ns=1&pvt=1&pvis=1&th=661111.200001.1.1.1.1.1632216664066.1632216664066.1.2_${testId}.${newVariationId}.1.1.1.1.1632216664068.1632216664068.1.2`,
syncWithLocalStorage: true
testId,
variationId: parseInt(newVariationId),
cookieValue: `uid=zed18spa36wefrnq&fst=1632216663697&pst=-1&cst=1632216663697&ns=1&pvt=1&pvis=1&th=661111.200001.1.1.1.1.1632216664066.1632216664066.1.2_${testId}.${newVariationId}.1.1.1.1.1632216664068.1632216664068.1.2`
}
})
expect(namespace.tabs.reload).toHaveBeenCalled()
Expand All @@ -303,11 +303,11 @@ describe('Detail', () => {
callback: expect.any(Function)
})
expect(sendMessage).toHaveBeenNthCalledWith(2, {
action: 'setStorage',
action: 'changeVariation',
data: {
name: 'ABTasty',
value: `uid=zed18spa36wefrnq&fst=1632216663697&pst=-1&cst=1632216663697&ns=1&pvt=1&pvis=1&th=661111.200001.1.1.1.1.1632216664066.1632216664066.1.2_${testId}.${newVariationId}.1.1.1.1.1632216664068.1632216664068.1.2`,
syncWithLocalStorage: true
testId,
variationId: parseInt(newVariationId),
cookieValue: `uid=zed18spa36wefrnq&fst=1632216663697&pst=-1&cst=1632216663697&ns=1&pvt=1&pvis=1&th=661111.200001.1.1.1.1.1632216664066.1632216664066.1.2_${testId}.${newVariationId}.1.1.1.1.1632216664068.1632216664068.1.2`
}
})
expect(namespace.tabs.reload).toHaveBeenCalled()
Expand Down
13 changes: 4 additions & 9 deletions src/popup/components/detail/assets/scripts/detail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,16 @@ export default class Detail extends Component {
`${testId}.${currentVariationId[1]}`,
`${testId}.${newVariationId}`
)
response = response.replace(thValue, thValueUpdated)

sendMessage({
action: 'setStorage',
action: 'changeVariation',
data: {
name: 'ABTasty',
value: response,
syncWithLocalStorage: true
testId,
variationId: newVariationId,
cookieValue: response.replace(thValue, thValueUpdated)
}
})
namespace.tabs.reload()

// Update the data with the new variation ID
// Useful if the detail view is re-rendered after updating the variation
this.props.results[testId].variationID = newVariationId
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/popup/components/list/__tests__/list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ describe('List', () => {

expect(target.setAttribute).toHaveBeenCalledWith('disabled', '')
expect(sendMessage).toHaveBeenCalledWith({
action: 'setStorage',
action: 'setCookie',
data: {
name: 'abTastyDebug',
value: true
Expand Down
2 changes: 1 addition & 1 deletion src/popup/components/list/assets/scripts/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export default class List extends Component {
target.setAttribute('disabled', '')

sendMessage({
action: target.checked ? 'setStorage' : 'removeCookie',
action: target.checked ? 'setCookie' : 'removeCookie',
data: {
name: 'abTastyDebug',
value: target.checked
Expand Down
44 changes: 31 additions & 13 deletions src/shared/assets/content-scripts/content-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,34 @@ function getCookie(name) {
}

/**
* Set storage (cookie and localStorage)
* Set cookie
* @param {Object} options
* @param {String} options.name Storage name
* @param {String} options.value Storage value
* @param {Boolean} options.syncWithLocalStorage Sync localtorage
*/
function setStorage({ name, value, syncWithLocalStorage = false }) {
const domain = window.location.host.split('.').slice(-2).join('.')
function setCookie({ name, value }) {
const domain = window.location.host.split('.')
const isSecure = window.location.protocol === 'https:'

document.cookie = `${name}=${value}; path=/; domain=.${domain};${isSecure ? ' Secure;' : ''}`
}

const valueInStorage = window.localStorage.getItem(name)
if (syncWithLocalStorage && valueInStorage) {
window.localStorage.setItem(name, value)
}
/**
* Change test variation
* Update new browser storage to force the variation changes
* @param {Object} options
* @param {String} options.testId Test ID
* @param {String} options.variationId Variation ID
* @param {String} options.cookieValue Cookie value
*/
function changeVariation({ testId, variationId, cookieValue }) {
setCookie({ name: 'ABTasty', value: cookieValue })

// Update new browser storage to force variation changes
const storageKey = 'ABTastyForcedVariations'
const storage = JSON.parse(window.sessionStorage.getItem(storageKey)) || {}
storage[testId] = variationId
window.sessionStorage.setItem(storageKey, JSON.stringify(storage))
}

/**
Expand All @@ -42,7 +54,7 @@ function setStorage({ name, value, syncWithLocalStorage = false }) {
* @param {String} options.value Cookie value
*/
function removeCookie({ name }) {
const domain = window.location.host.split('.').slice(-2).join('.')
const domain = window.location.host.split('.')
document.cookie = `${name}=; expires=${new Date(0).toUTCString()}; path=/; domain=.${domain};`
}

Expand Down Expand Up @@ -79,11 +91,10 @@ namespace.runtime.onMessage.addListener((message, sender, response) => {
document.dispatchEvent(new window.Event('abtastyDebugger::getData'))
} else if (message.action === 'getCookie') {
response(getCookie(message.data.name))
} else if (message.action === 'setStorage') {
setStorage({
} else if (message.action === 'setCookie') {
setCookie({
name: message.data.name,
value: message.data.value,
syncWithLocalStorage: message.data.syncWithLocalStorage
value: message.data.value
})
response('success')
} else if (message.action === 'removeCookie') {
Expand All @@ -94,6 +105,13 @@ namespace.runtime.onMessage.addListener((message, sender, response) => {
} else if (message.action === 'clearAbtastyCookies') {
document.dispatchEvent(new window.Event('abtastyDebugger::clearCookie'))
response('success')
} else if (message.action === 'changeVariation') {
changeVariation({
testId: message.data.testId,
variationId: message.data.variationId,
cookieValue: message.data.cookieValue
})
response('success')
}
}

Expand Down
1 change: 1 addition & 0 deletions src/shared/assets/content-scripts/page-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ document.addEventListener('abtastyDebugger::getData', () => {
document.addEventListener('abtastyDebugger::clearCookie', () => {
window.ABTasty.clearAllCookies()
window.localStorage.removeItem(ABTASTY_NAME)
window.sessionStorage.removeItem('ABTastyForcedVariations')
})

/**
Expand Down
2 changes: 1 addition & 1 deletion src/shared/assets/manifests/manifest-v2.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "AB Tasty Debugger",
"version": "1.3.2",
"version": "1.3.3",
"description": "Debug A/B tests from AB Tasty. Open source.",
"background": {
"scripts": ["background.js"]
Expand Down
2 changes: 1 addition & 1 deletion src/shared/assets/manifests/manifest-v3.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "AB Tasty Debugger",
"version": "1.3.2",
"version": "1.3.3",
"description": "Debug A/B tests from AB Tasty. Open source.",
"action": {
"default_title": "AB Tasty Debugger",
Expand Down

0 comments on commit 1113347

Please sign in to comment.