简体   繁体   English

分析脚本应该放在 React 应用程序的什么位置?

[英]Where should analytics script be put in a React app?

Barion Pixel requires to put in code in my React app. Barion Pixel 需要在我的 React 应用程序中输入代码。 When I put it in App.js or index.js , it raise an error.当我将它放在App.jsindex.js中时,它会引发错误。 Do you have any idea where to put it?你知道把它放在哪里吗? In index.html inhas no effect.在 index.html 中无效。 Strange.奇怪的。

https://docs.barion.com/Implementing_the_Base_Barion_Pixel https://docs.barion.com/Implementing_the_Base_Barion_Pixel

<script>
        // Create BP element on the window
        window["bp"] = window["bp"] || function () {
            (window["bp"].q = window["bp"].q || []).push(arguments);
        };
        window["bp"].l = 1 * new Date();

        // Insert a script tag on the top of the head to load bp.js
        scriptElement = document.createElement("script");
        firstScript = document.getElementsByTagName("script")[0];
        scriptElement.async = true;
        scriptElement.src = 'https://pixel.barion.com/bp.js';
        firstScript.parentNode.insertBefore(scriptElement, firstScript);

        // Send init event
        bp('init', 'addBarionPixelId', 'BP-0000000000-00');
    </script>

I just show how error looks like:我只是展示错误的样子:

在此处输入图像描述

You can try using dangerouslySetInnerHTML :您可以尝试使用dangerouslySetInnerHTML

    <script
      dangerouslySetInnerHTML={{
        __html: `
        // Create BP element on the window
        window["bp"] = window["bp"] || function () {
            (window["bp"].q = window["bp"].q || []).push(arguments);
        };
        window["bp"].l = 1 * new Date();

        // Insert a script tag on the top of the head to load bp.js
        scriptElement = document.createElement("script");
        firstScript = document.getElementsByTagName("script")[0];
        scriptElement.async = true;
        scriptElement.src = 'https://pixel.barion.com/bp.js';
        firstScript.parentNode.insertBefore(scriptElement, firstScript);

        // Send init event
        bp('init', 'addBarionPixelId', 'BP-0000000000-00');`
      }}
    />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该在哪里放置Google Analytics(分析)异步代码段 - Where should I put the Google Analytics asynchronous snippet 我应该将前端服务器代码放在使用 create-react-app 创建的应用程序中的什么位置? - Where should I put the frontend server code in app created with create-react-app? 在 React 应用程序中,您应该将仅用于测试的文件(json ...)放在哪里? - In a React App, where should you put files that are only used in tests (json...)? 在 React/Redux 应用程序中将 SignalR hub 放在哪里? - Where to put SignalR hub in React/Redux app? 应将已编译的脚本放在React的什么位置? - Where should a compiled script be placed in React? 我应该在React / Redux中的什么地方放置脚本? - Where should I place a script in React/Redux? 我应该把<script> tag for sentry CDN in html markup? - Where should I put the <script> tag for sentry CDN in html markup? 我应该放在哪里<script> tags in HTML markup? - Where should I put <script> tags in HTML markup? 我应该在此Codyhouse脚本中的何处放置isotope(&#39;layout&#39;) - Where should I put isotope('layout') in this Codyhouse script 我应该将Meteor订阅放在跟踪器反应容器中的什么位置? - Where should I put the Meteor subscription in a tracker-react container?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM