OP Custom Tag - Other SEO Details

This custom script captures additional SEO details. Under variable summary, select ObservePoint data and under SEO Data is the relevant information.

  • metaDescription: Collects the full value of the description Meta tag.
  • metaKeywords: Collects all the terms in the keywords Meta tag.
  • pageTitle: Shows the title of each URL.
  • imageAlt: Shows the caption that goes with each URL.
  • canonical URL: An HTML element for each URL that prevents duplicate content problems.
  • robots meta tag: Shows which pages have a robot text file. Robot text files instruct search engines on which pages they should crawl.
  • h1 - h6: Shows the presence of header tags on each page.

To capture these details in an Audit or Journey, paste the snippet below into an Execute Action:

function opReqGetAsync(paramObject, acct, callback) {
    var baseURL = "https://opreq.observepoint.com/?acct=" + acct;
    var opReq = new XMLHttpRequest();
    opReq.onreadystatechange = function() {
        if (opReq.readyState == 4 && opReq.status == 200) {
            callback(opReq.responseText);
        }
    }
    opReq.open("POST", baseURL, true);
    opReq.send(JSON.stringify(paramObject));
}
var metaDescription = 'No meta description found',
    metaKeywords = 'No meta keywords found',
    pageTitle = 'No page title found',
    pageURL = window.location.href,
    imageAlt = '0 alt tags found',
    canonicalURL = 'No canonical URL found',
    robots = 'No robots.txt found',
    h1 = '0 h1 tags found',
    h2 = '0 h2 tags found',
    h3 = '0 h3 tags found',
    h4 = '0 h4 tags found',
    h5 = '0 h5 tags found',
    h6 = '0 h6 tags found';
/* meta description */
var metaDescLower = document.querySelector('meta[name="description"'),
    metaDescCap = document.querySelector('meta[name="DESCRIPTION"');

if (metaDescLower != null && metaDescLower.getAttribute('content') !== '') {
    var metaDesc = metaDescLower.getAttribute('content');
    var chars = metaDesc.length;
    metaDescription = metaDesc + ' [' + chars + ' characters]';
} else if (metaDescCap.length != null && metaDescCap.getAttribute('content') !== '') {
    var metaDesc = metaDescCap.getAttribute('content');
    var chars = metaDesc.length;
    metaDescription = metaDesc + ' [' + chars + ' characters]';
}

/* meta keywords */
var metaKeyLower = document.querySelector('meta[name="keywords"'),
    metaKeyCap = document.querySelector('meta[name="KEYWORDS"');

if (metaKeyLower != null && metaKeyLower.getAttribute('content') !== '') {
    metaKeywords = metaKeyLower.getAttribute('content');
} else if (metaKeyCap != null && metaKeyCap.getAttribute('content') !== '') {
    metaKeywords = metaKeyCap.getAttribute('content');
}

/* page title */
if (document.title) {
    pageTitle = document.title;
}

/* all image alt attributes */
var allImages = document.querySelectorAll('img');
if (allImages.length > 0) {
    imageAlt = [];
    for (var x = 0; x < allImages.length; x++) {
        alt = allImages[x].getAttribute('alt');
        if (alt) {
            imageAlt.push(alt);
        }
    }
    imageAlt = (imageAlt.length > 0) ? imageAlt.join(', ') : '0 alt tags found';
}

/* canonical URL */
if (document.querySelectorAll('[rel="canonical"]').length > 0) {
    canonicalURL = document.querySelectorAll('[rel="canonical"]')[0].getAttribute('href');
}

/* robots.txt */
if (document.querySelectorAll('[name*="robots"]').length > 0) {
    robots = 'true';
}

/* all H1s */
var allH1s = document.querySelectorAll('h1');
if (allH1s.length > 0) {
    h1 = [];
    for (var x = 0; x < allH1s.length; x++) {
        h1.push(allH1s[x].textContent);
    }
    h1 = h1.join(', ');
} /* all H2s */
var allH2s = document.querySelectorAll('h2');
if (allH2s.length > 0) {
    h2 = [];
    for (var x = 0; x < allH2s.length; x++) {
        h2.push(allH2s[x].textContent);
    }
    h2 = h2.join(', ');
} /* all H3s */
var allH3s = document.querySelectorAll('h3');
if (allH3s.length > 0) {
    h3 = [];
    for (var x = 0; x < allH3s.length; x++) {
        h3.push(allH3s[x].textContent);
    }
    h3 = h3.join(', ');
} /* all H4s */
var allH4s = document.querySelectorAll('h4');
if (allH4s.length > 0) {
    h4 = [];
    for (var x = 0; x < allH4s.length; x++) {
        h4.push(allH4s[x].textContent);
    }
    h4 = h4.join(', ');
} /* all H5s */
var allH5s = document.querySelectorAll('h5');
if (allH5s.length > 0) {
    h5 = [];
    for (var x = 0; x < allH5s.length; x++) {
        h5.push(allH5s[x].textContent);
    }
    h5 = h5.join(', ');
} /* all H6s */
var allH6s = document.querySelectorAll('h6');
if (allH6s.length > 0) {
    h6 = [];
    for (var x = 0; x < allH6s.length; x++) {
        h6.push(allH6s[x].textContent);
    }
    h6 = h6.join(', ');
}
var seoDataObj = {
    "Meta_Description": metaDescription,
    "Page_Title": pageTitle,
    "Page_URL": pageURL,
    "Image_Alt": imageAlt,
    "Canonical_URL": canonicalURL,
    "Robots.txt": robots,
    "h1": h1,
    "h2": h2,
    "h3": h3,
    "h4": h4,
    "h5": h5,
    "h6": h6
};
opReqGetAsync(seoDataObj, "SEO Data");
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us