This example demonstrates how to retrieve detailed analytics for a specific proxy user’s activity on the residential network over a defined time period. You’ll learn how to use the analytics endpoint to track usage patterns, monitor data consumption, and visualize request volumes with daily granularity.

import requests
from datetime import datetime, timedelta

# API credentials
API_PUBLIC_KEY = "your_public_key"
API_PRIVATE_KEY = "your_private_key"
BASE_URL = "https://api.pingproxies.com/1.0/public"

# Headers for authentication
headers = {
    "X-API-Public-Key": API_PUBLIC_KEY,
    "X-API-Private-Key": API_PRIVATE_KEY
}

def get_proxy_user_analytics(proxy_user_id, network="residential", days_ago=30, interval="day"):
    """
    Retrieve analytics for a specific proxy user on a given network.
    
    Args:
        proxy_user_id: ID of the proxy user to analyze
        network: Network type to filter (datacenter, isp, residential)
        days_ago: Number of days to look back
        interval: Time interval for data grouping (minute, hour, day, month)
        
    Returns:
        Analytics data for the specified proxy user and parameters
    """
    # Calculate start and end dates
    end_date = datetime.now()
    start_date = end_date - timedelta(days=days_ago)
    
    # Format dates for API
    period_start = start_date.strftime("%Y-%m-%d %H:%M:%S")
    period_end = end_date.strftime("%Y-%m-%d %H:%M:%S")
    
    # Build request URL with query parameters
    url = f"{BASE_URL}/user/analytics/graph"
    params = {
        "proxy_user_id": proxy_user_id,
        "network": network,
        "period_start": period_start,
        "period_end": period_end,
        "interval": interval
    }
    
    # Make the API request
    response = requests.get(url, headers=headers, params=params)
    
    # Check if the request was successful
    if response.status_code == 200:
        return response.json()
    else:
        print(f"Error: {response.status_code}")
        print(response.text)
        return None

def print_analytics_summary(analytics_data):
    """Print a summary of the analytics data"""
    if not analytics_data:
        print("No analytics data available")
        return
    
    data = analytics_data.get("data", {})
    
    # Print overall summary
    print("=== ANALYTICS SUMMARY ===")
    print(f"Total Requests: {data.get('total_requests', 0):,}")
    print(f"Successful Requests: {data.get('total_successful', 0):,}")
    print(f"Failed Requests: {data.get('total_error', 0):,}")
    print(f"Total Bandwidth Used: {data.get('total_bytes', 0) / 1024 / 1024:.2f} MB")
    
    # Print interval data
    print("\n=== DAILY BREAKDOWN ===")
    intervals = data.get("intervals", [])
    
    if not intervals:
        print("No interval data available")
        return
    
    # Print the top 5 days by request volume
    sorted_intervals = sorted(intervals, key=lambda x: x.get("requests", 0), reverse=True)
    for i, interval in enumerate(sorted_intervals[:5]):
        print(f"{i+1}. {interval.get('interval_name', 'Unknown')}")
        print(f"   Requests: {interval.get('requests', 0):,}")
        print(f"   Successful: {interval.get('successful', 0):,}")
        print(f"   Bandwidth: {interval.get('bytes', 0) / 1024 / 1024:.2f} MB")

# Example usage
if __name__ == "__main__":
    # Configure parameters
    PROXY_USER_ID = "your_proxy_user_id"  # Replace with your actual proxy user ID
    NETWORK_TYPE = "residential"          # Options: datacenter, isp, residential
    DAYS_TO_ANALYZE = 30                  # Number of days to look back
    INTERVAL = "day"                      # Options: minute, hour, day, month
    
    # Fetch analytics data
    analytics = get_proxy_user_analytics(
        proxy_user_id=PROXY_USER_ID,
        network=NETWORK_TYPE,
        days_ago=DAYS_TO_ANALYZE,
        interval=INTERVAL
    )
    
    # Print summary
    print_analytics_summary(analytics)

Key Features Explained

Analytics Parameters

  1. Proxy User ID: Target a specific proxy user for usage analysis
  2. Network Type: Filter by network infrastructure (residential, ISP, or datacenter)
  3. Time Period: Define start and end dates for the analysis window
  4. Interval Granularity: Group data by minute, hour, day, or month