Example:
Basic Code to use Python to Copy Split Definition (Individual targeting, Targeting Rules and Treatments) from source Workspace/Environment to a target Workspace/Environment. The script will create the target Split in the target Workspace, then add the Split Definition.
If the target Split already exist, comment out the line that creates the target split.
Script will use the REST Admin API to perform the actions.
How to use:
- Class wrapper for Admin API, installation instructions in this link: Python Admin API Library Wrapper
- Update the following variables in the code below:
sourceSplitName, sourceWorkspaceName, sourceEnvironmentName
targetSplitName, targetWorkspaceName, targetEnvironmentName, targetSplitDescription, targetTrafficTypeName
Admin API Key
Note:
Please note each Split has a unique hash key used to calculate treatments when percentage distribution rule is used, i.e. users bucketing will be different in each Split, and their experience might change from the old to the new Split.
from splitapiclient.main import get_client
#############################################
sourceSplitName="clients"
sourceWorkspaceName="Default"
sourceEnvironmentName="Production"
targetSplitName = "clients-new"
targetWorkspaceName = "myApp"
targetEnvironmentName = "Prod-myApp"
targetSplitDescription = "target description"
targetTrafficTypeName = "user"
#############################################
client = get_client({'apikey': 'ADMIN API KEY'})
sourceWs = client.workspaces.find(sourceWorkspaceName)
sourceEnv = client.environments.find(sourceEnvironmentName, sourceWs.id)
sourceSplitDef = client.split_definitions.find(sourceSplitName, sourceEnv.id, sourceWs.id)
targetWs = client.workspaces.find(targetWorkspaceName)
targetEnv = client.environments.find(targetEnvironmentName, targetWs.id)
trs = []
for tr in sourceSplitDef._treatments:
trs.append(tr.export_dict())
rls = []
for rl in sourceSplitDef._rules:
rls.append(rl.export_dict())
drls = []
for drl in sourceSplitDef._default_rule:
drls.append(drl.export_dict())
splitDefinition = {"treatments": trs,"defaultTreatment": sourceSplitDef._default_treatment, "rules": rls, "defaultRule": drls}
split = targetWs.add_split({'name':targetSplitName, 'description':targetSplitDescription}, targetTrafficTypeName)
splitDef = split.add_to_environment(targetEnv.id, splitDefinition)
Comments
0 comments
Please sign in to leave a comment.