我在 vue 中為 SPA 應用程式添加特定的 Facebook 像素時遇到問題
<script>
!(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', 'test');
fbq('track', 'PageView');
fbq('track', 'CompleteRegistration');
</script>
<noscript>
<img
height="1"
width="1"
src="https://www.facebook.com/tr?id=1234456789&ev=PageView
&noscript=1"
/>
</noscript>
<!-- End Facebook Pixel Code -->
我想將此添加到我的頁面域名/注冊
如何在單頁應用程式VUE中為特定頁面添加腳本頭和無腳本頭
uj5u.com熱心網友回復:
您可以根據當前路線名稱呈現此腳本。
首先,它必須在已安裝的 Vue 應用程式 (Within <router-view/>) 內,例如:如果您有布局組件,則在它的內部。
重要的是,直接在 Vue 應用程式中添加腳本可能會導致問題。因此,最好將您的腳本移動到您的資產檔案夾中的 js 檔案中,例如:
/js/fbq.js.
之后檢查當前路由以如下方式渲染它:
<!-- Render based on route -->
<component v-if="$route.name == 'register'" :is="`script`" src="/js/fbq.js" async></component>
uj5u.com熱心網友回復:
第1步:添加facebook pixel code在public/index.html里面<head>的標簽
<!-- Facebook Pixel Code -->
<script>
!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', '266xxxxxxxx1');
fbq('track', 'PageView');
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=266xxxxxxxx1&ev=PageView&noscript=1"/>
</noscript>
<!-- End Facebook Pixel Code -->
第 2 步:在您的組件中,computed, methods, created您可以執行以下操作:
在里面methods你可以做,
methods: {
addtoCart() {
window.fbq('track', 'Add to your shopping cart')
}
}
在里面computed你可以做喜歡
computed: {
formattedRating () {
window.fbq('track', 'Format your rating')
return this.fixedPoints === null
? this.currentRating
: this.currentRating.toFixed(this.fixedPoints)
},
}
在里面created你可以這樣做,
created() {
this.loadPressRelease()
window.fbq('track', 'Load press release')
},

You can call the facebook pixel event faq function using window.faq in any components
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/352433.html
