-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom functions to create close cookies
Calling `mw.centralNotice.hideBanner()` causes a spike in false tracking events during the fundraising campaign, due to the function looping the multiple URLS contained in `wgNoticeHideUrls` in order to set cookies on multiple wikis. This replaces the `hideBanner` call with custom setters that only create the cookies on wikipedia.org. See https://phabricator.wikimedia.org/T344381
- Loading branch information
Showing
5 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const HIDE_EVENT_TRACKING_CATEGORY = 'fundraising'; | ||
|
||
export function createImageCookieSetter( reason: string, durationInSeconds: number, trackingUrl: string ): void { | ||
const trackingData = { | ||
duration: String( durationInSeconds ), | ||
category: HIDE_EVENT_TRACKING_CATEGORY, | ||
reason: reason | ||
}; | ||
|
||
const baseUrl = new URL( 'https:' + trackingUrl ); | ||
const newParams = new URLSearchParams( [ | ||
...Array.from( baseUrl.searchParams.entries() ), | ||
...Object.entries( trackingData ) | ||
] ).toString(); | ||
|
||
const trackingImgUrl = new URL( `${ baseUrl.origin }${ baseUrl.pathname }?${ newParams }` ); | ||
|
||
document.createElement( 'img' ).src = trackingImgUrl.toString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const COOKIE_NAME = 'centralnotice_hide_fundraising'; | ||
|
||
export function setCookie( reason: string, created: Date, durationInSeconds: number ): void { | ||
const expiryDate = new Date( created.getTime() ); | ||
expiryDate.setSeconds( created.getSeconds() + durationInSeconds ); | ||
|
||
const hideData = { | ||
v: 1, | ||
created: Math.floor( created.getTime() / 1000 ), | ||
reason: reason | ||
}; | ||
|
||
document.cookie = `${ COOKIE_NAME }=${ encodeURIComponent( JSON.stringify( hideData ) ) }; expires=${ expiryDate.toUTCString() }; path=/; SameSite=None;`; | ||
} |
17 changes: 17 additions & 0 deletions
17
test/integration/page/MediaWiki/createTrackingImage.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { describe, expect, it, vi, vitest } from 'vitest'; | ||
import { createImageCookieSetter } from '@src/page/MediaWiki/createImageCookieSetter'; | ||
|
||
describe( 'createTrackingImage', () => { | ||
it( 'creates the image cookie setter', () => { | ||
const element = { src: '' }; | ||
const documentMock = { | ||
createElement: vi.fn( () => element ) | ||
}; | ||
vitest.stubGlobal( 'document', documentMock ); | ||
|
||
createImageCookieSetter( 'fun', 424242, '//en.wikipedia.org/w/index.php?title=Special:HideBanners' ); | ||
|
||
expect( document.createElement ).toHaveBeenCalledOnce(); | ||
expect( element.src ).toBe( 'https://en.wikipedia.org/w/index.php?title=Special%3AHideBanners&duration=424242&category=fundraising&reason=fun' ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { setCookie } from '@src/page/MediaWiki/setCookie'; | ||
|
||
describe( 'setCookie', () => { | ||
it( 'sets the cookie', () => { | ||
Object.defineProperty( document, 'cookie', { writable: true, configurable: true, value: '' } ); | ||
|
||
setCookie( 'testReason', new Date( 'August 29, 1997 02:14:00' ), 99999 ); | ||
|
||
expect( document.cookie ).toBe( [ | ||
'centralnotice_hide_fundraising=%7B%22v%22%3A1%2C%22created%22%3A872813640%2C%22reason%22%3A%22testReason%22%7D;', | ||
'expires=Sat, 30 Aug 1997 04:00:39 GMT;', | ||
'path=/;', | ||
'SameSite=None;' | ||
].join( ' ' ) ); | ||
} ); | ||
} ); |