Google Analytics(分析)事件跟踪onClick问题

时间:2019-05-09 17:00:47

标签: javascript wordpress google-analytics onclick

我正在为登录页面上的多个按钮设置事件跟踪,已向每个按钮添加了onClick代码,但是似乎没有点击将任何数据发送到Google Analytics(分析)。当我在Chrome中检查“ Uncaught ReferenceError:ga在HTMLAnchorElement.onclick中未定义”时,控制台出现错误。我正在尝试跟踪3个单独按钮上的点击。

我的跟踪代码是:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        //CSRF
        services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");

        //Below methods are default methods that came with Core template.
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

        // In production, the Angular files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, IAntiforgery antiforgery)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();

        app.Use(async (context, next) =>
        {
            string path = context.Request.Path.Value;
            if (path != null && !path.ToLower().Contains("/api"))
            {
                // XSRF-TOKEN used by angular in the $http if provided
                var tokens = antiforgery.GetAndStoreTokens(context);
                context.Response.Cookies.Append("XSRF-TOKEN",
                  tokens.RequestToken, new CookieOptions
                  {
                      HttpOnly = false,
                      Secure = true
                  }
                );
            }
            await next();
        });


        app.UseSpaStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller}/{action=Index}/{id?}");
        });

        app.UseSpa(spa =>
        {
            // To learn more about options for serving an Angular SPA from ASP.NET Core,
            // see https://go.microsoft.com/fwlink/?linkid=864501

            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.Options.StartupTimeout = new System.TimeSpan(0, 0, 160);
                spa.UseAngularCliServer(npmScript: "start");
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

遵循此处的建议https://www.en.advertisercommunity.com/t5/Google-Analytics-Code/gtag-js-event-tracking-for-mailto-and-tellephone-link-not/td-p/1259427

解决了此问题

问题是我使用的是“ ga”而不是“ gtag”,同时使用“ gtag”也不需要使用“发送”。

相关问题