internal/Write-Telemetry.ps1

function Write-Telemetry {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateSet("InvokeMyCorp","InvokeMycorp","InvokeMaester")]
        [string] $EventName
    )

    Write-Verbose "Sending telemetry event: $EventName"

    # Tenant ID for distinct identification
    $tenantId = $null
    try {
        $tenantId = (Get-MgContext).TenantId
    } catch {}
    if (-not $tenantId) { $tenantId = "unknown" }

    # Build JSON body
    $jsonData = @{
        api_key     = "phc_VxA235FsdurMGycf9DHjlUeZeIhLuC7r11Ptum0WjRK"
        distinct_id = $tenantId
        event       = $EventName
    }

    $jsonBody = $jsonData | ConvertTo-Json -Depth 5

    # PostHog endpoint
    $url = "https://us.i.posthog.com/capture/"

    # Make the request safely
    try {
        Invoke-RestMethod -Uri $url -Method Post -ContentType "application/json" -Body $jsonBody -ErrorAction Stop | Out-Null
    }
    catch {
        # Telemetry must NEVER break MyCorp
        Write-Verbose "Telemetry failure suppressed: $($_.Exception.Message)"
    }
}