Skip to content

Deploying the Site Integrations Client SDK

Embed the Shaf Edge JavaScript SDK to automatically rewrite outbound links and capture conversion attribution.

Deploying the Site Integrations Client SDK

Manually creating short links for hundreds of static blog posts and outbound affiliate anchors creates maintenance overhead. This guide embeds Shaf’s edge-compiled JavaScript SDK into your web application to dynamically convert outbound links into tracked short links with zero build steps.


Prerequisites

  • Workspace permissions: Owner or Admin role in your Shaf organization.
  • An active branded short domain (e.g., go.acme.com) or system domain (shaf.is).
  • Access to your web application’s root template or HTML layout <head>.

Implementation Recipe

  1. Create a Site Integration Key

    In the Shaf Dashboard, navigate to Settings → Site Integrations and click New Integration, or dispatch an API request:

    curl -X POST "https://shaf.app/api/v1/integrations" \
      -H "Authorization: Bearer $SHAF_API_TOKEN" \
      -H "Content-Type: application/json" \
      -d '{
        "name": "Production Marketing Site",
        "allowedOrigins": ["https://acme.com", "https://blog.acme.com"],
        "targetDomain": "go.acme.com",
        "autoRewrite": true,
        "captureAttribution": true
      }'

    Expected response (201 Created):

    {
      "siteKey": "site_live_9b4e1a0c2d3e4f5a",
      "name": "Production Marketing Site",
      "targetDomain": "go.acme.com",
      "sdkUrl": "https://shaf.app/sdk/site_live_9b4e1a0c2d3e4f5a.js"
    }
  2. Embed the Edge SDK

    Include the lightweight (sub-3KB) script in your application template:

    Add the script tag before the closing </head> tag:

    <script
      src="https://shaf.app/sdk/site_live_9b4e1a0c2d3e4f5a.js"
      async
      data-shaf-auto-rewrite="true"
      data-shaf-attribution="true"
      data-shaf-exclude="acme.com,auth.acme.com"
    ></script>

    Add Script to app/layout.tsx:

    import Script from 'next/script';
    
    export default function RootLayout({ children }: { children: React.ReactNode }) {
      return (
        <html lang="en">
          <head>
            <Script
              src="https://shaf.app/sdk/site_live_9b4e1a0c2d3e4f5a.js"
              strategy="afterInteractive"
              data-shaf-auto-rewrite="true"
              data-shaf-attribution="true"
            />
          </head>
          <body>{children}</body>
        </html>
      );
    }

    Inject the script via app.vue or nuxt.config.ts:

    // nuxt.config.ts
    export default defineNuxtConfig({
      app: {
        head: {
          script: [
            {
              src: 'https://shaf.app/sdk/site_live_9b4e1a0c2d3e4f5a.js',
              async: true,
              'data-shaf-auto-rewrite': 'true',
              'data-shaf-attribution': 'true',
            },
          ],
        },
      },
    });

    Generate links programmatically on client or server using the standalone SDK:

    import { ShafClient } from '@shaf/sdk';
    
    const client = new ShafClient({
      token: process.env.SHAF_API_TOKEN,
      domain: 'go.acme.com',
    });
    
    const link = await client.links.create({
      url: 'https://github.com/shaf-is/shaf',
      slug: 'dynamic-docs',
      tags: ['sdk', 'automated'],
    });
    
    console.log(`Generated short link: ${link.shortUrl}`);
  3. Configure Link Rewrite Exclusions

    Prevent internal navigation anchors from being rewritten by defining excluded domains with the data-shaf-exclude attribute or class .no-shaf:

    <!-- Explicitly exclude an individual anchor -->
    <a href="https://partner.com/signup" class="no-shaf">Untracked Partner Link</a>

Verification

Inspect DOM mutation and network requests in your browser Developer Tools:

  1. Open your website in a browser and check the Network tab for the SDK bundle:

    curl -I "https://shaf.app/sdk/site_live_9b4e1a0c2d3e4f5a.js"

    Expected headers:

    HTTP/2 200 OK
    content-type: application/javascript; charset=utf-8
    cache-control: public, max-age=3600, stale-while-revalidate=86400
  2. Inspect any outbound anchor (<a href="https://external-product.com">) on your page. The SDK transforms it into your branded short path upon DOM load:

    <a href="https://go.acme.com/r/site_live_9b4e1a?to=https%3A%2F%2Fexternal-product.com">External Product</a>

Common Gotchas

Issue / Error Root Cause Solution
Script blocked by CSP Content Security Policy forbids external script sources. Add https://shaf.app to your CSP script-src and connect-src directives.
Internal navigation broken Internal hostnames were rewritten into short links. Add internal domains to data-shaf-exclude="acme.com,login.acme.com".
Hydration mismatch in React SDK mutated anchor tags before React completed hydration. Use strategy="afterInteractive" in Next.js or initialize inside useEffect().
Clicks not registering Allowed origins in Shaf integration settings do not match site domain. Update allowedOrigins in dashboard settings to include all staging and production hostnames.

Type to search…

↑↓ navigate↵ selectEsc close