Site icon DataForSEO

How to check the presence of the GTM/GA tracking code?

The presence of the GTM/GA tracking code tells whether a website is connected to the Google Tag Manager and Google Analytics platforms.

When integrating a tool with such platforms, it’s essential to check the presence of the GTM/GA tracking code on a website.

In DataForSEO, we designed the custom_js parameter that can help you with tasks like this.

The custom_js field allows tailoring data extraction to your specific needs. You should just add the necessary JavaScript code to the On-Page API Task POST body, and upon crawl completion, you’ll find the requested data in the custom_js_response object.

Note: when using the custom_js field, additional charges apply. For more information on the cost of tasks with the custom_js parameter, please refer to this help article.

For example, you can use the following JS snippet to check if the website contains Google Tag Manager and Google Analytics as an scr attribute:

let meta = { haveGoogleAnalytics: false, haveTagManager: false };\r\nfor (var i = 0; i < document.scripts.length; i++) {\r\n let src = document.scripts[i].getAttribute(\"src\");\r\n if (src != undefined) {\r\n if (src.indexOf(\"analytics.js\") >= 0)\r\n meta.haveGoogleAnalytics = true;\r\n\tif (src.indexOf(\"gtm.js\") >= 0)\r\n meta.haveTagManager = true;\r\n }\r\n}\r\nmeta;

Your POST request will be structured as in the example below:

[
  {
    "target": "dataforseo.com",
    "max_crawl_pages": 10,
    "custom_js": "let meta = { haveGoogleAnalytics: false, haveTagManager: false };\r\nfor (var i = 0; i < document.scripts.length; i++) {\r\n let src = document.scripts[i].getAttribute(\"src\");\r\n if (src != undefined) {\r\n if (src.indexOf(\"analytics.js\") >= 0)\r\n meta.haveGoogleAnalytics = true;\r\n\tif (src.indexOf(\"gtm.js\") >= 0)\r\n meta.haveTagManager = true;\r\n }\r\n}\r\nmeta;"
  }
]

The API server will provide the custom_js_response with the required data.

Example:

{
  "custom_js_response": {
    "haveGoogleAnalytics": false,
    "haveTagManager": false
  }
}

Using Custom JavaScript (custom_js) in On-Page API, you can extract virtually any piece of information from any web page.

Exit mobile version