Cloudflare Setup

Deploy your tag bundles to Cloudflare's global edge network for sub-millisecond latency and infinite scalability.

Architecture

YourTM uses Cloudflare R2 (object storage) for maximum performance and cost efficiency:

🌐 Your Website
<script src="https://cdn.yourdomain.com/latest/core.js" async></script>
Cloudflare Edge (300+ PoPs)
📦 R2 Static CDN
  • Direct CDN cache delivery
  • No Worker overhead for bundles
  • Versioned & Latest URLs
  • Latency: 5-20ms global
Worker (API Only)
  • Telemetry collection
  • Live Preview mode
  • Health & Status checks
  • Zero-cost for bundle reads

The Edge-Native Advantage

YourTM is the first tag manager built from the ground up to leverage the Cloudflare Edge. By using R2 Object Storage and the global CDN, we've eliminated the performance "tax" associated with traditional tag managers.

Sub-Millisecond Execution

Unlike GTM, which requires a Worker-like invocation to "stitch" tags together, YourTM serves pre-compiled, immutable bundles directly from Cloudflare's edge cache. This results in global latency of 5-20ms.

💰 Near-Zero Cost

By serving static assets through R2, you bypass Worker invocation costs. A site with 10 million page views costs roughly $3.60/month to track, which easily fits within most Cloudflare free tiers.

Key Benefits

Requirements

To deploy to Cloudflare, you'll need:

Requirement Details
Cloudflare Account Free or paid account at dash.cloudflare.com
Workers Enabled Workers are enabled by default on all accounts
API Token Token with Workers and KV permissions
Account ID Found in your Cloudflare dashboard sidebar

Cloudflare Pricing

With R2 architecture, YourTM is extremely cost-effective:

R2 Storage (Static Bundles)

Resource Free Tier Paid
Storage 10 GB/month $0.015/GB/month
Class A ops (writes) 1M/month $4.50/million
Class B ops (reads) 10M/month $0.36/million

Workers (API Only - Optional)

Resource Free Tier Paid ($5/mo)
Requests 100,000/day 10M/month
Cost Example: A site with 1 million daily page views:
R2 reads: ~1M/day × 30 = 30M/month = $10.80/month
Worker API: Telemetry only = minimal usage
Total: ~$11/month (vs ~$15-25 with Workers + KV)
For most websites, the free tier is sufficient. A site with 100,000 daily page views uses only ~3M R2 reads/month — well within the free 10M/month allowance.

Step 1: Create a Cloudflare Account

If you don't have a Cloudflare account:

1 Sign up at Cloudflare

Go to dash.cloudflare.com/sign-up and create a free account.

2 Find your Account ID

After logging in, your Account ID is displayed in the right sidebar of the dashboard. It looks like: a1b2c3d4e5f6g7h8i9j0...

You can also find it in the URL: dash.cloudflare.com/ACCOUNT_ID/...

Step 2: Create an API Token

YourTM needs an API token to deploy Workers and manage KV storage.

1 Go to API Tokens

Navigate to My Profile → API Tokens or go directly to:

https://dash.cloudflare.com/profile/api-tokens

2 Create Custom Token

Click "Create Token", then "Create Custom Token".

3 Configure Permissions

Set the following permissions:

Permission Access Level Purpose
Account → R2 Storage Edit Create buckets, upload bundles
Account → Workers Scripts Edit Optional: Deploy API worker (telemetry)
Account → Workers KV Storage Edit Optional: Legacy mode or metadata
Zone → Workers Routes Edit Optional: Custom domain routing
Account Resources: Select "Include → All accounts" or your specific account.

4 Copy the Token

After creating, copy the token immediately. It won't be shown again!

The token looks like: Abc123XyzLongTokenString...

Step 3: Run Deploy Init

The ytm deploy init command sets up everything automatically:

ytm deploy init

The interactive wizard will prompt you:

🚀 YourTM Cloudflare Setup
──────────────────────────────

  Container ID   YTM-MYSITE
  Container Name My Website Tags

? Cloudflare Account ID: a1b2c3d4e5f6g7h8i9j0
? Cloudflare API Token: ****************************

✔ API token validated

? Which environment(s) to set up?
  ❯ Production only
    Staging only
    Both (staging + production)

? Storage mode for tag bundles:
  ❯ R2 Bucket (Recommended) - Fast CDN delivery, no Worker invocations
    KV Namespace (Legacy)   - Worker serves all requests

✔ Created R2 bucket "ytm-mysite"
✔ Public access enabled: https://pub-a1b2c3d4.r2.dev/ytm-mysite

? Deploy API worker for telemetry and preview mode? (optional) No

✔ Updated yourtm.config.ts

🎉 Cloudflare setup complete!

Created resources:
  R2 Bucket: ytm-mysite
    Public URL: https://pub-a1b2c3d4.r2.dev/ytm-mysite

Add this to your site:
  <script src="https://pub-a1b2c3d4.r2.dev/ytm-mysite/latest/core.js" async></script>

Architecture:
  • Static bundles served from R2 CDN (fast, low cost)
  • No Worker invocations for bundle requests

