cfreely / Inject SensorsData GA4-Forward

// ==UserScript==
// @name         Inject SensorsData GA4-Forward
// @namespace    https://openuserjs.org/user/cfreely
// @version      0.1
// @description  Inject specific SensorsData script into pages
// @author       Fu Lili
// @match        https://example.com/*
// @grant        none
// @license     MIT
// ==/UserScript==

(function () {
  'use strict';

  // Create script element for external JS file
  const externalScript = document.createElement('script');
  externalScript.src = 'https://jssdk.debugbox.sensorsdata.cn/js/syg/ga-test/ga-forward-sensors.js';
  document.head.appendChild(externalScript);

  // Once the external script is loaded, execute the following code
  externalScript.onload = function () {
    const inlineScript = document.createElement('script');
    inlineScript.innerHTML = `
            sensorsDataAnalytic201505.init({
                server_url: 'https://sdk-product-datasink.sensorsdata.cn/sa?project=shengyonggen',
                heatmap: {},
                show_log: true
            });
            sensorsDataAnalytic201505.quick('autoTrack');
        `;
    document.head.appendChild(inlineScript);
  };
})();