检测用户的防病毒软件或浏览器是否阻止了 Facebook FBQ 等营销跟踪代码?

Detect if a users Anti-Virus or Browser is blocking marketing tracking code like Facebook FBQ?

有没有一种方法可以在 C# 中检测浏览器是否阻止外部营销 跟踪信息,例如 fbq?

在JQuery中您可以使用typeof fbq !== 'undefined'来检测Facebook像素跟踪代码是否被阻止,例如:

if(typeof fbq !== 'undefined'){
   fbq('track', 'ViewContent', {
    value: FacebookPriceIncVat,
    currency: FacebookInitiateCheckoutCurrency,
    content_ids: FacebookSKUView,
    content_type: 'product',
 });
}

我有一个模块,其中 Facebook 跟踪代码只能在后端生成。有没有一种方法可以使用 ajax 来检测 fbq 是否存在,它可以更改 bool 或其他一些方法来检测浏览器中是否有某些东西像某种插件一样阻止了链接?

如果 fbq 不存在,则不应执行以下代码。

if (FacebookPixelID != null)
      {
        EndUserFirstName = SessionManager.CurrentUserInfo.FirstName;
        EndUserLastName = SessionManager.CurrentUserInfo.LastName;
        EndUserEmail = SessionManager.CurrentUserInfo.UserName;

        FacebookInitializationCodeID = FacebookPixelID;

        string FacebookPixelResult = string.Empty;

        using (var FBPxl = new StringWriter())
          using (var FBCodescript = new HtmlTextWriter(FBPxl))
          {
              FBCodescript.AddAttribute(Attr.Type, "text/javascript");
              FBCodescript.RenderBeginTag(Tag.Script);
              //load ecommerce plugin
              FBCodescript.WriteLine("!function(f,b,e,v,n,t,s){if (f.fbq) return; n = f.fbq = function(){n.callMethod?n.callMethod.apply(n, arguments):n.queue.push(arguments)};");
              FBCodescript.WriteLine("if (!f._fbq) f._fbq = n; n.push = n; n.loaded = !0; n.version = '2.0';");
               FBCodescript.WriteLine("n.queue =[]; t = b.createElement(e); t.async = !0;");
               FBCodescript.WriteLine("t.src = v; s = b.getElementsByTagName(e)[0];");
               FBCodescript.WriteLine("s.parentNode.insertBefore(t, s)}");
               FBCodescript.WriteLine("(window, document,'script','https://connect.facebook.net/en_US/fbevents.js');");
               FBCodescript.WriteLine($"fbq('init', '{FacebookInitializationCodeID}', {{ em: '{EndUserEmail}', fn: '{EndUserFirstName}', ln: '{EndUserLastName}',}});");
               FBCodescript.WriteLine("fbq('track', 'PageView');");
               FBCodescript.RenderEndTag();
               FBCodescript.WriteLine($"<noscript><img height='1' width='1' style='display:none'  src='https://www.facebook.com/tr?id={FacebookInitializationCodeID}&ev=PageView&noscript=1'/></noscript>");
               FacebookPixelResult = FBCodescript.InnerWriter.ToString();
          }

      Page.ClientScript.RegisterStartupScript(this.GetType(), "myFacebookPixelInitialization", FacebookPixelResult, false);

          //Facebook Pixels Purchase Code
          string FacebookPixelPurchase = string.Empty;

          if (RevenueCurrency != null)
          {
              FBPurchaseCurrency = RevenueCurrency;
          }
          else
          {
          FBPurchaseCurrency = "ZAR";
          }

          using (var FBPxlPurchase = new StringWriter())
          using (var FBCodescriptPurchase = new HtmlTextWriter(FBPxlPurchase))
          {
               FBCodescriptPurchase.AddAttribute(Attr.Type, "text/javascript");
               FBCodescriptPurchase.RenderBeginTag(Tag.Script);
               //Write Facebook Pixel Purchase Event code
               FBCodescriptPurchase.WriteLine("fbq('track', 'Purchase', {");
               FBCodescriptPurchase.WriteLine($"value: {trans.Revenue},");
               FBCodescriptPurchase.WriteLine($"currency: '{FBPurchaseCurrency}',");
               FBCodescriptPurchase.WriteLine($"content_ids: '{string.Join(",", trans.LineItems.Select(l => l.SKU).ToArray())}',");
               FBCodescriptPurchase.WriteLine("content_type: 'product',");
               FBCodescriptPurchase.WriteLine($"contents: [");
               var PurchaseCounter = 1;

              foreach (var lineitem in trans.LineItems)
                  {
                   FBCodescriptPurchase.WriteLine("{");
                   FBCodescriptPurchase.WriteLine($"id: '{lineitem.SKU}',");
                   FBCodescriptPurchase.WriteLine($"quantity: {lineitem.Quantity},");
                   FBCodescriptPurchase.WriteLine($"item_price: {lineitem.UnitPrice}");
                   FBCodescriptPurchase.WriteLine("}");
                   if (PurchaseCounter != trans.LineItems.Count) FBCodescriptPurchase.WriteLine(",");
                        PurchaseCounter++;
                   }
                      FBCodescriptPurchase.WriteLine("]");
                      FBCodescriptPurchase.WriteLine("});");
                      FBCodescriptPurchase.RenderEndTag();

                      FacebookPixelPurchase = FBCodescriptPurchase.InnerWriter.ToString();
                   }

       Page.ClientScript.RegisterStartupScript(this.GetType(), "myFacebookPixelPurchase", FacebookPixelPurchase, false);
                    }
             }  

我认为解决这个问题其实很容易。 不知道为什么我没有想到这个。

我可以在 C# 代码中添加 if 语句。