DynamicAds Cookbook

Frontend SDK Usage

Learn how to use the DynamicAds web components in your frontend.

Basic Usage

<!-- Add the SDK script -->
<script src="https://dynamicads.app/sdk.js"></script>

<!-- Use components -->
<dynamic-ad ad-id="your-ad-id"></dynamic-ad>
<dynamic-agent agent-id="your-agent-id"></dynamic-agent>

<!-- Basic styling -->
<style>
  /* Ad container styles */
  .dynamic-ad-container {
    width: 100%;
    max-width: 320px;
    margin: 0 auto;
    border-radius: 0.75rem;
    overflow: hidden;
    background: rgb(17 24 39);
    height: auto;
    display: flex;
    flex-direction: column;
    position: relative;
  }

  /* Agent container styles */
  .dynamic-agent-container {
    width: 320px;
    max-width: 320px;
    margin: 0 auto;
    position: relative;
  }

  .agent-ad {
    width: 100%;
    position: relative;
  }

  .agent-preview {
    position: absolute;
    bottom: 15px;
    right: -290px;
    width: 250px;
    min-height: 150px;
    border-radius: 0.75rem;
    overflow: visible;
    background: transparent;
    z-index: 100;
    transform: scale(0.85);
    transform-origin: bottom right;
  }

  /* Mobile responsive styles */
  @media (max-width: 640px) {
    .agent-preview {
      position: relative;
      bottom: 0;
      right: 0;
      width: 100%;
      transform: none;
      margin-top: 1rem;
    }
  }
</style>

Basic setup with components and styling. The SDK automatically handles all required dependencies.

React Integration

import { useEffect, useRef } from 'react';

function DynamicAdsComponent() {
  // Keep track of the SDK script
  const sdkScriptRef = useRef<HTMLScriptElement | null>(null);

  useEffect(() => {
    // Clean up previous script if it exists
    if (sdkScriptRef.current) {
      sdkScriptRef.current.remove();
    }

    // Add SDK script
    const script = document.createElement('script');
    script.src = 'https://dynamicads.app/sdk.js';
    script.async = true;
    script.type = 'text/javascript';
    sdkScriptRef.current = script;
    document.body.appendChild(script);

    // Clean up on unmount
    return () => {
      if (sdkScriptRef.current) {
        sdkScriptRef.current.remove();
      }
    };
  }, []); // Empty deps array means this runs once on mount

  return (
    <div>
      {/* Ad Component */}
      <div className="dynamic-ad-container">
        <dynamic-ad ad-id="your-ad-id"></dynamic-ad>
      </div>

      {/* Agent Component */}
      <div className="dynamic-agent-container">
        <div className="agent-ad">
          <dynamic-agent agent-id="your-agent-id"></dynamic-agent>
        </div>
      </div>
    </div>
  );
}

Integration with React components. The useRef hook helps manage the SDK script lifecycle, preventing memory leaks and duplicate script loading.

Advanced Configuration

// Initialize with options
DynamicAds.init({
  debug: true, // Enable debug logging
  baseUrl: 'https://custom-api.example.com' // Custom API endpoint
});

// Use with custom styling
<div class="custom-container">
  <dynamic-ad 
    ad-id="your-ad-id"
    style="max-width: 500px; margin: 0 auto;"
  ></dynamic-ad>
</div>

<style>
  .custom-container dynamic-ad {
    /* Custom styles */
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  }
</style>

Advanced configuration and styling options