Scroll to Top
💻
Free Code
Users get ready-to-use code at no cost.
📋
Easy Copy
Copy and use the code instantly.
Quick Learning
Understand concepts fast and clearly.
📝
Step-by-Step
Follow simple instructions to implement.
📅 August 16, 2024 💻 Tutorial ⭐ Beginner Friendly

Easiest Way to Integrate Moz to Google Spreadsheets Using Free Apps Script

Author Avatar

Ashish Dwivedi

Editorial Team • Tech Writer

About This Tutorial

The easiest way to integrate Moz with Google Spreadsheets is by using a free Google Apps Script. Enter your Moz Access ID and Secret Key, then use the script to call Moz’s API. Automatically fetch metrics like DA, PA, and backlinks into your sheet for quick SEO analysis and tracking.
  1. Step 1: Click on the Copy button to copy the code snippet.
  2. Step 2: Paste the copied code into your project’s script editor.

Apps Scripts Blog

Read Blog

📂 javascript
⚡ script1.js
⚡ script1.js
function fetchMozMetrics() {
  var accessId = "YOUR_MOZ_ACCESS_ID";
  var secretKey = "YOUR_MOZ_SECRET_KEY";
  var targetUrl = "https://www.google.com";

  // Construct the authentication header
  var token = Utilities.base64Encode(accessId + ":" + secretKey);
  var headers = {
    "Authorization": "Basic " + token,
    "Content-Type": "application/json"
  };

  // Construct payload
  var payload = {
    "targets": [targetUrl],
    "metrics": ["domain_authority", "page_authority", "external_links", "mozrank", "moztrust"]
  };

  var options = {
    "method": "post",
    "headers": headers,
    "payload": JSON.stringify(payload),
    "muteHttpExceptions": true
  };

  try {
    var response = UrlFetchApp.fetch("https://lsapi.seomoz.com/v2/url_metrics", options);
    var data = JSON.parse(response.getContentText());

    if (data && data.results && data.results.length > 0) {
      var metrics = data.results[0];
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

      // Write headers if first row
      if (sheet.getLastRow() === 0) {
        sheet.appendRow(Object.keys(metrics));
      }

      // Write metric values
      var row = Object.values(metrics);
      sheet.appendRow(row);

      Logger.log("Metrics saved to sheet.");
    } else {
      Logger.log("No metrics found in response: " + response.getContentText());
    }
  } catch (e) {
    Logger.log("Error fetching Moz metrics: " + e);
  }
}