簡體   English   中英

分析腳本應該放在 React 應用程序的什么位置?

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

Barion Pixel 需要在我的 React 應用程序中輸入代碼。 當我將它放在App.jsindex.js中時,它會引發錯誤。 你知道把它放在哪里嗎? 在 index.html 中無效。 奇怪的。

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>

我只是展示錯誤的樣子:

在此處輸入圖像描述

您可以嘗試使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM