-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
My Jetpack: add A/B experiment to welcome flow (#38664)
* My Jetpack: add A/B test to Welcome flow * changelog * Fix project version * Sideload Tracks script after connection * Default experiment and control to redirect to user connection * My Jetpack: only reset notice after experiment loads * My Jetpack: don't change step if loading experiment * Prevent 'control' user to see evaluation step and code adjustments * Reshuffle conditions * Add try/catch to explats connection --------- Co-authored-by: robertsreberski <[email protected]>
- Loading branch information
1 parent
a2d6810
commit 150447c
Showing
5 changed files
with
118 additions
and
10 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
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
58 changes: 58 additions & 0 deletions
58
projects/packages/my-jetpack/_inc/utils/side-load-tracks.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,58 @@ | ||
declare global { | ||
interface Window { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
_tkq: Array< Array< any > >; | ||
} | ||
} | ||
|
||
/** | ||
* Function to get the current year and week number. | ||
* | ||
* @returns {string} The current year and week number. | ||
*/ | ||
function getCurrentYearAndWeek() { | ||
const date = new Date(); | ||
const year = date.getFullYear(); | ||
|
||
const firstDayOfYear = new Date( year, 0, 1 ); | ||
const daysSinceFirstDay = Math.floor( | ||
( date.getTime() - firstDayOfYear.getTime() ) / ( 24 * 60 * 60 * 1000 ) | ||
); | ||
|
||
// Calculate the current week number (assuming week starts on Sunday) | ||
const weekNumber = Math.ceil( ( daysSinceFirstDay + firstDayOfYear.getDay() + 1 ) / 7 ); | ||
const formattedWeekNumber = weekNumber.toString().padStart( 2, '0' ); | ||
|
||
return `${ year }${ formattedWeekNumber }`; | ||
} | ||
|
||
/** | ||
* Function to dynamically load a script into the document. | ||
* It creates a new script element, sets its source to the provided URL, | ||
* and appends it to the document's head. | ||
* | ||
* @param {string} src - The URL of the script to load. | ||
* @returns {Promise<void>} A promise that resolves once the script has loaded. | ||
*/ | ||
function loadScript( src: string ): Promise< void > { | ||
return new Promise( resolve => { | ||
const script = document.createElement( 'script' ); | ||
script.src = src; | ||
script.onload = () => resolve(); | ||
document.head.appendChild( script ); | ||
} ); | ||
} | ||
|
||
/** | ||
* Function to sideload Tracks script. | ||
* | ||
* It initializes the _tkq array on the window object if it doesn't exist, | ||
* and then loads the tracking script from the specified URL. Once the script has loaded, | ||
* the provided callback function is called. | ||
* | ||
* @returns {Promise<void>} A promise that resolves once the Tracks has been side loaded. | ||
*/ | ||
export default function sideloadTracks(): Promise< void > { | ||
window._tkq = window._tkq || []; | ||
return loadScript( `//stats.wp.com/w.js?${ getCurrentYearAndWeek() }` ); | ||
} |
5 changes: 5 additions & 0 deletions
5
projects/packages/my-jetpack/changelog/add-ab-experiment-welcome-flow
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,5 @@ | ||
Significance: patch | ||
Type: changed | ||
Comment: Added a simple A/B experimento to the Welcome Flow on My Jetpack. | ||
|
||
|