#!/usr/bin/env bash
# Daftar semua tag GTM/GA/Ads yang sedang jalan di studiosoft.id + isi container publiknya
set -u
TS=$(date +%s)
curl -s -4 --max-time 90 -A "Mozilla/5.0" "https://studiosoft.id/?cb=$TS" -o /tmp/ss_tag.html
echo "=== ID yang muncul di HTML ==="
grep -o -E "GTM-[A-Z0-9]+|GT-[A-Z0-9]+|G-[A-Z0-9]{8,}|AW-[0-9]+|UA-[0-9-]+" /tmp/ss_tag.html | sort | uniq -c | sort -rn
echo
echo "=== baris pemuatan tag ==="
grep -o -E "(src|href)=\"[^\"]*(gtm|gtag|googletagmanager)[^\"]*\"" /tmp/ss_tag.html | sort -u | head -8
echo
for ID in $(grep -o -E "GTM-[A-Z0-9]+" /tmp/ss_tag.html | sort -u); do
  echo "=== isi container $ID (10 KB pertama, cari nama) ==="
  curl -s -4 --max-time 60 -A "Mozilla/5.0" "https://www.googletagmanager.com/gtm.js?id=$ID" -o /tmp/ss_gtm.js
  wc -c < /tmp/ss_gtm.js | xargs printf "  ukuran gtm.js: %s byte\n"
  grep -o -E "\"name\":\"[^\"]{2,60}\"" /tmp/ss_gtm.js | sort -u | head -12
  echo "  --- id tag/trigger/pemicu yang disebut:"
  grep -o -E "\"tag_id\":\"[0-9]+\"|\"trigger_id\":\"[0-9]+\"" /tmp/ss_gtm.js | sort -u | head -10
  echo "  --- jumlah tag di container:"
  grep -o -E "\"tag_id\":" /tmp/ss_gtm.js | wc -l
  echo "  --- GA4 id yang disebut:"
  grep -o -E "G-[A-Z0-9]{8,}|GT-[A-Z0-9]{6,}" /tmp/ss_gtm.js | sort -u | head
done
