无法从 Google App Engine 将事件发布到 Google Analytics

nic*_*ris 5 google-app-engine google-analytics measurement-protocol

我正在尝试使用 Google App Engine (Java) 网络应用程序中的测量协议将事件发布到 google 分析。

我已经通过直接提交 URL 测试了 URL,它会立即(实时)显示在 Google Analytics 中。但是当我尝试在应用程序中发布它时,什么也没有显示。

所以我假设从我的 Java 代码开始有缺陷(也许是),所以我用谷歌的示例代码替换了我的代码,以便从谷歌应用引擎集成到谷歌分析这里。我改变了一点,但这里是关键位......

private static final URL GA_URL_ENDPOINT = getGoogleAnalyticsEndpoint();

private static URL getGoogleAnalyticsEndpoint() {
    try {
        return new URL("http", "www.google-analytics.com", "/collect");
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }
}

public boolean trackEvent(
        String category, String action, String label, String value) {

    try {

    Map<String, String> map = new LinkedHashMap<>();
    map.put("v", "1");             // Version.
    map.put("tid", gaTrackingId);
    map.put("cid", "555");
    map.put("t", "event");         // Event hit type.
    map.put("ec", encode(category, true));
    map.put("ea", encode(action, true));
    map.put("el", encode(label, false));
    map.put("ev", encode(value, false));

    HTTPRequest request = new HTTPRequest(GA_URL_ENDPOINT, HTTPMethod.POST);
    request.addHeader(CONTENT_TYPE_HEADER);
    request.setPayload(getPostData(map));

    HTTPResponse httpResponse = urlFetchService.fetch(request);

    // Return True if the call was successful.
    log.info("Response code for GA event is: " + httpResponse.getResponseCode());
    return httpResponse.getResponseCode() >= 200;

    } catch ( Exception e ) {
        //HANDLE EXCEPTION
        return false;
    }
}

private static byte[] getPostData(Map<String, String> map) {
    StringBuilder sb = new StringBuilder();
    for (Map.Entry<String, String> entry : map.entrySet()) {
        sb.append(entry.getKey());
        sb.append('=');
        sb.append(entry.getValue());
        sb.append('&');
    }
    if (sb.length() > 0) {
        sb.setLength(sb.length() - 1); // Remove the trailing &.
    }
    log.info("GA event string is: " + sb.toString());
    return sb.toString().getBytes(StandardCharsets.UTF_8);
}

private static String encode(String value, boolean required)
        throws UnsupportedEncodingException {
    if (value == null) {
        if (required) {
            throw new IllegalArgumentException("Required parameter not set.");
        }
        return "";
    }
    return URLEncoder.encode(value, StandardCharsets.UTF_8.name());
}
Run Code Online (Sandbox Code Playgroud)

调用这段代码时,我会从日志文件中获取参数字符串:

v=1&tid=UA-XXXXXXXX-1&cid=555&t=event&ec=settings&ea=autopost-on&el=rkAutoPost&ev=5
Run Code Online (Sandbox Code Playgroud)

我还看到我从对 google 的调用中获得了 2xx 响应代码。但是 GA 界面中没有显示任何内容(实时或其他方式)。

然后我尝试从我的浏览器中执行 GET ...

http://www.google-analytics.com/collect?v=1&tid=UA-XXXXXXXX-1&cid=555&t=event&ec=settings&ea=autopost-on&el=rkAutoPost&ev=5
Run Code Online (Sandbox Code Playgroud)

......这立即击中了 GA 实时。所以这告诉我消息的内容没有问题。

注意我还创建了一个全新的干净视图,以确保没有过滤掉任何内容......没有帮助。

请问有什么想法吗?谢谢!

nic*_*ris 1

与我读过的有关类似主题的所有其他问题一样,这是一个愚蠢的错误:问题是我确实在 GA 中创建了一个新的、干净的视图,但是当我创建它时我检查了......

排除已知机器人和蜘蛛的所有点击

...因此,通过 Google App Engine 调用生成的所有事件都被过滤掉。啊。