1.2.1.10 A/B实验接入介绍

优质
小牛编辑
127浏览
2023-12-01

1.1. 各个端接入sdk后以及开启A/B实验功能说明

1.1.1. iOS

引入v2.1.11以及以上版本后,开启A/B实验功能

(void)setABTestEnabled:(BOOL)ABEnabled;

具体说明点击查看详情

1.1.2. Android

引入v2.1.7以及以上版本,开启权限

<!-- Ab编程试验需要的权限 -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

重点:具体使用说明点击查看详情

1.1.3. web

引入 v1.6.8以及以上版本后,开启A/B实验功能

    DATracker.init('xxxxx', {
        abtest: {
            enable_abtest: true,
            // 实时拉取实验配置
            interval_mins_abtest: 0
        }
    });

具体使用说明点击查看详情

1.1.4. JAVA 服务端

引入v1.1-SNAPSHOT以及以上版本后,开启A/B实验功能

// 打开A/B实验功能
DATracker daTracker=DATracker.init("MA-6RWE-QWHKHEKDF12",true);

具体使用说明点击查看详情

1.1.5. 微信小程序

引入小程序sdk v1.1以及以上版本后,引入A/B实验sdk。

A/B实验模块获取(DATracker_ABtest)请联系开发者

//app.js
import DATracker from './utils/DATracker';
import ABtest from './utils/DATracker_ABtest';

DATracker.init('88888', {
  appVersion: 'v1.0.1'
});

// 将ABtest实例注册到 DATracker 中,方便后面使用
DATracker['abtest'] = new ABtest(DATracker);

具体使用说明点击查看详情

1.2. 接入一个编程实验

下面讲解接入web应用编程实验流程为例

1.2.1. A/B实验平台中创建web应用编程实验

登录demo后

进入A/B实验平台操作流程:

  1. 点击创建实验按钮;
  2. 进入新建实验界面;
  3. 设置一个实验名称 开发文档内的web编程实验例子
  4. 类型设置为编程实验;
  5. 选择一个web应用;
  6. 点击新增分层,创建一个分层后,选择该分层;
  7. 点击下一步;
  8. 点击新增变量(这里名称设置为 bgColor)并创建后,选择该变量;该变量在sdk里使用,目的是改变背景颜色;
  9. 按照提示新建实验版本;设置的变量值将在网页中调用sdk的API获取到;
  10. 设置优化指标,选择 提交订单
  11. 点击确定完成实验创建;
  12. 进入运行控制,调整流量,点击开始实验

点击查看该实验配置点击查看该实验流量分配

截图:

设置实验基本信息
Figure: 设置实验基本信息
设置实验版本信息
Figure: 设置实验版本信息

页面里引入sdk,开启A/B实验

引入sdk方式请点击获取和引入 HubbleData SDK

    // sdk 初始化
    <script type="text/javascript">
        //初始化
        //注意: 在Hubble平台创建一个类型为`Web`应用,复制对应的appkey,替换下面的 `MA-883D-4FFC199551FF`
        DATracker.init('MA-883D-4FFC199551FF', {truncateLength: 255,persistence: "localStorage",cross_subdomain_cookie: false,
        abtest: {
            // 开启实验功能
            enable_abtest: true,
            // 实时拉取实验配置
            interval_mins_abtest: 0
        }});
    </script>
