JSON 中位置 0 的意外标记 G,使用 Google Analytics
Unexpected token G in JSON at position 0 with Google Analytics
我正在尝试在我的工作 Angular 5 项目中使用来自 Google Analytics 的 Measurement Protocol。我将 Google Analytics Universal 代码放在 index.html
中,然后像这样对服务进行 http 调用
index.html
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXX-Y', {
'cookieDomain': 'none'
});
// console.log(window.dataLayer);
</script>
启动 http 调用的服务
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { DataService } from './Data.service';
@Injectable()
export class AnalyticsService {
private anUID = 'UA-XXXXXXXX-Y';
private analyticsURL = 'https://www.google-analytics.com/collect?';
constructor(private http: HttpClient, private datos: DataService) { }
public pageViewLista(): void {
this.http.get(
this.setScreenViewUrl(
encodeURI('Lista de empresas'))).subscribe((data) => {
console.log('Received data from GAnalytics: ' + JSON.stringify(data));
}
);
}
protected setScreenViewUrl (pantalla: string): string {
const constructUrl = `${this.analyticsURL}v=1&t=screenview&tid=${this.anUID}&cid=${this.datos.id}&an=${this.datos.app}&dt=${pantalla}&cd=${pantalla}`;
return constructUrl;
}
}
问题是 Google returns 一个奇怪的错误,我不知道这意味着什么,也不知道这个错误的原因。我是不是执行得不好?
来自 Google 的错误:
ERROR
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "https://www.google-analytics.com/collect?v=1&t=...", ok: false, …}
error : {error: SyntaxError: Unexpected token G in JSON at position 0 at JSON.parse () at XMLHttp…, text: "GIF89a�����,D;"}
服务器似乎正在尝试 JSON 解析 GIF 图像。在文档中找不到任何内容,Google 未显示任何信息。
非常感谢您的帮助。
用于数据收集的 Google Analytics 端点 returns 一个透明的 gif 文件(并且它 returns 除了服务器错误之外的所有内容都是 200 状态,所以你不能用它来查看您的数据是否真的被跟踪)。 gif 无法解码为 JSON.
如果您想要 JSON 响应,您需要使用 endpoint for the GA debugger (google-analytics.com/debug/collect)。如果您的有效负载有效,那将提供信息,但不会跟踪呼叫。
我正在尝试在我的工作 Angular 5 项目中使用来自 Google Analytics 的 Measurement Protocol。我将 Google Analytics Universal 代码放在 index.html
中,然后像这样对服务进行 http 调用
index.html
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXX-Y', {
'cookieDomain': 'none'
});
// console.log(window.dataLayer);
</script>
启动 http 调用的服务
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { DataService } from './Data.service';
@Injectable()
export class AnalyticsService {
private anUID = 'UA-XXXXXXXX-Y';
private analyticsURL = 'https://www.google-analytics.com/collect?';
constructor(private http: HttpClient, private datos: DataService) { }
public pageViewLista(): void {
this.http.get(
this.setScreenViewUrl(
encodeURI('Lista de empresas'))).subscribe((data) => {
console.log('Received data from GAnalytics: ' + JSON.stringify(data));
}
);
}
protected setScreenViewUrl (pantalla: string): string {
const constructUrl = `${this.analyticsURL}v=1&t=screenview&tid=${this.anUID}&cid=${this.datos.id}&an=${this.datos.app}&dt=${pantalla}&cd=${pantalla}`;
return constructUrl;
}
}
问题是 Google returns 一个奇怪的错误,我不知道这意味着什么,也不知道这个错误的原因。我是不是执行得不好?
来自 Google 的错误:
ERROR
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "https://www.google-analytics.com/collect?v=1&t=...", ok: false, …}
error : {error: SyntaxError: Unexpected token G in JSON at position 0 at JSON.parse () at XMLHttp…, text: "GIF89a�����,D;"}
服务器似乎正在尝试 JSON 解析 GIF 图像。在文档中找不到任何内容,Google 未显示任何信息。
非常感谢您的帮助。
用于数据收集的 Google Analytics 端点 returns 一个透明的 gif 文件(并且它 returns 除了服务器错误之外的所有内容都是 200 状态,所以你不能用它来查看您的数据是否真的被跟踪)。 gif 无法解码为 JSON.
如果您想要 JSON 响应,您需要使用 endpoint for the GA debugger (google-analytics.com/debug/collect)。如果您的有效负载有效,那将提供信息,但不会跟踪呼叫。