如何在DNN代码隐藏中加载Facebook Pixel NoScript代码? [英] How to load Facebook Pixel NoScript code in DNN codebehind?

查看:107
本文介绍了如何在DNN代码隐藏中加载Facebook Pixel NoScript代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have a checkout module with custom code which will show a wizard for End Users and another multi view when the user is a Dealer. I am trying to only fire the Facebook pixel code when the user is an End User. The problem I am having is the NoScript code which I can't call in a JavaScript function. I also don't want to put it after the script tags in the ASCX file because the rest of the pixel code is only called when an invoice is generated and the Facebook Pixel Helper Browser Extension is giving an error because of this. I attempted to split the Script and NoScript code. In the codebehind below I am setting the Facebook Pixel ID from a module setting which I have set. I have also created a setting for the NoScript code which is the FacebookPixelForEndUserNoScriptImageCode setting.

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

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

The code below is the front end code which is called from the codebehind.

    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代码。

This is the NoScript code which I set in the settings and call right after the RegisterStartupScript code has been called.

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

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

My current code seems to be failing as it is saying that fbq is undefined. I am calling the Purchase Event right after the above code has been called, but I think it is failing because the code is not together.

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

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

Are there any solution on how to call the Script and NoScript code within Javascript / JQuery?

推荐答案

通常当你有一个关于fbq没有初始化的错误时,这是​​由于你在实际的Facebook脚本被调用之前渲染的代码因此尚未定义。

Typically when you have an error about fbq not initialized it is due to the code that you are rendering triggering before the actual Facebook script is called, thus it is not defined yet.

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

I would ensure that the reference to the Facebook JS is before your code (for example at the top of the skin, or header, rather than the footer.)

至于无脚本,如果你想从后面的代码加载它,你可以使用< asp:literal> 如果使用WebForms模式,则控制将其呈现给页面。

As for the no script, if you want to load it from the code behind you can use an <asp:literal> control to render it to the page if you are using the WebForms pattern.

这篇关于如何在DNN代码隐藏中加载Facebook Pixel NoScript代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