Skip to content

Commit fd4f4ce

Browse files
committed
fix seg rep inter
1 parent 0387da3 commit fd4f4ce

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

scenarios/segment_sync_inter/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
----
44
## Overview
55

6-
This project provides a solution to replicating segments between master segments.
6+
This project provides a solution for replicating segments between master segments.
77

88
----
99
## Implementation
@@ -12,7 +12,7 @@ This project provides a solution to replicating segments between master segments
1212
----
1313
## Considerations
1414

15-
This project doesn't cover "Segment Include/Exclude" segment rules.
15+
This project doesn't cover "Drag-on Rules - Including/Excluding Segment" conditions.
1616

1717
----
1818
## Questions

scenarios/segment_sync_inter/scripts/seg_rep_inter_ms.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#TODO: edge case where segment rules have "existing segments", need to check if dependent segment exists beforehand
2-
31
import os
42
import requests
53

@@ -55,7 +53,7 @@ def build_seg_lists():
5553
from_data_dict[from_segs_list[i]["name"]] = [from_segs_list[i]["id"], from_segs_list[i]["description"], from_segs_list[i]["rule"], from_segs_list[i]["kind"]]
5654
from_name_set.add(from_segs_list[i]["name"])
5755
for j in range(len(to_segs_list)):
58-
to_data_dict[to_segs_list[j]["name"]]
56+
#to_data_dict[to_segs_list[j]["name"]]
5957
to_name_set.add(to_segs_list[j]["name"])
6058

6159
# Post new segment via Audience API
@@ -74,23 +72,48 @@ def detect_existing(sn, ns2):
7472
seg_exists = True
7573
return seg_exists
7674

75+
# Segments that have existing segment reference
76+
def detect_seg_ref(sn, rule):
77+
seg_ref = False
78+
for c in rule.get("conditions", []):
79+
if "conditions" in c:
80+
for sc in c["conditions"]:
81+
if str(sc).find("'type': 'Reference'") > 0:
82+
seg_ref = True
83+
return seg_ref
84+
85+
# Extract existing segments from segment rule
86+
def extract_existing_segments(rule):
87+
ref_list = []
88+
for condition_group in rule["conditions"]:
89+
for condition in condition_group.get("conditions", []):
90+
if condition.get("type") == "Reference" and "id" in condition:
91+
ref_list.append(condition["id"])
92+
return ref_list
93+
7794
# Compare segment lists and create diff
7895
def replicate_diff():
7996
for sn in from_name_set:
80-
if not detect_existing(sn, to_name_set):
97+
rule = from_data_dict[sn][2]
98+
kind = from_data_dict[sn][3]
99+
desc = from_data_dict[sn][1]
100+
if not detect_existing(sn, to_name_set) and not detect_seg_ref(sn, rule):
81101
payload = {
82102
'audienceId': to_audience,
83103
'name': sn,
84-
'kind': from_data_dict[sn][3],
85-
'description': from_data_dict[sn][1],
104+
'kind': kind,
105+
'description': desc,
86106
'segmentFolderId': to_folder,
87-
'rule': from_data_dict[sn][2]
107+
'rule': rule
88108
}
89109
headers = {
90110
"Authorization": f"TD1 {td_api_key}",
91111
"Content-Type": "application/json"
92112
}
93113
post_seg(sn, headers, payload)
114+
elif detect_seg_ref(sn, rule):
115+
ref_seg_ids = extract_existing_segments(rule)
116+
print(f"Skipping '{sn}'. Found referenced segment ids: '{ref_seg_ids}'.")
94117

95118
from_segs_list, to_segs_list = get_segs()
96119
build_seg_lists()

0 commit comments

Comments
 (0)