Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -125,7 +125,7 @@ def fetch_basic_leads(start_date, end_date):
|
|
| 125 |
def enrich_leads_with_visits(df, start_date, end_date, max_rows=None, progress=gr.Progress()):
|
| 126 |
"""
|
| 127 |
Step 2: Go back and fetch the specific Visit URLs for each company.
|
| 128 |
-
|
| 129 |
"""
|
| 130 |
if df.empty:
|
| 131 |
return df
|
|
@@ -139,7 +139,6 @@ def enrich_leads_with_visits(df, start_date, end_date, max_rows=None, progress=g
|
|
| 139 |
lead_id = row["lead_id"]
|
| 140 |
|
| 141 |
try:
|
| 142 |
-
# FIX: Added start_date and end_date to params!
|
| 143 |
visit_data = make_request(
|
| 144 |
f"{BASE_URL}/accounts/{ACCOUNT_ID}/leads/{lead_id}/visits",
|
| 145 |
params={
|
|
@@ -160,21 +159,37 @@ def enrich_leads_with_visits(df, start_date, end_date, max_rows=None, progress=g
|
|
| 160 |
visit = visits[0]
|
| 161 |
v_attrs = visit.get("attributes", {})
|
| 162 |
|
| 163 |
-
#
|
| 164 |
landing = v_attrs.get("landing_page_path") or v_attrs.get("landing_page_url")
|
| 165 |
-
exit_p = v_attrs.get("exit_page_path") or v_attrs.get("exit_page_url")
|
| 166 |
|
| 167 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
if not landing or not exit_p:
|
| 169 |
pv_map = {p["id"]: p["attributes"] for p in included if p["type"] == "page_views"}
|
| 170 |
pv_ids = [r["id"] for r in visit.get("relationships", {}).get("page_views", {}).get("data", [])]
|
| 171 |
|
| 172 |
if pv_ids:
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
if
|
|
|
|
|
|
|
| 178 |
|
| 179 |
df.at[index, "landing_page_path"] = landing
|
| 180 |
df.at[index, "exit_page_path"] = exit_p
|
|
@@ -193,7 +208,6 @@ def enrich_leads_with_visits(df, start_date, end_date, max_rows=None, progress=g
|
|
| 193 |
def inspect_raw_json(lead_id, start_date, end_date):
|
| 194 |
if not lead_id: return "Please enter a Lead ID"
|
| 195 |
try:
|
| 196 |
-
# FIX: Added dates to Debugger URL params
|
| 197 |
url = f"{BASE_URL}/accounts/{ACCOUNT_ID}/leads/{lead_id}/visits"
|
| 198 |
params = {
|
| 199 |
"start_date": start_date,
|
|
@@ -214,7 +228,6 @@ def load_preview(start, end):
|
|
| 214 |
if df.empty:
|
| 215 |
return df, pd.DataFrame(), "⚠️ No data found."
|
| 216 |
|
| 217 |
-
# Pass dates to enrichment!
|
| 218 |
df_preview = df.copy()
|
| 219 |
df_preview = enrich_leads_with_visits(df_preview, start, end, max_rows=10)
|
| 220 |
|
|
@@ -225,7 +238,6 @@ def download_full_excel(df, start, end):
|
|
| 225 |
if df is None or df.empty: return None
|
| 226 |
|
| 227 |
print("⏳ Starting full enrichment for Excel export...")
|
| 228 |
-
# Pass dates to enrichment!
|
| 229 |
enriched_df = enrich_leads_with_visits(df.copy(), start, end)
|
| 230 |
|
| 231 |
path = "/tmp/leadfeeder_company_data_full.xlsx"
|
|
@@ -292,7 +304,6 @@ with gr.Blocks(title="Leadfeeder Pro Analytics") as demo:
|
|
| 292 |
table = gr.Dataframe(label="Preview (Top 10 Enriched)", interactive=True)
|
| 293 |
|
| 294 |
btn_load.click(load_preview, [start, end], [df_state, table, status])
|
| 295 |
-
# Updated to pass start/end inputs
|
| 296 |
btn_dl.click(download_full_excel, [df_state, start, end], file_dl)
|
| 297 |
|
| 298 |
# --- TAB 2: DASHBOARD ---
|
|
@@ -313,7 +324,6 @@ with gr.Blocks(title="Leadfeeder Pro Analytics") as demo:
|
|
| 313 |
dbg_id = gr.Textbox(label="Lead ID (Copy from Data tab)")
|
| 314 |
dbg_btn = gr.Button("Inspect Raw JSON")
|
| 315 |
dbg_out = gr.Code(language="json")
|
| 316 |
-
# Updated to pass start/end inputs
|
| 317 |
dbg_btn.click(inspect_raw_json, [dbg_id, start, end], dbg_out)
|
| 318 |
|
| 319 |
if __name__ == "__main__":
|
|
|
|
| 125 |
def enrich_leads_with_visits(df, start_date, end_date, max_rows=None, progress=gr.Progress()):
|
| 126 |
"""
|
| 127 |
Step 2: Go back and fetch the specific Visit URLs for each company.
|
| 128 |
+
FIX: Now specifically parses 'visit_route' to find the Exit Page.
|
| 129 |
"""
|
| 130 |
if df.empty:
|
| 131 |
return df
|
|
|
|
| 139 |
lead_id = row["lead_id"]
|
| 140 |
|
| 141 |
try:
|
|
|
|
| 142 |
visit_data = make_request(
|
| 143 |
f"{BASE_URL}/accounts/{ACCOUNT_ID}/leads/{lead_id}/visits",
|
| 144 |
params={
|
|
|
|
| 159 |
visit = visits[0]
|
| 160 |
v_attrs = visit.get("attributes", {})
|
| 161 |
|
| 162 |
+
# --- STRATEGY 1: Direct Attributes ---
|
| 163 |
landing = v_attrs.get("landing_page_path") or v_attrs.get("landing_page_url")
|
|
|
|
| 164 |
|
| 165 |
+
# --- STRATEGY 2: Parse 'visit_route' (The FIX for Exit Page) ---
|
| 166 |
+
# The visit_route is a list of all pages visited.
|
| 167 |
+
# Last item in list = Exit Page.
|
| 168 |
+
visit_route = v_attrs.get("visit_route", [])
|
| 169 |
+
if visit_route and isinstance(visit_route, list):
|
| 170 |
+
# Get Last Item for Exit Page
|
| 171 |
+
last_step = visit_route[-1]
|
| 172 |
+
if not exit_p:
|
| 173 |
+
exit_p = last_step.get("page_path") or last_step.get("page_url")
|
| 174 |
+
|
| 175 |
+
# Fallback for Landing Page (First Item)
|
| 176 |
+
if not landing:
|
| 177 |
+
first_step = visit_route[0]
|
| 178 |
+
landing = first_step.get("page_path") or first_step.get("page_url")
|
| 179 |
+
|
| 180 |
+
# --- STRATEGY 3: Deep Link via 'included' Page Views (Backup) ---
|
| 181 |
if not landing or not exit_p:
|
| 182 |
pv_map = {p["id"]: p["attributes"] for p in included if p["type"] == "page_views"}
|
| 183 |
pv_ids = [r["id"] for r in visit.get("relationships", {}).get("page_views", {}).get("data", [])]
|
| 184 |
|
| 185 |
if pv_ids:
|
| 186 |
+
if not landing:
|
| 187 |
+
first_pv = pv_map.get(pv_ids[0])
|
| 188 |
+
if first_pv: landing = first_pv.get("url") or first_pv.get("path")
|
| 189 |
+
|
| 190 |
+
if not exit_p:
|
| 191 |
+
last_pv = pv_map.get(pv_ids[-1])
|
| 192 |
+
if last_pv: exit_p = last_pv.get("url") or last_pv.get("path")
|
| 193 |
|
| 194 |
df.at[index, "landing_page_path"] = landing
|
| 195 |
df.at[index, "exit_page_path"] = exit_p
|
|
|
|
| 208 |
def inspect_raw_json(lead_id, start_date, end_date):
|
| 209 |
if not lead_id: return "Please enter a Lead ID"
|
| 210 |
try:
|
|
|
|
| 211 |
url = f"{BASE_URL}/accounts/{ACCOUNT_ID}/leads/{lead_id}/visits"
|
| 212 |
params = {
|
| 213 |
"start_date": start_date,
|
|
|
|
| 228 |
if df.empty:
|
| 229 |
return df, pd.DataFrame(), "⚠️ No data found."
|
| 230 |
|
|
|
|
| 231 |
df_preview = df.copy()
|
| 232 |
df_preview = enrich_leads_with_visits(df_preview, start, end, max_rows=10)
|
| 233 |
|
|
|
|
| 238 |
if df is None or df.empty: return None
|
| 239 |
|
| 240 |
print("⏳ Starting full enrichment for Excel export...")
|
|
|
|
| 241 |
enriched_df = enrich_leads_with_visits(df.copy(), start, end)
|
| 242 |
|
| 243 |
path = "/tmp/leadfeeder_company_data_full.xlsx"
|
|
|
|
| 304 |
table = gr.Dataframe(label="Preview (Top 10 Enriched)", interactive=True)
|
| 305 |
|
| 306 |
btn_load.click(load_preview, [start, end], [df_state, table, status])
|
|
|
|
| 307 |
btn_dl.click(download_full_excel, [df_state, start, end], file_dl)
|
| 308 |
|
| 309 |
# --- TAB 2: DASHBOARD ---
|
|
|
|
| 324 |
dbg_id = gr.Textbox(label="Lead ID (Copy from Data tab)")
|
| 325 |
dbg_btn = gr.Button("Inspect Raw JSON")
|
| 326 |
dbg_out = gr.Code(language="json")
|
|
|
|
| 327 |
dbg_btn.click(inspect_raw_json, [dbg_id, start, end], dbg_out)
|
| 328 |
|
| 329 |
if __name__ == "__main__":
|