File size: 4,940 Bytes
3381a81
 
 
 
 
 
 
 
 
 
 
8ede948
 
 
 
 
 
 
 
3381a81
 
8ede948
3381a81
 
 
 
 
 
 
 
8ede948
3381a81
 
 
 
 
 
 
 
6f12fcb
 
 
 
 
3381a81
 
 
 
 
 
8ede948
3381a81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ede948
3381a81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bc0c36
dcc38da
 
7248272
dcc38da
fc8fd5d
dcc38da
 
ff0e480
 
 
 
 
 
 
 
fc8fd5d
ff0e480
 
dcc38da
 
3381a81
 
 
 
 
 
 
 
 
fdb8e52
7d4fb4e
3381a81
 
 
 
 
 
 
 
8ede948
3381a81
 
 
 
 
 
 
 
 
 
8ede948
3381a81
 
8ede948
3381a81
 
 
 
 
 
 
 
8ede948
3381a81
 
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import streamlit as st
import pandas as pd
from api_logic import fetch_keyword_ideas, rank_and_filter_keywords, KeywordRequestModel


# -----------------------------
# Display functions
# -----------------------------
def display_keyword_results(data: dict):
    """
    Display keyword ideas returned from fetch_keyword_ideas()

    Parameters
    ----------
    data : dict
        {
            "results": [...],
            "aggregateMetricResults": {...}
        }
    """

    # When no results are found
    if not data or not data.get("results"):
        st.warning("No keyword ideas found.")
        return

    results = data["results"]

    st.subheader("πŸ“Œ Keyword Ideas Overview")

    # ------- 1 Display main metrics as a table -------
    df_main = pd.DataFrame([
        {
            "Keyword": r["keyword"],
            "Avg Monthly Searches": r["avg_monthly_searches"],
            "Competition": r["competition"],
            "Competition Index": r["competition_index"],
            "Low Bid (micros)": r["low_top_of_page_bid_micros"],
            "High Bid (micros)": r["high_top_of_page_bid_micros"],
            #"Avg CPC (micros)": r["average_cpc_micros"],
            ## Added Features
            "Log CPC": r["log_cpc"],
            "Normalized CPC": r["log_cpc_norm_0_100"],
            
        }
        for r in results
    ])

    st.dataframe(df_main, use_container_width=True)

    # ------- 2 Monthly search volumes (inside expanders) -------
    st.subheader("πŸ“… Monthly Search Volumes")

    for r in results:
        with st.expander(f"{r['keyword']} - Monthly Trends"):
            mv = r.get("monthly_search_volumes", [])

            if mv:
                df_mv = pd.DataFrame(mv)
                df_mv = df_mv.rename(columns={
                    "month": "Month",
                    "year": "Year",
                    "monthly_searches": "Monthly Searches"
                })
                st.dataframe(df_mv, use_container_width=True)
            else:
                st.write("No monthly search volume data available.")


    # ------- 3 Aggregate metrics (device-based searches) -------
    st.subheader("πŸ“± Aggregate Metric Results")

    agg = data.get("aggregateMetricResults", {})
    device_searches = agg.get("device_searches", [])

    if device_searches:
        df_device = pd.DataFrame(device_searches)
        st.dataframe(df_device, use_container_width=True)
    else:
        st.write("No aggregate device metrics.")


# -----------------------------
# Streamlit UI
# -----------------------------
def main():

    st.title("πŸ” Google Ads Keyword Ideas ToolV007")
    with st.expander("πŸ› οΈ Keyword Planner Demo – Development Progress  (click to view)"):
        st.markdown("""
1. Obtain Google Ads API authentication token

2. Implement `fetch_keyword_ideas` in `api_logic.py`  
   This function communicates with the Google Ads API and retrieves keyword ideas.

3. Implement methodology to sort data    **⚠️(currently in progress)⚠️** requires 1 day more. 
   - Methodology is taken from **["Rule Book For GKP With Code"]**   
     3.1 Volume Search Section  
     3.2 Competition Score Section   
     3.3 CPC Score Section **(Completed)**   
     3.4 Intent Section  
     3.5 Trend Section 

4. Implement `app.py`  
   Integrate the results into the Streamlit UI and deploy  
                    **Hugging Face Link will be shared soon ** 
                        
                """)   

    st.markdown("Enter keyword(s) and your Google Ads settings to fetch keyword ideas.")

    # Input UI
    customer_id = st.text_input("Customer ID", value="")
    keywords = st.text_input("Keywords (comma separated)", value="")
    geo_target = st.text_input("Geo Target ID (optional)", value="2840")
    language = st.text_input("Language ID (optional)", value="1000")

    st.write("---")

    if st.button("πŸš€ Get Keyword Ideas"):

        if not customer_id or not keywords:
            st.error("Please enter Customer ID and keywords.")
            return

        keyword_list = [k.strip() for k in keywords.split(",")]

        # Build KeywordRequestModel
        request_model = KeywordRequestModel(
            customer_id=customer_id,
            keywords=keyword_list,
            geo_target=geo_target if geo_target else None,
            language=language if language else None,
        )

        with st.spinner("Fetching keyword ideas..."):
            response = fetch_keyword_ideas(request_model)

        # Filter top 20 items
        top20 = rank_and_filter_keywords(response, limit=20)

        # Build display-friendly response structure
        filtered_response = {
            "results": top20,
            "aggregateMetricResults": response.get("aggregateMetricResults")
        }

        st.success("βœ” Completed")
        display_keyword_results(filtered_response)

# Run application
if __name__ == "__main__":
    main()