// 清除本地数据,实际使用不要用 localStorage.setItem('mp_MA-883D-4FFC199551FF_hubble',''); (function(document,datracker,root){var help={};help.cookie={get:function(name){var nameEQ=name+"=";var ca=document.cookie.split(";");for(var i=0;i<ca.length;i++){var c=ca[i];while(c.charAt(0)==" "){c=c.substring(1,c.length)}if(c.indexOf(nameEQ)===0){return decodeURIComponent(c.substring(nameEQ.length,c.length))}}return null},parse:function(name){var cookie;try{cookie=_.JSONDecode(_.cookie.get(name))||{}}catch(err){}return cookie},set:function(name,value,days,cross_subdomain,is_secure){var cdomain="",expires="",secure="";if(cross_subdomain){var matches=document.location.hostname.match(/[a-z0-9][a-z0-9\-]+\.[a-z\.]{2,6}$/i),domain=matches?matches[0]:"";cdomain=((domain)?"; domain=."+domain:"")}if(days){var date=new Date();date.setTime(date.getTime()+(days*24*60*60*1000));expires="; expires="+date.toGMTString()}if(is_secure){secure="; secure"}var new_cookie_val=name+"="+encodeURIComponent(value)+expires+"; path=/"+cdomain+secure;document.cookie=new_cookie_val;return new_cookie_val},remove:function(name,cross_subdomain){_.cookie.set(name,"",-1,cross_subdomain)}};root.getHubbleJSSDKVersions=function(version){help.cookie.set("mp_versions_hubble_jsSDK",version,false,true);loadJsSDK()};function removeElement(_element){var _parentElement=_element.parentNode;if(_parentElement){_parentElement.removeChild(_element)}}function addScriptTag(src){var loaded;var head=document.getElementsByTagName("head")[0];var script=document.createElement("script");var localVersion=help.cookie.get("mp_versions_hubble_jsSDK");script.setAttribute("type","text/javascript");script.src=src;script.onload=script.onreadystatechange=function(){if(!loaded&&(!script.readyState||/loaded|complete/.test(script.readyState))){script.onload=script.onreadystatechange=null;loaded=true;removeElement(script)}};head.appendChild(script)}function addScriptTagTwo(url,fn){var isFirst=true;var iframe;try{iframe=document.createElement("iframe");iframe.style.display="none"}catch(e){iframe=document.createElement("iframe");iframe.setAttribute("src",url)}var loadfn=function(){if(isFirst){iframe.contentWindow.location="about:blank";isFirst=false}else{fn(iframe.contentWindow.name);iframe.contentWindow.document.write("");iframe.contentWindow.close();document.body.removeChild(iframe);iframe.src="";iframe=null}};iframe.src=url;if(iframe.attachEvent){iframe.attachEvent("onload",loadfn)}else{iframe.onload=loadfn}if(iframe){document.body.appendChild(iframe)}}function loadJsSDK(){var localVersion=help.cookie.get("mp_versions_hubble_jsSDK");var HUBBLE_LIB_URL="https://hubble.netease.com/track/w/DATracker.globals.js";var script,first_script;script=document.createElement("script");script.type="text/javascript";script.async=true;if(localVersion){HUBBLE_LIB_URL="https://hubble-js-bucket.nosdn.127.net/"+localVersion+".js"}script.src=HUBBLE_LIB_URL;first_script=document.getElementsByTagName("script")[0];first_script.parentNode.insertBefore(script,first_script)}if(!datracker["__SV"]){var win=window;try{var getHashParam,matches,state,loc=win.location,hash=loc.hash;getHashParam=function(hash,param){matches=hash.match(new RegExp(param+"=([^&]*)"));return matches?matches[1]:null};if(hash&&getHashParam(hash,"state")){state=JSON.parse(decodeURIComponent(getHashParam(hash,"state")));if(state["action"]==="mpeditor"){win.sessionStorage.setItem("_mpcehash",hash);history.replaceState(state["desiredHash"]||"",document.title,loc.pathname+loc.search)}}}catch(e){}var gen_fn,functions,i,lib_name="DATracker";window[lib_name]=datracker;datracker["_i"]=[];datracker["init"]=function(token,config,name){var target=datracker;if(typeof(name)!=="undefined"){target=datracker[name]=[]}else{name=lib_name}target["people"]=target["people"]||[];target["abtest"]=target["abtest"]||[];target["toString"]=function(no_stub){var str=lib_name;if(name!==lib_name){str+="."+name}if(!no_stub){str+=" (stub)"}return str};target["people"]["toString"]=function(){return target.toString(1)+".people (stub)"};function _set_and_defer(target,fn){var split=fn.split(".");if(split.length==2){target=target[split[0]];fn=split[1]}target[fn]=function(){target.push([fn].concat(Array.prototype.slice.call(arguments,0)))}}functions="track_heatmap register_attributes register_attributes_once clear_attributes unregister_attributes current_attributes single_pageview disable time_event get_appStatus track set_userId track_pageview track_links track_forms register register_once alias unregister identify login logout signup name_tag set_config reset people.set people.set_once people.set_realname people.set_country people.set_province people.set_city people.set_age people.set_gender people.increment people.append people.union people.track_charge people.clear_charges people.delete_user people.set_populationWithAccount people.set_location people.set_birthday people.set_region people.set_account abtest.get_variation abtest.async_get_variable".split(" ");for(i=0;i<functions.length;i++){_set_and_defer(target,functions[i])}datracker["_i"].push([token,config,name]) };datracker["__SV"]=1.6}if(!help.cookie.get("mp_versions_hubble_jsSDK")){addScriptTag("https://hubble.netease.com/track/w/version.js?random"+Math.random())}else{loadJsSDK()}})(document,window["DATracker"]||[],window); //初始化 //注意: 在Hubble平台创建一个类型为`Web`应用,复制对应的appkey,替换下面的 `MA-883D-4FFC199551FF` DATracker.init('MA-883D-4FFC199551FF', {truncateLength: 255,persistence: "localStorage",cross_subdomain_cookie: false, abtest: { enable_abtest: true, // 实时拉取实验配置 interval_mins_abtest: 0 }});

1.2.2. web页面里参与编程实验实现

当前参与的实验变量:bgColor;
获取到的实验变量值:$$

// 参与编程实验实现
DATracker.abtest.get_variation(function(flgs) {
    // 调用 get 方法参与编程实验 ;
    // `bgColor` 是在创建编程实验预定义的变量;
    // '#fff' 是默认值(当获取变量值失败后使用);
    var bgColor = flgs.get('bgColor', '#fff');
    // 设置上面提交按钮的背景颜色
    document.getElementById('submit').style.backgroundColor = bgColor;
    // 其它
    showTip(bgColor);
});

function showTip($val) { var $tip = document.getElementById('tip'); var innerHtml = $tip.innerHTML.replace('$$', $val); $tip.innerHTML = innerHtml; $tip.style.display = 'block'; } document.getElementById('run').onclick = function() { var $js = document.getElementById('js'); var runJs = $js.innerText; eval(runJs); }; // 上报优化指标事件 document.getElementById('submit').onclick = function() { DATracker.track('SubmitOrder'); };