Next steps:
  1. Run 'ytm build' to compile your tags
  2. Run 'ytm deploy' to publish to Cloudflare

Command Options

Option Description
--account-id <id> Cloudflare Account ID (skip prompt)
--api-token <token> Cloudflare API Token (skip prompt)
--r2-bucket <name> Custom R2 bucket name
--r2-domain <domain> Custom domain for R2 (e.g., cdn.yourdomain.com)
--worker-name <name> Custom worker name (for API worker)
--env <environment> staging, production, or both
--skip-worker Skip deploying the API worker
--legacy-kv Use legacy KV-only mode (not recommended)
--save-credentials Save to ~/.yourtm/credentials.json
-y, --yes Skip confirmation prompts

CI/CD Setup

For automated deployments, use environment variables or flags:

ytm deploy init \
  --account-id $CLOUDFLARE_ACCOUNT_ID \
  --api-token $CLOUDFLARE_API_TOKEN \
  --env production \
  --yes

Configuration File

After running deploy init, your config is updated:

yourtm.config.ts
import { defineConfig } from '@yourtm/core'

export default defineConfig({
  container: {
    id: 'YTM-MYSITE',
    name: 'My Website Tags',
  },

  // Cloudflare configuration (added by deploy init)
  cloudflare: {
    accountId: 'a1b2c3d4e5f6g7h8i9j0',
    // R2 bucket for static bundles (recommended)
    r2BucketName: 'ytm-mysite',
    r2PublicUrl: 'https://pub-a1b2c3d4.r2.dev/ytm-mysite',
    // Optional: Custom domain
    // r2PublicUrl: 'https://cdn.mysite.com',
    // Optional: Worker for API (telemetry, preview)
    // workerUrl: 'https://ytm-api.workers.dev',
  },
})

Legacy KV Configuration

If using legacy KV mode (--legacy-kv):

yourtm.config.ts
  cloudflare: {
    accountId: 'a1b2c3d4e5f6g7h8i9j0',
    workerName: 'yourtm-cdn',
    kvNamespaceId: 'kv-namespace-id-production',
  },

Credentials Storage

YourTM looks for credentials in this order:

  1. Environment Variables (recommended for CI/CD)
    export CLOUDFLARE_API_TOKEN="your-token"
    export CLOUDFLARE_ACCOUNT_ID="your-account-id"
  2. Credentials File (for local development)
    ~/.yourtm/credentials.json
    {
      "cloudflare": {
        "apiToken": "your-token",
        "accountId": "your-account-id"
      }
    }
  3. Config File (not recommended for tokens)
    cloudflare: {
      accountId: 'your-account-id',
      // Don't put apiToken in config - use env vars
    }
Security Warning: Never commit API tokens to git. Use environment variables or the credentials file.

Multiple Environments

YourTM supports staging and production environments with separate storage:

# Set up both environments
ytm deploy init --env both

# Deploy to staging first
ytm deploy -e staging

# Then promote to production
ytm deploy -e production

Each environment has its own:

Deploying

After initial setup, deploy with:

# Build and deploy to staging
ytm build && ytm deploy -e staging

# Deploy to production
ytm deploy -e production

# Deploy with a message
ytm deploy -e production -m "Added TikTok pixel"

Deploy output:

Deploying to production...
──────────────────────────────

  Container     YTM-MYSITE
  Environment   production
  Version       v1.2.3

Uploading bundles:
  ✔ core.js                 2.06 KB
  ✔ zone-global.js          1.92 KB
  ✔ zone-checkout.js          635 B
  ✔ zone-product.js           229 B

✔ Deploy successful!

  CDN URL: https://yourtm-cdn.your.workers.dev/c/YTM-MYSITE/core.js
  Version: v1.2.3
  Time:    1.2s

Rollback

If something goes wrong, rollback instantly:

# Quick rollback to previous version
ytm rollback quick -e production

# Rollback to specific version
ytm rollback v1.2.2 -e production

# View version history
ytm deploy versions -e production

Custom Domain

By default, your bundles are served from:

https://yourtm-cdn.YOUR_SUBDOMAIN.workers.dev/c/CONTAINER_ID/core.js

To use a custom domain like cdn.yourdomain.com:

1 Add Domain to Cloudflare

Your domain must be on Cloudflare (either full setup or CNAME setup).

2 Add Worker Route

In Cloudflare Dashboard → Workers → your worker → Triggers:

Add a route: cdn.yourdomain.com/*

3 Add DNS Record

Add a CNAME or A record pointing to your workers.dev subdomain.

4 Update Script Tag

<script src="https://cdn.yourdomain.com/latest/core.js"></script>

Troubleshooting

Authentication Errors

Error: Authentication failed (10000)

Solution: Check that your API token has the correct permissions and hasn't expired.

KV Namespace Not Found

Error: KV namespace not found

Solution: Run ytm deploy init again or check that the namespace ID in config is correct.

Worker Deploy Failed

Error: Worker upload failed

Solutions:

Slow Response Times

If bundle loading is slow:

Getting Help

If you're still having issues: