This guide is for developers using Cloud Run for server-side tagging who want to set up a private preview server.
برچسبگذاری سمت سرور به شما امکان میدهد قبل از اعمال تغییرات برچسبگذاری، آنها را در حالت پیشنمایش و اشکالزدایی پیشنمایش کنید . این بخش مهمی از گردش کار توسعه است تا اطمینان حاصل شود که برچسبهای شما مطابق انتظار رفتار میکنند. برای فعال کردن این قابلیت، به دو استقرار Cloud Run نیاز است: یکی برای سرور برچسبگذاری و دیگری برای سرور پیشنمایش. این راهنما اطلاعات بیشتری در مورد نحوه ارتباط دو سرور و نحوه پیکربندی آنها در یک محیط سازمانی ارائه میدهد.
نمای کلی
وقتی برچسبگذاری سمت سرور را در Cloud Run مستقر میکنید، میتوانید آن را با یا بدون متعادلکننده بار مستقر کنید. برای استقرارهای چند منطقهای، به یک متعادلکننده بار نیاز دارید تا ترافیک را به نزدیکترین نمونه هدایت کند.
بدون متعادل کننده بار

شکل ۱: استقرار بدون متعادلکننده بار.
با متعادل کننده بار

شکل 2: استقرار با متعادلکننده بار.
در هر دو پیادهسازی، نقطه ورود برای سرور پیشنمایش و محیط برچسبگذاری در محیط عملیاتی، یک URL یکسان است (برای مثال، sgtm.example.com ). سرور برچسبگذاری با یک متغیر محیطی به نام PREVIEW_SERVER_URL پیادهسازی میشود که نشاندهنده URL سرور پیشنمایش است.
وقتی از tagmanager.google.com بازدید میکنید و حالت پیشنمایش را باز میکنید، Tag Manager به sgtm.example.com?id=[gtm_server_id]>m_auth=[auth_id]>m_preview=[env_id] میرود و یک کوکی تنظیم میکند. وقتی از همان مرورگر برای کاوش در وبسایت هدف استفاده میکنید، درخواستهای ارسال شده به sgtm.example.com نیز کوکی را ارسال میکنند. به دلیل وجود کوکی، سرور برچسبگذاری میداند که اینها رویدادهای شما هستند، بنابراین فقط رویدادهای شما را برای مشاهده و عیبیابی به سرور پیشنمایش ارسال میکند.
The tagging server forwards these events by making HTTP requests to the URL set in the PREVIEW_SERVER_URL environment variable.
درباره سرورهای پیشنمایش عمومی
این رفتار پیشفرض مستلزم آن است که سرور پیشنمایش شما عمومی باشد. با این حال، این برای برخی سازمانها مشکلساز است زیرا محدودیتهایی در مورد اینکه چه زیرساختهایی میتوانند عمومی باشند، دارند و معمولاً اقدامات امنیتی اضافی را از طریق یک متعادلکننده بار اعمال میکنند. یک متعادلکننده بار موارد زیر را در اختیار شما قرار میدهد:
- More control over features such as SSL policies to restrict cipher suites.
- Native integration with Cloud Armor for web-application firewall (WAF) or distributed denial-of-service (DDoS).
- قابلیتهای پیشرفته مدیریت ترافیک
Route your preview server through a load balancer
برای ایجاد یک سرور پیشنمایش خصوصی، از مسیریابی مبتنی بر میزبان از طریق متعادلکننده بار با یک زیردامنه اضافی استفاده کنید. ادامه این راهنما نحوه مسیریابی سرور پیشنمایش خود را از طریق متعادلکننده بار توضیح میدهد.
پیشنیازها
- Tagging server is deployed on Cloud Run behind a load balancer.
- دسترسی به پروژه گوگل کلود
- Access to a new subdomain for the preview server, for exaple,
preview.sgtm.example.com.
Step 1: Update the environment variable with a new subdomain
To use the new preview server domain, update the server-side tagging server environment variable PREVIEW_SERVER_URL :
- برنامه Cloud Run را باز کنید.
- سرور برچسبگذاری را انتخاب کنید.
- روی ویرایش و استقرار نسخه جدید کلیک کنید.
- Under the Variables and secrets tab, in the Containers section, change the
PREVIEW_SERVER_URLto the new domain. For example:preview.sgtm.example.com.
Step 2: Configure the existing load balancer to use host-based routing
To configure the existing load balancer, use host-based routing and send traffic to the correct server:
- In Cloud Run, open the Load balancing page and click the name of your load balancer.
- روی دکمه ویرایش کلیک کنید.
- Click the Backend configuration page and open the drop down for Backend services and backend buckets .
- Click Create a backend service and complete the following:
- Give it an appropriate name (for example,
preview-backend-service). - Select the backend type as Serverless network endpoint group .
- Give it an appropriate name (for example,
- In the Backend section, create a new serverless network endpoint group and complete the following:
- Give it a name and select the region where the preview server is hosted.
- Select Cloud Run as the serverless network endpoint group type, and the preview server as the service.
- روی ایجاد کلیک کنید.
- Change other settings as needed, or keep the defaults, and click Create .
Step 3: Add a new rule and update the ingress control
To add a rule for the new host URL and update the ingress control:
- صفحه قوانین میزبان و مسیر را باز کنید.
- یک قانون جدید اضافه کنید که در آن:
- The host is the new URL:
preview.sgtm.example.com - مسیر این است:
/* - بکاند عبارت است از:
preview-backend-service
- The host is the new URL:
- Click the Update button to redeploy the load balancer.
- Back in Cloud Run open the preview server, and select the Networking tab.
- Change the Ingress control to Internal and check Allow traffic from external application load balancers so that it isn't publicly accessible.
نتیجه : معماری سرور شما اکنون باید مانند شکل 3 باشد، که در آن درخواستهای ترافیک همگی به سرور برچسبگذاری هدایت میشوند و سرور برچسبگذاری قادر است درخواستها را به سرور پیشنمایش ارسال کند.

شکل ۳: استفاده از مسیریابی مبتنی بر میزبان.
پیکربندی خود را بررسی کنید
برای بررسی پیکربندی خود:
- گوگل تگ منیجر را باز کنید .
- کانتینر سرور خود را باز کنید.
- Click Admin > Container settings and set the server container URL to the tagging server URL.
- Close the window and preview the workspace. The debug window opens. As you navigate your website, your events should appear in the debug window.