如何在DNN代码隐藏中加载Facebook Pixel NoScript代码?

时间:2018-04-04 15:09:25

标签: javascript dotnetnuke noscript facebook-pixel

我们有一个带有自定义代码的结账模块,当用户是经销商时,该模块将显示最终用户的向导和另一个多视图。我试图只在用户是最终用户时触发Facebook像素代码。我遇到的问题是NoScript代码,我无法在JavaScript函数中调用它。我也不想把它放在ASCX文件中的脚本标签之后,因为只有在生成发票并且Facebook Pixel Helper Browser Extension因此而发出错误时才会调用其余的像素代码。我试图拆分脚本和NoScript代码。在下面的代码隐藏中,我从我设置的模块设置中设置了Facebook Pixel ID。我还为NoScript代码创建了一个设置,即FacebookPixelForEndUserNoScriptImageCode设置。

enter image description here

if (Settings["FacebookPixelIDForEndUser"] != null && !string.IsNullOrWhiteSpace(Settings["FacebookPixelIDForEndUser"].ToString())){
      EndUserFirstName = SessionManager.CurrentUserInfo.FirstName;
      EndUserLastName = SessionManager.CurrentUserInfo.LastName;
      EndUserEmail = SessionManager.CurrentUserInfo.PersonalEmailAddress;

      facebookInitializationCodeID = Settings["FacebookPixelIDForEndUser"].ToString();
      ScriptManager.RegisterStartupScript(Page, this.GetType(), "attacheEvents", "javascript:initializeFacebookPixelCodeForEndUser();", true);

      string FacebookPixelNoScriptCode = Settings["FacebookPixelForEndUserNoScriptImageCode"].ToString();
      Page.ClientScript.RegisterStartupScript(this.GetType(), "FacebookPixelForEndUserNoScriptImageCode", FacebookPixelNoScriptCode, false);
}

下面的代码是从代码隐藏中调用的前端代码。

    function initializeFacebookPixelCodeForEndUser(){
    var custemail = '<%=EndUserEmail%>';
    var custfirstname = '<%=EndUserFirstName%>';
    var custlastname = '<%=EndUserLastName%>';

    !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)};
        if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
        n.queue=[];t=b.createElement(e);t.async=!0;
        t.src=v;s=b.getElementsByTagName(e)[0];
        s.parentNode.insertBefore(t,s)}(window, document,'script',
    'https://connect.facebook.net/en_US/fbevents.js');
    fbq('init', '<%=facebookInitializationCodeID%>', {
        em: custemail,
        fn: custfirstname,
        ln: custlastname,
    });
    fbq('track', 'PageView');
}

这是我在设置中设置的NoScript代码,并在调用RegisterStartupScript代码后立即调用。

<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr? 
id=RemovedTheIDForPrivacy&ev=PageView&noscript=1"
/></noscript>

我当前的代码似乎失败了,因为它说fbq未定义。我在调用上面的代码之后立即调用了购买事件,但我认为它失败了,因为代码不在一起。

<script>
 fbq('track', 'Purchase', {
   value: 1,
   currency: 'ZAR',
 });
</script>

有关如何在Javascript / JQuery中调用脚本和NoScript代码的解决方案吗?

2 个答案:

答案 0 :(得分:1)

通常,当您有关fbq未初始化的错误时,这是​​由于您在调用实际Facebook脚本之前呈现的代码触发,因此尚未定义。

我会确保Facebook JS的引用位于您的代码之前(例如,在皮肤顶部或标题,而不是页脚。)

对于no脚本,如果要从后面的代码加载它,可以使用<asp:literal>控件将其呈现到页面,如果您使用的是WebForms模式。

答案 1 :(得分:0)

我设法通过使用StringWriter在服务器端编写Facebook Pixel Tracking代码来解决这个问题。

if (!SessionManager.CurrentUserInfo.IsCustomerDealer)
                {     

                    if (Settings["FacebookPixelIDForEndUser"] != null && !string.IsNullOrWhiteSpace(Settings["FacebookPixelIDForEndUser"].ToString()))
                    {
                        EndUserFirstName = SessionManager.CurrentUserInfo.FirstName;
                        EndUserLastName = SessionManager.CurrentUserInfo.LastName;
                        EndUserEmail = SessionManager.CurrentUserInfo.UserName;

                        facebookInitializationCodeID = Settings["FacebookPixelIDForEndUser"].ToString();

                        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);

                    }
                }