-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_apply_transaction_nests_to_event.py
More file actions
135 lines (111 loc) · 5.38 KB
/
Copy pathmain_apply_transaction_nests_to_event.py
File metadata and controls
135 lines (111 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import requests
import json
from methods import init_session, select_active, confirm_environment, environment_url
from methods import recursively_convert_parameter_for_event
select_active("source")
rh, settings, src_project, _ = init_session(validate_src=True, validate_target=False)
org_id = settings["srcOrgId"]
project_id = settings["srcProjectId"]
environment_id = settings["srcEnvId"]
project_name = src_project.get("name", "Unknown Project")
print(" APPLY TRANSACTION'S NESTED OBJECT/ARRAY BLOCKS TO ANOTHER EVENT ")
confirm_environment(settings, "source", src_project, label="TARGET")
print("-------------------------------------------------")
print("Fetching all events...")
src_schemas = rh.get_schemas(org_id, project_id, environment_id)
if not src_schemas:
print("No schemas found in source project. This probably means your token is expired. Look in the cookie for https://cloud.unity.com/, under 'token'.")
print("Store the latest token in token.auth and re-run the script.")
print("Exiting...")
exit(1)
transaction_name = "transaction"
transaction_schema = rh.get_schema_by_id(org_id, project_id, environment_id, "transaction")
if not transaction_schema:
print(f"Failed to fetch transaction schema. Exiting...")
exit(1)
# Locate TRANSACTION's eventParams block and pull out its OBJECT/ARRAY children
# verbatim — these are the blocks (productsReceived, productsSpent, …) we'll
# graft onto the target event's eventParams with their full child trees intact.
event_params_node = next(
(p for p in transaction_schema["parameters"] if p["name"] == "eventParams"),
None,
)
nested_blocks = [
c for c in (event_params_node.get("children", []) or [])
if c.get("type") in ("OBJECT", "ARRAY") and c.get("name") in ("productsSpent", "productsReceived")
]
print(f"Found {len(nested_blocks)} OBJECT/ARRAY block(s) in {transaction_name}.eventParams:")
for block in nested_blocks:
child_names = [c["name"] for c in block.get("children", []) or []]
print(f" - {block['type']} '{block['name']}' (children: {', '.join(child_names) or '<none>'})")
# Build the list of selectable target events (exclude TRANSACTION itself and
# restricted/predefined events that the API will refuse to PUT).
selectable_schemas = [
s for s in src_schemas
if not s.get("isRestricted")
and not s.get("isPredefined")
and s["name"] != transaction_name
]
if not selectable_schemas:
print("No editable target events available. Exiting...")
exit(1)
print("-------------------------------------------------")
print("Select the event to apply the nested OBJECT/ARRAY blocks to:")
for index, schema in enumerate(selectable_schemas):
description = schema.get("description", "") or ""
print(f"{index} - {schema['name']} \t- {description}")
schema_index = input("Please select the event's number: ")
if not schema_index.isdigit() or int(schema_index) < 0 or int(schema_index) >= len(selectable_schemas):
print("Invalid event index. Exiting...")
exit(1)
schema_index = int(schema_index)
target_event_summary = selectable_schemas[schema_index]
target_event_name = target_event_summary["name"]
print(f"Selected event: \"{target_event_name}\"")
print("-------------------------------------------------")
input("If this is correct, press enter to continue... If not, terminate the script and change the settings.json file.\n THERE WILL BE NO FURTHER CONFIRMATIONS.")
print("thanks! beginning...")
# Fetch the current state of the target event.
target_schema = rh.get_schema_by_id(org_id, project_id, environment_id, target_event_name)
if not target_schema:
print(f"Failed to fetch {target_event_name} schema. Exiting...")
exit(1)
# Recursively merge a source subtree (TRANSACTION-side, full parameter shape)
# into a list of target children (event-parameter shape). If a same-named child
# is already present, descend into it and ensure every nested child from the
# source is also present. Existing nodes are NOT removed — a user may have
# manually added their own children, and we preserve those.
def merge_subtree_into_children(target_children, source_node):
existing = next((c for c in target_children if c["name"] == source_node["name"]), None)
if existing is None:
target_children.append(recursively_convert_parameter_for_event(source_node))
return
existing.setdefault("children", [])
for src_child in source_node.get("children", []) or []:
merge_subtree_into_children(existing["children"], src_child)
updated_parameters = target_schema["parameters"]
target_event_params = next(
(p for p in updated_parameters if p["name"] == "eventParams"),
None,
)
if target_event_params is None:
print(f"{target_event_name} has no eventParams block. Exiting...")
exit(1)
target_event_params.setdefault("children", [])
for block in nested_blocks:
print(f"Merging {block['type']} block '{block['name']}' into 'eventParams'")
merge_subtree_into_children(target_event_params["children"], block)
response = rh.update_schema(
org_id,
project_id,
environment_id,
target_event_name,
target_schema.get("description", "") or "",
updated_parameters,
target_schema.get("isEnabled", True),
)
if response:
print(f"Successfully updated schema {target_event_name}")
print(f"View it here: {environment_url(org_id, project_id, environment_id)}/analytics/v2/events/{target_event_name}")
else:
print(f"Failed to update schema {target_event_name}")