Lookalike Audiences Algorithm with GA4 & Salesforce Data
March 19, 2025
2 min
To automate the Customer Lifetime Value (LTV) calculation using APIs from Google Analytics and Monday.com CRM, you can follow the steps outlined below
The automation involves:
Before starting, ensure:
requests
, pandas
).average_order_value
and purchase_frequency
.from google.oauth2 import service_accountfrom googleapiclient.discovery import builddef fetch_google_analytics_data(property_id):# Authenticate using service accountcredentials = service_account.Credentials.from_service_account_file('path/to/credentials.json')analytics = build('analyticsdata', 'v1beta', credentials=credentials)# Query Google Analytics for required metricsrequest = {"property": f"properties/{property_id}","dateRanges": [{"startDate": "2023-01-01", "endDate": "2023-12-31"}],"metrics": [{"name": "averagePurchaseRevenue"}, {"name": "purchaseFrequency"}]}response = analytics.properties().runReport(body=request).execute()# Extract metricsdata = []for row in response['rows']:data.append({'user_id': row['dimensionValues'][^0]['value'],'average_order_value': float(row['metricValues'][^0]['value']),'purchase_frequency': float(row['metricValues'][^1]['value'])})return data
import requestsdef fetch_monday_data(api_key):url = "https://api.monday.com/v2"headers = {"Authorization": api_key}query = """query {items {idnamecolumn_values {idtext}}}"""response = requests.post(url, headers=headers, json={"query": query})data = response.json()# Parse customer lifespan from column valuescustomers = []for item in data['data']['items']:customer_lifespan = next((col['text'] for col in item['column_values'] if col['id'] == 'customer_lifespan'), None)customers.append({'user_id': item['id'],'customer_lifespan': int(customer_lifespan) if customer_lifespan else 0})return customers
Combine data from both APIs and calculate LTV using the formula:
import pandas as pddef calculate_ltv(ga_data, monday_data):# Convert data to DataFramesga_df = pd.DataFrame(ga_data)monday_df = pd.DataFrame(monday_data)# Merge on user_idcombined_df = pd.merge(ga_df, monday_df, on='user_id')# Calculate LTVcombined_df['LTV'] = (combined_df['average_order_value'] *combined_df['purchase_frequency'] *combined_df['customer_lifespan'])return combined_df[['user_id', 'LTV']]
Export the calculated LTV values to a dashboard or database for visualization or further use.
Example of exporting to a CSV file:
def export_to_csv(dataframe, filename="ltv_report.csv"):dataframe.to_csv(filename, index=False)
If coding is not feasible, consider using integration platforms like:
By leveraging APIs from Google Analytics and Monday.com CRM, businesses can automate LTV calculations and unlock powerful insights into customer behavior and value, driving smarter marketing strategies and improved ROI.
Quick Links
Legal Stuff