ወደ ትምህርቱ ይመለሱ

የመጀመሪያው የሚሠራ ፋይል

public/app.js

ኮዱ እና የሚሠሩ ምሳሌ ገጾች የመጀመሪያ መለያዎቻቸውንና የምሳሌ ጽሑፋቸውን ይይዛሉ። ትምህርቱ እና የፋይል መመሪያዎች በእርስዎ ቋንቋ ያብራሯቸዋል።

import { MEASUREMENT_ID } from "./config.js";
let enabled = false,
  opened = false;
const status = document.querySelector("[role=status]");
function enable() {
  if (enabled) return;
  if (!/^G-[A-Z0-9]+$/.test(MEASUREMENT_ID) || MEASUREMENT_ID === "G-REPLACE-ME") {
    status.textContent = "Configure your own GA4 web stream measurement ID first.";
    return;
  }
  enabled = true;
  window.dataLayer = window.dataLayer || [];
  window.gtag = function () {
    dataLayer.push(arguments);
  };
  gtag("js", new Date());
  gtag("config", MEASUREMENT_ID, {
    send_page_view: false,
    allow_google_signals: false,
    allow_ad_personalization_signals: false,
    page_location: location.origin + location.pathname,
    page_referrer: "",
    debug_mode: true,
  });
  gtag("event", "page_view", {
    page_location: location.origin + location.pathname,
    page_referrer: "",
    page_title: "Analytics practice",
  });
  const script = document.createElement("script");
  script.async = true;
  script.src =
    "https://www.googletagmanager.com/gtag/js?id=" + encodeURIComponent(MEASUREMENT_ID);
  script.onerror = () => {
    status.textContent = "Analytics script could not load. The feature still works.";
  };
  document.head.append(script);
  status.textContent =
    "Analytics enabled for this tab. Verify receipt in your own DebugView.";
}
document.querySelector("#allow").addEventListener("click", enable);
document.querySelector("#decline").addEventListener("click", () => {
  if (!enabled)
    status.textContent = "Analytics stays off. You can still open the workshop.";
});
document.querySelector("#open").addEventListener("click", () => {
  document.querySelector("#instructions").hidden = false;
  if (enabled && !opened) {
    gtag("event", "workshop_open", { workshop_id: "practice-one" });
  }
  opened = true;
});