|
| 1 | +# RefRef Widget |
| 2 | + |
| 3 | +A customizable referral widget that can be easily embedded into any website. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Add the following script tag to your HTML: |
| 8 | + |
| 9 | +```html |
| 10 | +<script |
| 11 | + src="https://cdn.refref.io/widget.js" |
| 12 | + data-client-key="YOUR_CLIENT_KEY" |
| 13 | +></script> |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +### Basic Usage |
| 19 | + |
| 20 | +The widget can be triggered in multiple ways: |
| 21 | + |
| 22 | +1. Using a data attribute: |
| 23 | + |
| 24 | +```html |
| 25 | +<button data-refref-trigger>Refer a Friend</button> |
| 26 | +``` |
| 27 | + |
| 28 | +2. Programmatically using the RefRef object: |
| 29 | + |
| 30 | +```javascript |
| 31 | +// Open the widget |
| 32 | +window.RefRef.open(); |
| 33 | + |
| 34 | +// Close the widget |
| 35 | +window.RefRef.close(); |
| 36 | + |
| 37 | +// Toggle the widget |
| 38 | +window.RefRef.toggle(); |
| 39 | +``` |
| 40 | + |
| 41 | +### Configuration |
| 42 | + |
| 43 | +You can customize the widget's appearance and behavior: |
| 44 | + |
| 45 | +```javascript |
| 46 | +window.RefRef.setConfig({ |
| 47 | + appearance: { |
| 48 | + theme: "light", // or 'dark' |
| 49 | + primaryColor: "#4F46E5", |
| 50 | + position: "bottom-right", // 'bottom-left', 'top-right', 'top-left' |
| 51 | + buttonText: "Refer & Earn", |
| 52 | + widgetTitle: "Refer a Friend", |
| 53 | + }, |
| 54 | + campaign: { |
| 55 | + id: "your-campaign-id", |
| 56 | + name: "Your Campaign Name", |
| 57 | + rewardType: "fixed", // or 'percentage' |
| 58 | + rewardAmount: 10, |
| 59 | + referralLink: "https://your-domain.com/ref/[CODE]", |
| 60 | + }, |
| 61 | + sharing: { |
| 62 | + enabledChannels: ["email", "twitter", "facebook", "copy"], |
| 63 | + customMessage: "Check out this amazing product!", |
| 64 | + }, |
| 65 | +}); |
| 66 | +``` |
| 67 | + |
| 68 | +### Advanced: Pre-initialization Command Queue |
| 69 | + |
| 70 | +You can queue commands before the widget script loads. This is useful for setting config or initializing with user data as soon as possible. The `init` call is used to associate the widget with a specific project and participant, and optionally a token for authentication. This enables personalized referral links and campaign tracking. |
| 71 | + |
| 72 | +```html |
| 73 | +<script> |
| 74 | + window.RefRef = window.RefRef || []; |
| 75 | + window.RefRef.push([ |
| 76 | + "setConfig", |
| 77 | + { |
| 78 | + appearance: { primaryColor: "#FF0000" }, |
| 79 | + }, |
| 80 | + ]); |
| 81 | + window.RefRef.push([ |
| 82 | + "init", |
| 83 | + { |
| 84 | + projectId: "my-project", |
| 85 | + participantId: "user123", |
| 86 | + }, |
| 87 | + ]); |
| 88 | +</script> |
| 89 | +<script |
| 90 | + async |
| 91 | + src="https://cdn.refref.io/widget.js" |
| 92 | + data-client-key="YOUR_CLIENT_KEY" |
| 93 | +></script> |
| 94 | +``` |
| 95 | + |
| 96 | +### Checking Widget State |
| 97 | + |
| 98 | +```javascript |
| 99 | +// Check if the widget is open |
| 100 | +const isOpen = window.RefRef.isOpen; |
| 101 | + |
| 102 | +// Get current configuration |
| 103 | +const config = window.RefRef.getConfig(); |
| 104 | +``` |
| 105 | + |
| 106 | +### API Reference |
| 107 | + |
| 108 | +- `window.RefRef.open()`: Open the widget. |
| 109 | +- `window.RefRef.close()`: Close the widget. |
| 110 | +- `window.RefRef.toggle()`: Toggle the widget open/closed. |
| 111 | +- `window.RefRef.setConfig(config)`: Update the widget configuration. |
| 112 | +- `window.RefRef.getConfig()`: Get the current widget configuration. |
| 113 | +- `window.RefRef.isOpen`: Boolean indicating if the widget is open. |
| 114 | +- `window.RefRef.init({ projectId, participantId, token? })`: (Optional) Programmatically initialize the widget with user/project info. |
| 115 | + Makes api call to load the config data. |
| 116 | + |
| 117 | +#### `init` Method Details |
| 118 | + |
| 119 | +The `init` method is used to associate the widget instance with a specific project and participant (user). This enables personalized referral links, campaign tracking, and secure widget initialization. It is especially useful if you want to dynamically set the user context after page load or in a single-page app. |
| 120 | + |
| 121 | +**Parameters:** |
| 122 | + |
| 123 | +- `projectId` (string, required): The unique identifier for your referral campaign or project. |
| 124 | +- `participantId` (string, required): The unique identifier for the current user or participant. |
| 125 | +- `token` (string, optional): An authentication token (e.g., JWT) if your API requires secure access. |
| 126 | + |
| 127 | +**Usage Example:** |
| 128 | + |
| 129 | +```javascript |
| 130 | +window.RefRef.init({ |
| 131 | + projectId: "my-project-id", |
| 132 | + participantId: "user-123", |
| 133 | + token: "optional-jwt-token", |
| 134 | +}); |
| 135 | +``` |
| 136 | + |
| 137 | +**What it does:** |
| 138 | + |
| 139 | +- Makes a POST request to your backend to initialize the referral context. |
| 140 | +- Stores the referral link and user context in the widget state. |
| 141 | +- Enables personalized sharing and tracking for the participant. |
| 142 | +- If called before the widget script loads (via the command queue), initialization will occur as soon as the widget is ready. |
| 143 | + |
| 144 | +## Development |
| 145 | + |
| 146 | +1. Install dependencies: |
| 147 | + |
| 148 | +```bash |
| 149 | +npm install |
| 150 | +``` |
| 151 | + |
| 152 | +2. Start development server: |
| 153 | + |
| 154 | +```bash |
| 155 | +npm run dev |
| 156 | +``` |
| 157 | + |
| 158 | +3. Build for production: |
| 159 | + |
| 160 | +```bash |
| 161 | +npm run build |
| 162 | +``` |
| 163 | + |
| 164 | +## Browser Support |
| 165 | + |
| 166 | +The widget supports all modern browsers (Chrome, Firefox, Safari, Edge) and uses Shadow DOM for style isolation. |
0 commit comments