# 📊 How to Connect PlantGo Email Signups to Google Sheets

PlantGo is pre-configured to send waitlist emails, platform preferences, and queue numbers directly into a **Google Sheet** in real-time without requiring any paid backend or database.

---

## ⚡ 60-Second Setup Instructions

### Step 1: Create a Google Sheet
1. Go to [Google Sheets](https://sheets.new) and create a new blank spreadsheet.
2. Rename the spreadsheet to: **`PlantGo VIP Waitlist`** (or any name you prefer).

---

### Step 2: Open Apps Script
1. In the top menu of your Google Sheet, click **Extensions** &rarr; **Apps Script**.
2. Delete any existing code in the `Code.gs` editor.

---

### Step 3: Paste this Google Apps Script
Copy and paste the entire script below into `Code.gs`:

```javascript
/**
 * PlantGo Landing Page - Google Sheets Webhook
 * Automatically logs email signups with platform and queue info.
 */
function doPost(e) {
  var lock = LockService.getScriptLock();
  lock.tryLock(10000);

  try {
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

    // Auto-create headers if the sheet is empty
    if (sheet.getLastRow() === 0) {
      sheet.appendRow([
        "Timestamp",
        "Email Address",
        "Target Platform",
        "Queue Number",
        "Signup Location"
      ]);
      // Style header row
      sheet.getRange(1, 1, 1, 5)
        .setFontWeight("bold")
        .setBackground("#10b981")
        .setFontColor("#ffffff");
      sheet.setFrozenRows(1);
    }

    var params = e.parameter || {};
    var timestamp = params.timestamp || new Date().toISOString();
    var email = params.email || "";
    var platform = params.platform || "Not specified";
    var queueNumber = params.queue_number || "";
    var source = params.source || "Website Form";

    // Only append if email is provided
    if (email) {
      sheet.appendRow([
        timestamp,
        email,
        platform,
        queueNumber,
        source
      ]);
    }

    return ContentService
      .createTextOutput(JSON.stringify({ "status": "success", "row": sheet.getLastRow() }))
      .setMimeType(ContentService.MimeType.JSON);

  } catch (error) {
    return ContentService
      .createTextOutput(JSON.stringify({ "status": "error", "message": error.toString() }))
      .setMimeType(ContentService.MimeType.JSON);
  } finally {
    lock.releaseLock();
  }
}

function doGet(e) {
  return ContentService
    .createTextOutput(JSON.stringify({ "status": "active", "service": "PlantGo Waitlist Webhook" }))
    .setMimeType(ContentService.MimeType.JSON);
}
```

3. Click the **Save** icon (💾) at the top of the editor.

---

### Step 4: Deploy as a Web App
1. In the upper-right corner of the Apps Script window, click the blue **Deploy** button &rarr; choose **New deployment**.
2. Next to *Select type*, click the gear icon (⚙️) and select **Web app**.
3. Fill in the deployment details:
   - **Description:** `PlantGo Email Collector`
   - **Execute as:** `Me (your google account)`
   - **Who has access:** **`Anyone`** *(CRITICAL: Select "Anyone" so the landing page can post form data!)*
4. Click **Deploy**.
5. Google will ask you to *Authorize Access*:
   - Click **Authorize access** &rarr; select your Google account.
   - Click **Advanced** &rarr; click **Go to Untitled project (unsafe)** &rarr; click **Allow**.
6. Copy the **Web App URL** generated (it looks like: `https://script.google.com/macros/s/AKfycb.../exec`).

---

### Step 5: Connect It to PlantGo

You can connect your URL in either of two ways:

#### Option A: Through the Website UI (Instant)
1. Open the website at `http://localhost:3000`.
2. Scroll to the waitlist box and click the **⚙️ Config Sheet** button (or the footer link).
3. Paste your Web App URL and click **Save & Connect Sheet**.
4. Click **Send Test Email** to verify a test row appears in your Google Sheet immediately!

#### Option B: In Code (`app.js`)
Open `app.js` and set your URL on line 8:
```javascript
const GOOGLE_SHEET_WEBHOOK_URL = 'https://script.google.com/macros/s/YOUR_SCRIPT_ID/exec';
```

---

## 📋 What Gets Stored in the Google Sheet?

| Column A | Column B | Column C | Column D | Column E |
| :--- | :--- | :--- | :--- | :--- |
| **Timestamp** | **Email Address** | **Target Platform** | **Queue Number** | **Signup Location** |
| `2026-10-04 21:20:00` | `explorer@example.com` | `iOS` | `#24,813` | `Hero Waitlist Form` |
| `2026-10-04 21:22:15` | `trainer@gmail.com` | `Android` | `#24,814` | `Perks Section Form` |

