內容簡介
Beta Flags 可讓開發人員管理新功能的發佈。不必讓程式碼馬上在生產環境中執行。現在你可以把它包裝在一個 Beta Flag 條件語句中,並從後端啟用它。
A/B 測試的工作方式非常相似,只是它不是開啟或關閉一個程式碼塊,而是讓它每隔 50% 就開啟一次。這會使用顯示文章或詞彙頁面的 URL 的微小變體,然後可以在分析服務中追蹤它 (例如 Adobe Omniture、Google 360)。
Beta Flags 的基本知識
若要建立新的 Beta Flag,請打開配置 JSON 檔案。插件已提供該檔案之複本,並存儲在 data/beta-flags.json 中。您可能更喜歡在您的主題根目錄中創建一個複本,與 functions.php 同一個文件夾 ([主題]/beta-flags.json)。
JSON 檔案的格式如下:
{
"flags": {
"sidebar_web": {
"title": "Beta Flags IN THEME",
"description": "Add a sidebar to the post page",
"author": "Charles Jaimet"
},
"library_admin": {
"title": "Beta Flags QA: Plugin Admin Test",
"description": "For Beta Flag testing in admin interface",
"author": "Charles Jaimet"
}
}
}
每個 Beta Flag 都由一個鍵 (例如 sidebar_web,library_admin) 定義,表示一個標題、説明和作者的对象。該鍵必須是唯一的,並在整個程序中用於識別給定的 Beta Flag。
在啟用插件後 (沒有特殊的説明),移至管理介面中的 "工具 > Beta Flags"(/wp-admin/tools.php?page=beta-flags)。如果您已正確建立 JSON 檔案,則會在此處找到來自您的 JSON 檔案的所有 Beta Flag。
關於 JSON 的注意事項,最容易犯的錯誤是在數組或對象的最後一個元素後加逗號。這會破壞 JSON 但在知道要查找的內容時很容易解決。有點像在 PHP 中忘記分號一樣。蠢蠢的分號...
當您首次加載插件或更新 JSON 文件後,應返回此管理屏幕。新的 Beta Flag 默認為禁用狀態,只能在此啟用。勾選要啟用的每個 Beta Flag 旁邊的 "啟用" 欄。完成後,單擊 "保存更改"。
在您的主題或其他插件中,您可以使用這些 Beta Flag 鍵來通過將相關的代碼包裝在條件語句周圍來控制功能執行。請嘗試將您包裝的代碼分組為單個函數、方法或類,以避免在您的主題中散布 Beta Flag 條件語句。
公共函数 beta_flag_enabled( $key ) 將返回一個 true | false 值,如果 Beta Flag 已啟用,則為 true。
一些示例:
if ( beta_flag_enabled( 'sidebar_web' ) ) {
get_sidebar();
}
if ( beta_flag_enabled( 'new_design' ) ) {
wp_register_style( 'my_styles', '/assets/my_styles.css', array(), '1.0.0', false );
wp_enqueue_style( 'my_styles' );
}
if ( beta_flag_enabled( 'popup_offer' ) ) {
new PopupOffer( '10% Off', 0.1 );
}
我建議使用有意義並傳達它們的目的的 Beta Flag 鍵。添加版本號也不會有任何影響。沒有字符限制,所以瘋狂一點也沒關係。
例如:
if ( beta_flag_enabled( 'revised_sticky_video_for_youtube_widgets_v.1.0.5' ) ) {
get_sidebar();
}
A/B 測試
現在您已經有了 Beta Flags,並在其中嵌入了它們,則可以使用 A/B 測試。
外掛標籤
開發者團隊
② 後台搜尋「Beta Flags … now with A/B Testing!」→ 直接安裝(推薦)
原文外掛簡介
Beta Flags allow developers to manage the release of new features. Instead of having code execute as soon as it is deployed to the production environment. You can now wrap it in a beta flag conditional and activate it from the back end.
A/B Testing works very similarly, except that instead of turning a code block on or off, they allow you to turn it on 50% of the time. This uses a slight variant on the URL used to display a post or term page, which can then be tracked in an analytics service (e.g. Adobe Omniture, Google 360).
The Basics of Beta Flags
To create a new beta flag, open the configuration JSON file. A copy of this file is provided with this plugin and is stored at data/beta-flags.json. You may prefer to create a copy in the root of your theme, the same folder that contains functions.php ([theme]/beta-flags.json).
The JSON file follows the format below:
{
"flags": {
"sidebar_web": {
"title": "Beta Flags IN THEME",
"description": "Add a sidebar to the post page",
"author": "Charles Jaimet"
},
"library_admin": {
"title": "Beta Flags QA: Plugin Admin Test",
"description": "For Beta Flag testing in admin interface",
"author": "Charles Jaimet"
}
}
}
Each flag is defined by a key (e.g. sidebar_web, library_admin), representing an object with a title, description, and author. The key must be unique, and is used throughout to identify the given flag.
Once you have activated the plugin (there are no special instructions for this), navigate to Tools > Beta Flags in the admin interface (/wp-admin/tools.php?page=beta-flags). Here you will find the flags from your JSON file if you have created it correctly.
A note about JSON, the easiest mistake to make is to put a comma after the last element in an array or object. This will break the JSON but is easy enough to fix when you know what to look for. Kinda like forgetting a semi-colon in PHP. Stupid semi-colons…
When you first load the plugin, and any time after you update the JSON file, you should return to this admin screen. New flags are disabled by default, and can only be enabled here. Check the box in the Enabled column beside each flag you want to turn on. Click Save Changes when done.
In your theme or other plugins, you can use these beta flag keys to control feature execution by wrapping a conditional around the relevant code. Try to group your wrapped code into a single function, method, or class to avoid littering your theme with beta flag conditionals.
The public function beta_flag_enabled( $key ) will return a true|false value if the beta flag is enabled.
Some examples:
if ( beta_flag_enabled( 'sidebar_web' ) ) {
get_sidebar();
}
if ( beta_flag_enabled( 'new_design' ) ) {
wp_register_style( 'my_styles', '/assets/my_styles.css', array(), '1.0.0', false );
wp_enqueue_style( 'my_styles' );
}
if ( beta_flag_enabled( 'popup_offer' ) ) {
new PopupOffer( '10% Off', 0.1 );
}
I suggest using beta flag keys that make sense and convey their purpose. Adding a version number never hurt, either. There is no character limit, so go nuts.
if ( beta_flag_enabled( 'revised_sticky_video_for_youtube_widgets_v.1.0.5' ) ) {
get_sidebar();
}
A/B Testing
Now you have your beta flags and you’ve embedded them in your code. The feature works as expected and you have it running on production. Is it better than what it replaced?
Enter A/B testing.
Check the A/B Test box beside the flag you want to test in the admin interface (see screen shot below), and check the “Enable beta testing” box at the bottom of the flag list, then click “Save Changes”.
Go to your website and refresh a few times. You will start to see some term and post links appearing with ?ab=1 appended to their URLs. (e.g. http://local.wordpress.test/hello-world/?ab=1).
When a visitor follows one of these links they will see your page with the beta flag disabled. When they follow the normal URL without the query string (e.g. http://local.wordpress.test/hello-world/) they will see the page with the beta flag enabled.
The query string is randomly appended 50% of time, so two visitors may follow the same link in the sidebar or menu, and one will get the flag turned on and one will get it turned off.
Because the URL matches the state of the beta flag, you will be able to see in your analytics service which experience visitors engaged with more. Implementing an effective A/B testing campaign is outside the scope of a README file but there are plenty of good reference books and sites.
As long as the “Enable beta testing” box is checked, post and term URLs on your site will get this query string treatment. Only beta flags that have the A/B Test box checked will be affected. Those with it unchecked will be controlled strictly by their Enabled box. Note also that checking A/B Test on a flag that is disabled will have no effect. Off is off.
