오늘날의 웹사이트에서는 많은 동적 콘텐츠를 표시하는 데 자바스크립트를 사용합니다. JavaScript를 사용하여 웹사이트에서 구조화된 데이터를 생성할 때 주의해야 할 몇 가지 사항이 있습니다. 본 가이드에서는 권장사항과 구현 전략을 설명합니다. 구조화된 데이터를 처음 사용한다면 구조화된 데이터의 작동 방식을 자세히 알아보세요.
자바스크립트로 구조화된 데이터를 생성하는 방법에는 여러 가지가 있지만 가장 일반적인 방법은 다음과 같습니다.
Google 태그 관리자(GTM)는 변수를 지원해 페이지에 있는 정보를 구조화된 데이터의 일부로 사용하도록 합니다. GTM에서 정보를 복제하는 대신 변수를 사용하여 페이지에서 구조화된 데이터를 추출합니다. GTM에서 정보를 복제하면 페이지 콘텐츠와 GTM을 통해 삽입된 구조화된 데이터가 일치하지 않을 위험이 커집니다.
예를 들어 이름이 다음과 같은 맞춤 변수를 생성하여 레시피 이름이 페이지 제목인 레시피 JSON-LD 블록을 동적으로 생성할 수 있습니다. recipe_name:
function(){returndocument.title;}
그런 다음 맞춤 태그 HTML에서 {{recipe_name}}을 사용할 수 있습니다.
변수를 사용하는 페이지에서 필요한 모든 정보를 수집하기 위해 변수를 만드는 것이 좋습니다.
구조화된 데이터를 생성하는 또 다른 방법은 JavaScript를 사용하여 구조화된 데이터를 모두 생성하거나 서버 측에서 렌더링된 구조화된 데이터에 더 많은 정보를 추가하는 것입니다. 어떻게 하든 Google 검색에서는 페이지를 렌더링할 때 DOM에서 사용 가능한 구조화된 데이터를 이해하고 처리할 수 있습니다. Google 검색에서 JavaScript를 처리하는 방법을 자세히 알아보려면 JavaScript 기본 가이드를 참조하세요.
성공: 올바르게 작업했으며 구조화된 데이터 유형이 도구에서 지원되면 '페이지가 리치 결과 기능에 적합함'이라는 메시지가 표시됩니다. 리치 결과 테스트에서 지원하지 않는 구조화된 데이터 유형을 테스트하는 경우 렌더링된 HTML을 확인하세요. 렌더링된 HTML에 구조화된 데이터가 포함되어 있으면 Google 검색에서 처리할 수 있습니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-08-04(UTC)"],[[["\u003cp\u003eThis guide provides best practices for implementing structured data with JavaScript on your website to enhance search engine understanding.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Tag Manager and custom JavaScript code are the primary methods for dynamically generating structured data.\u003c/p\u003e\n"],["\u003cp\u003eWhen using JavaScript, ensure your server has enough resources to handle potential increased traffic from Google, especially with \u003ccode\u003eProduct\u003c/code\u003e markup.\u003c/p\u003e\n"],["\u003cp\u003eValidate your structured data implementation using the Rich Results Test to identify and resolve any errors or warnings.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the comprehensive structured data documentation and JavaScript troubleshooting guide for detailed information and support.\u003c/p\u003e\n"]]],["JavaScript dynamically generates website content and structured data through methods like Google Tag Manager (GTM) or custom JavaScript. GTM allows adding structured data via Custom HTML tags and using variables to avoid data duplication. Custom JavaScript can generate or augment structured data, which becomes available in the DOM. Server-side rendering can also include structured data. Implementation should be validated with the Rich Results Test, ensuring the data is crawlable and indexable by Google Search.\n"],null,["Generate structured data with JavaScript\n\nModern websites use JavaScript to display lots of dynamic content. There are a\nfew things you need to look out for when using JavaScript to generate structured data on your\nwebsites, and this guide covers best practices and implementation strategies. If you're new to\nstructured data, you can learn more about\n[how structured data works](/search/docs/appearance/structured-data/intro-structured-data).\n\nThere are different ways to generate structured data with JavaScript, but the most common are:\n\n- [Google Tag Manager](#use-google-tag-manager)\n- [Custom JavaScript](#custom-javascript)\n\n| **Using `Product` markup?** Be aware that dynamically-generated markup can make Shopping crawls less frequent and less reliable, which can be an issue for fast-changing content like product availability and price. If you're a merchant optimizing for all types of shopping results, make sure your server has enough computing resources to handle increased traffic from Google.\n\nUse Google Tag Manager to generate JSON-LD dynamically\n\n[Google Tag Manager](https://tagmanager.google.com/) is a\nplatform that lets you manage tags on your website without editing the code. To generate structured\ndata with Google Tag Manager, follow these steps:\n\n1. [Set up and install Google Tag Manager](https://support.google.com/tagmanager/answer/6103696) on your site.\n2. Add a new **Custom HTML** tag to the container.\n3. Paste a [supported structured data](/search/docs/guides/search-gallery) block into the tag content.\n4. Install the container as shown in the **Install Google Tag Manager** section of your container's admin menu.\n5. To add the tag to your website, publish your container in the Google Tag Manager interface.\n6. [Test your implementation](#testing).\n\nUsing variables in Google Tag Manager\n\nGoogle Tag Manager (GTM) supports [variables](https://support.google.com/tagmanager/topic/7683268?ref_topic=3441647)\nto use information on the page as part of your structured data. Use variables to extract\nthe structured data from the page instead of duplicating the information in GTM. Duplicating the information in GTM increases\nthe risk of having a mismatch between page content and the structured data inserted using GTM.\n\nFor example, you can dynamically create\na [Recipe](/search/docs/appearance/structured-data/recipe) JSON-LD block\nthat uses the page title as the recipe name by creating the following custom variable named\n`recipe_name`: \n\n```javascript\nfunction() { return document.title; }\n```\n\nYou can then use `{{recipe_name}}` in your custom tag HTML.\n\nWe recommend to create variables to collect all the necessary information from the page using variables.\n\nHere is an example for the custom HTML tag content: \n\n```javascript\n\u003cscript type=\"application/ld+json\"\u003e\n {\n \"@context\": \"https://schema.org/\",\n \"@type\": \"Recipe\",\n \"name\": \"{{recipe_name}}\",\n \"image\": [ \"{{recipe_image}}\" ],\n \"author\": {\n \"@type\": \"Person\",\n \"name\": \"{{recipe_author}}\"\n }\n }\n\u003c/script\u003e\n```\n| **Note:** The previous sample assumes that you defined the variables `recipe_name`, `recipe_image` and `recipe_author` in GTM.\n\nGenerate structured data with custom JavaScript\n\nAnother way you can generate structured data is by using JavaScript to either generate all of\nyour structured data or add more information to the server-side rendered structured data. Either way,\nGoogle Search can understand and process structured data that's available in the DOM when it\nrenders the page. To learn more about how Google Search processes JavaScript, check out the\n[JavaScript basics guide](/search/docs/crawling-indexing/javascript/javascript-seo-basics).\n\nHere is an example of JavaScript-generated structured data:\n\n1. [Find the type of\n structured data](/search/docs/appearance/structured-data/search-gallery) you are interested in.\n2. Edit your website's HTML to include a JavaScript snippet like the following example (refer to the documentation from your CMS or hosting provider, or ask your developers). \n\n ```javascript\n fetch('https://api.example.com/recipes/123')\n .then(response =\u003e response.text())\n .then(structuredDataText =\u003e {\n const script = document.createElement('script');\n script.setAttribute('type', 'application/ld+json');\n script.textContent = structuredDataText;\n document.head.appendChild(script);\n });\n ```\n3. [Test your implementation with the Rich Results Test](#testing).\n\nUsing server-side rendering\n\nIf you are using [server-side rendering](/web/updates/2019/02/rendering-on-the-web#server-rendering),\nyou can also include the structured data in the rendered output. Check the documentation\nof your framework to find out how to generate the JSON-LD for [the type of structured data](/search/docs/appearance/structured-data/search-gallery)\nyou're interested in.\n\nTest your implementation\n\nTo make sure Google Search can crawl and index your structured data, test your implementation:\n\n1. Open the [Rich Results Test](https://goo.gle/richresults).\n2. Enter the URL that you want to test. We recommend that you use the URL input instead of the code input because there are JavaScript limitations when using the code input (for example, CORS restrictions).\n3. Click **Test URL** .\n\n **Success** : If you did everything correctly and your [structured data type is supported in the tool](https://support.google.com/webmasters/answer/7445569),\n you will see the message \"Page is eligible for rich results\". \n\n If you are testing a structured data type that is not supported by the Rich Results test, check the rendered HTML.\n If the rendered HTML contains the structured data, Google Search will be able to process it.\n\n **Try again** : If you see errors or warnings, it is most likely a syntax error or a missing property.\n Read the [documentation\n for your type of structured data](/search/docs/appearance/structured-data/search-gallery) and make sure you've added all the properties. If your problem\n persists, make sure to also check the guide on [fixing search-related JavaScript problems](/search/docs/crawling-indexing/javascript/fix-search-javascript)."]]