{"id":29976167,"url":"https://github.com/petermann/bricks-google-sheets-integration","last_synced_at":"2025-08-04T08:43:19.007Z","repository":{"id":307580595,"uuid":"1030012705","full_name":"petermann/bricks-google-sheets-integration","owner":"petermann","description":"Bricks Form to Google Sheets Integration","archived":false,"fork":false,"pushed_at":"2025-08-01T00:28:29.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-01T02:33:58.694Z","etag":null,"topics":["bricksbuilder","google-sheets","wordpress"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/petermann.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-08-01T00:27:59.000Z","updated_at":"2025-08-01T00:35:17.000Z","dependencies_parsed_at":"2025-08-01T02:34:03.923Z","dependency_job_id":"6d59d175-7848-4c91-ba75-0498d5d73f99","html_url":"https://github.com/petermann/bricks-google-sheets-integration","commit_stats":null,"previous_names":["petermann/bricks-google-sheets-integration"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/petermann/bricks-google-sheets-integration","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petermann%2Fbricks-google-sheets-integration","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petermann%2Fbricks-google-sheets-integration/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petermann%2Fbricks-google-sheets-integration/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petermann%2Fbricks-google-sheets-integration/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/petermann","download_url":"https://codeload.github.com/petermann/bricks-google-sheets-integration/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petermann%2Fbricks-google-sheets-integration/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268672171,"owners_count":24288244,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-04T02:00:09.867Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bricksbuilder","google-sheets","wordpress"],"created_at":"2025-08-04T08:43:16.847Z","updated_at":"2025-08-04T08:43:18.977Z","avatar_url":"https://github.com/petermann.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Bricks Form to Google Sheets Integration\n\nThis guide explains how to send Bricks form submissions directly to Google Sheets using a custom action in your WordPress child theme. Optionally, you can use a popup script to pre-fill product data.\n\n---\n\n## 1. Add Google Sheets Action to Bricks Form\n\nAdd the following code to your child theme's `functions.php` to enable the Google Sheets action in Bricks forms:\n\n```php\n// Add controls for Google Sheets action\nadd_filter('bricks/elements/form/controls', function($controls) {\n    $controls['actions']['options']['googlesheets'] = esc_html__('Send to Google Sheets', 'bricks-child');\n    $controls['googleSheetsEndpoint'] = [\n        'group'       =\u003e 'googleSheets',\n        'label'       =\u003e esc_html__('Endpoint URL', 'bricks-child'),\n        'type'        =\u003e 'text',\n        'description' =\u003e sprintf(\n            '%s \u003ca href=\"%s\" target=\"_blank\" rel=\"noopener\"\u003e%s\u003c/a\u003e',\n            esc_html__('Paste your Google Sheets endpoint URL here.', 'bricks-child'),\n            'https://github.com/petermann/bricks-google-sheets-integration/?tab=readme-ov-file#3-google-apps-script-for-receiving-webhook-data',\n            esc_html__('Learn more', 'bricks-child')\n        ),\n    ];\n    $controls['googleSheetsJson'] = [\n        'group'       =\u003e 'googleSheets',\n        'label'       =\u003e esc_html__('JSON Model', 'bricks-child'),\n        'type'        =\u003e 'textarea',\n        'description' =\u003e sprintf(\n            '%s %s \u003ca href=\"%s\" target=\"_blank\" rel=\"noopener\"\u003e%s\u003c/a\u003e',\n            esc_html__('Use the field ID from your Bricks form for each value. Example: { \"name\": \"{{form_field_id}}\", \"email\": \"{{form_field_id}}\", \"phone\": \"{{form_field_id}}\", \"product\": \"{{form_field_id}}\" }', 'bricks-child'),\n            esc_html__('Set the correct field ID for each value to send the submitted data to Google Sheets.', 'bricks-child'),\n            'https://github.com/petermann/bricks-google-sheets-integration/?tab=readme-ov-file#2-example-json-model',\n            esc_html__('Learn more', 'bricks-child')\n        ),\n    ];\n    return $controls;\n});\n\n// Add control group for Google Sheets\nadd_filter('bricks/elements/form/control_groups', function($control_groups) {\n    $control_groups['googleSheets'] = [\n        'title'    =\u003e esc_html__('Google Sheets', 'bricks-child'),\n        'required' =\u003e ['actions', '=', 'googlesheets'],\n    ];\n    return $control_groups;\n});\n\n// Custom action logic\nadd_action('bricks/form/action/googlesheets', function($form) {\n    $settings = $form-\u003eget_settings();\n    $fields   = $form-\u003eget_fields();\n    if (empty($settings['googleSheetsEndpoint'])) {\n        error_log('Google Sheets: Endpoint URL is missing.');\n        $form-\u003eset_result([\n            'action'  =\u003e 'googlesheets',\n            'type'    =\u003e 'error',\n            'message' =\u003e current_user_can('manage_options')\n                ? esc_html__('Google Sheets: Endpoint URL is required.', 'bricks-child')\n                : esc_html__('An error occurred. Please try again later.', 'bricks-child')\n        ]);\n        return;\n    }\n    if (empty($settings['googleSheetsJson'])) {\n        error_log('Google Sheets: JSON Model is missing.');\n        $form-\u003eset_result([\n            'action'  =\u003e 'googlesheets',\n            'type'    =\u003e 'error',\n            'message' =\u003e current_user_can('manage_options')\n                ? esc_html__('Google Sheets: JSON Model is required.', 'bricks-child')\n                : esc_html__('An error occurred. Please try again later.', 'bricks-child')\n        ]);\n        return;\n    }\n    if (empty($fields)) {\n        error_log('Google Sheets: No form fields submitted.');\n        $form-\u003eset_result([\n            'action'  =\u003e 'googlesheets',\n            'type'    =\u003e 'error',\n            'message' =\u003e current_user_can('manage_options')\n                ? esc_html__('Google Sheets: No form fields submitted.', 'bricks-child')\n                : esc_html__('An error occurred. Please try again later.', 'bricks-child')\n        ]);\n        return;\n    }\n    // Replace {{field_id}} with form values\n    $json_model = $settings['googleSheetsJson'];\n    if (\n        !is_string($json_model) ||\n        trim($json_model)[0] !== '{' ||\n        trim($json_model)[strlen(trim($json_model))-1] !== '}' ||\n        !preg_match('/{{[a-zA-Z0-9_]+}}/', $json_model)\n    ) {\n        error_log('Google Sheets: Invalid JSON model!');\n        $form-\u003eset_result([\n            'action'  =\u003e 'googlesheets',\n            'type'    =\u003e 'error',\n            'message' =\u003e current_user_can('manage_options')\n                ? esc_html__('Google Sheets: Invalid JSON model.', 'bricks-child')\n                : esc_html__('An error occurred. Please try again later.', 'bricks-child')\n        ]);\n        return;\n    }\n    foreach ($fields as $key =\u003e $value) {\n        if (strpos($key, 'form-field-') === 0) {\n            $field_id = substr($key, 11);\n            $replace_value = is_array($value) ? implode(', ', $value) : $value;\n            $replace_value = sanitize_text_field($replace_value);\n            $json_model = str_replace('{{' . $field_id . '}}', $replace_value, $json_model);\n        }\n    }\n    $json_model = preg_replace('/{{[a-zA-Z0-9_]+}}/', '\"\"', $json_model);\n    $endpoint = esc_url_raw($settings['googleSheetsEndpoint']);\n    $response = wp_remote_post($endpoint, [\n        'method'      =\u003e 'POST',\n        'headers'     =\u003e ['Content-Type' =\u003e 'application/json'],\n        'body'        =\u003e $json_model,\n        'data_format' =\u003e 'body'\n    ]);\n    if (is_wp_error($response)) {\n        error_log('Google Sheets error: ' . $response-\u003eget_error_message());\n        $form-\u003eset_result([\n            'action'  =\u003e 'googlesheets',\n            'type'    =\u003e 'error',\n            'message' =\u003e current_user_can('manage_options')\n                ? esc_html__('Google Sheets error: ', 'bricks-child') . $response-\u003eget_error_message()\n                : esc_html__('An error occurred. Please try again later.', 'bricks-child')\n        ]);\n        return;\n    }\n});\n```\n\n**Instructions:**\n- Paste the code above into your child theme’s `functions.php`.\n- In the Bricks form builder, select the “Send to Google Sheets” action.\n- Fill in the Endpoint URL and JSON Model fields as described above.\n\n---\n\n## 2. Example JSON Model\n\nUse this template in the JSON Model field. Replace the field IDs with those from your form:\n\n```\n{\n  \"name\": \"{{form_field_id}}\",\n  \"email\": \"{{form_field_id}}\",\n  \"phone\": \"{{form_field_id}}\",\n  \"product\": \"{{form_field_id}}\"\n}\n```\n\n---\n\n## 3. Google Apps Script for Receiving Webhook Data\n\nCreate a new Google Apps Script attached to your Google Sheet and paste the following code:\n\n```javascript\nfunction doPost(e) {\n  var sheet = SpreadsheetApp.getActiveSheet();\n  var data = JSON.parse(e.postData.contents);\n  function processArray(value) {\n    if (Array.isArray(value)) {\n      return value.join(', ');\n    }\n    return value;\n  }\n  var row = [\n    processArray(data.name),\n    processArray(data.email),\n    processArray(data.phone),\n    processArray(data.product),\n    new Date()\n  ];\n  sheet.appendRow(row);\n  return ContentService.createTextOutput(JSON.stringify({ result: \"success\" }))\n    .setMimeType(ContentService.MimeType.JSON);\n}\n```\n\n**Setup Steps:**\n1. Open your Google Sheet.\n2. Go to Extensions \u003e Apps Script.\n3. Paste the code above and save.\n4. Deploy as a web app (set access to \"Anyone, even anonymous\").\n5. Copy the web app URL to use as your webhook endpoint in Bricks.\n\n\n---\n\n## 4. (Optional) Open Bricks Form Popup and Set Product\n\nAdd this JavaScript to your site if you want to open a popup and pre-fill a product field:\n\n```js\nfunction openPopupSubscribe(product) {\n  let popupId = 1; // Replace with your actual popup ID\n  const productName = product || 'No product selected.';\n  const productField = document.querySelector('input[name=\"product\"]');\n  if (productField) {\n    productField.value = productName;\n  } else {\n    console.warn('Hidden input with name \"product\" was not found in the DOM.');\n  }\n  bricksOpenPopup(popupId);\n}\n```\n\n---\n\n## References\n\n- [Bricks Builder Documentation](https://academy.bricksbuilder.io/article/form-element/)\n- [Google Apps Script Web Apps](https://developers.google.com/apps-script/guides/web)\n\n---\n\nBy [Ivan Petermann](https://ivanpetermann.com/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetermann%2Fbricks-google-sheets-integration","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetermann%2Fbricks-google-sheets-integration","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetermann%2Fbricks-google-sheets-integration/lists"}