You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows应用商店Acquisitions数据接口缺失多数字段求助

微软商店Analytics API仅返回部分字段(缺失localCurrencyCode等)的解决建议

以下是针对API返回字段不全问题的具体解决步骤:

1. 添加groupby参数拆分聚合维度

微软Analytics API默认会对数据进行聚合汇总,仅返回少量统计字段。你需要显式指定groupby参数,列出需要拆分的维度字段,才能获取包含明细维度的完整数据。

修改fetchanalytics函数中的$params数组,添加groupby参数:

$params = [
    'applicationId' => $appid,
    'startDate' => $startdate,
    'endDate'  => $enddate,
    'top'      => $pagesize,
    'skip'     => $startindex,
    // 列出需拆分的维度,对应文档中的完整字段
    'groupby' => 'date,applicationId,acquisitionType,deviceType,market,osVersion,sandboxId,storeClient,localCurrencyCode,availabilityId,skuId,productTypeName,purchaseTaxType'
];

2. 显式指定返回字段(可选)

若添加groupby后仍有字段缺失,可通过select参数强制指定所有需要返回的字段:

$params['select'] = 'date,applicationId,applicationName,acquisitionType,age,deviceType,gender,market,osVersion,paymentInstrumentType,sandboxId,storeClient,xboxTitleId,localCurrencyCode,xboxProductId,availabilityId,skuId,skuDisplayName,xboxParentProductId,parentProductName,productTypeName,purchaseTaxType,acquisitionQuantity,purchasePriceUSDAmount,purchasePriceLocalAmount,purchaseTaxUSDAmount,purchaseTaxLocalAmount';

3. 缩小日期范围测试

如果请求的日期范围过大,API可能会自动聚合数据以优化性能。先尝试请求单日数据,验证是否能返回完整字段;若单日正常,再分批次请求大跨度日期的数据。

4. 验证Token权限范围

确认登录获取的Token包含正确权限,登录时需确保请求的scope包含https://manage.devcenter.microsoft.com/.default,否则可能无法访问完整的Analytics数据。

5. 检查API版本兼容性

确认使用的v1.0接口版本与官方文档一致,若微软有接口更新,需同步调整参数或升级到对应版本。


当前返回的数据示例:

[
  {
    "applicationId": "{MYAPPID}",
    "date": "2025-01-24",
    "acquisitionQuantity": 3,
    "purchasePriceUSDAmount": 37.68879931824771,
    "purchasePriceLocalAmount": 1689.22,
    "purchaseTaxUSDAmount": 2.2483605704174052,
    "purchaseTaxLocalAmount": 3.2399999999999998
  }
]

文档中应返回的完整字段示例:

[ 
    { 
        "date": "2019-01-15T01:00:00.0000000Z", 
        "applicationId": "9WZDNCRFHXHT", 
        "applicationName": null, 
        "acquisitionType": "Paid", 
        "age": null, 
        "deviceType": "Phone", 
        "gender": null, 
        "market": "US", 
        "osVersion": "Windows 11", 
        "paymentInstrumentType": null, 
        "sandboxId": "RETAIL", 
        "storeClient": "Microsoft Store (client)", 
        "xboxTitleId": null, 
        "localCurrencyCode": "USD", 
        "xboxProductId": null, 
        "availabilityId": "B42LRTSZ2MCJ", 
        "skuId": "0010", 
        "skuDisplayName": null, 
        "xboxParentProductId": null, 
        "parentProductName": null, 
        "productTypeName": "Game", 
        "purchaseTaxType": "TaxesNotIncluded", 
        "acquisitionQuantity": 1, 
        "purchasePriceUSDAmount": 3.08, 
        "purchasePriceLocalAmount": 3.08, 
        "purchaseTaxUSDAmount": 0.09, 
        "purchaseTaxLocalAmount": 0.09 
    } 
]

调整后的PHP请求代码示例:

<?

$token = logintoappstore(...);

$appsales = fetchanalytics($token, $appid, $startdatestring, $enddatestring, 1000, 0);

print_r(['appsales' => $appsales]);

function fetchanalytics(string $token, string $appid, string $startdate, string $enddate, int $pagesize, int $startindex): ?array {
    $url = "https://manage.devcenter.microsoft.com/v1.0/my/analytics/acquisitions";

    $params = [
        'applicationId' => $appid,
        'startDate' => $startdate,
        'endDate'  => $enddate,
        'top'      => $pagesize,
        'skip'     => $startindex,
        // 添加groupby参数
        'groupby' => 'date,applicationId,acquisitionType,deviceType,market,osVersion,sandboxId,storeClient,localCurrencyCode,availabilityId,skuId,productTypeName,purchaseTaxType',
        // 可选:显式指定返回字段
        'select' => 'date,applicationId,applicationName,acquisitionType,age,deviceType,gender,market,osVersion,paymentInstrumentType,sandboxId,storeClient,xboxTitleId,localCurrencyCode,xboxProductId,availabilityId,skuId,skuDisplayName,xboxParentProductId,parentProductName,productTypeName,purchaseTaxType,acquisitionQuantity,purchasePriceUSDAmount,purchasePriceLocalAmount,purchaseTaxUSDAmount,purchaseTaxLocalAmount'
    ];

    print "fetching acquisitions from $startdate to $enddate\n";
    $data = jsonget($url, $params, $token);
    if (isset($data['error'])) {
        error_log("unable to fetch acquisitions: ".$data['error']);
        return null;

    } else {
        return $data['Value'];
    }
}

function jsonget(string $url, array $params, ?string $authtoken = null): ?array {
    // just does intelligent url encoding on the values in the $params array
    $queryparamstring = data_url_encode($params);

    $url .= '?' . $queryparamstring;
    $text = curlget($url, $authtoken);
    $data = json_decode($text, true);
    if (is_array($data)) {
        return $data;
    } else {
        return null;
    }
}


function curlget(string $url, ?string $authtoken = null): string {
    $curl = curl($url, $authtoken);
    $output = curl_exec($curl);
    curl_close($curl);

    return $output;
}


function curl(string $url, ?string $authtoken = null) {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

    $headers = [
        'Content-Type: application/x-www-form-urlencoded',
    ];

    if ($authtoken !== null) {
        $headers[] = 'Authorization: Bearer ' . $authtoken;
    }
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

    return $curl;
}

内容的提问来源于stack exchange,提问作者Kenny Wyland

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 18:57:11