Skip to content

Instantly share code, notes, and snippets.

@sfboss
Created November 18, 2022 17:36
Show Gist options
  • Select an option

  • Save sfboss/bfc4b2a19d4b4ccec96826b8f6cba578 to your computer and use it in GitHub Desktop.

Select an option

Save sfboss/bfc4b2a19d4b4ccec96826b8f6cba578 to your computer and use it in GitHub Desktop.
LWC that pulls google page speed stats
import { LightningElement, api, track } from 'lwc';
import parseMetaDetails from '@salesforce/apex/seoBoss_API_HTTPUtils.parseMetaDetails';
//c/avonniBlockquoteimport doGoogleSearchConsoleCallout from '@salesforce/apex/GoogleAPIService.doGoogleSearchConsoleCallout';
//import doLegacyGoogleSearchConsoleCallout from '@salesforce/apex/GoogleAPI_SearchConsole.getSiteData';
import sendPayloadToApex from '@salesforce/apex/GoogleAPIService.sendPayloadToApex';
export default class SeoBoss_siteInfo extends LightningElement {
@api theMetaDetails = '';
@api theTitle = '';
@api theDescription = '';
@api metadatas = [];
@api imgsrcsitess = '';
@api mobileFriendly = '';
@api searchConsoleResp = '';
@api isLoading;
@api thePData;
@api projectId;
connectedCallback() {
this.handleQueryMetaData();
this.getSearchConsole();
}
handleInputTopic(e) {
}
handleToggleSection() {
}
getSearchConsole() {
// doLegacyGoogleSearchConsoleCallout().then((resp) => {
/// this.searchConsoleResp = JSON.stringify(resp);
// });
}
handleQuerySearchConsole() {
/*
doGoogleSearchConsoleCallout({ 'theURL': 'https://sfdcboss.com' }).then((resp) => {
let theDetails = JSON.parse(resp);
this.imgsrcsitess = 'data:image/png;base64,' + theDetails.screenshot.data;
this.mobileFriendly = theDetails.mobileFriendliness;
});
*/
}
handleQueryMetaData() {
parseMetaDetails({ 'theSiteURL': 'https://sfdcboss.com' }).then((theResult) => {
let theDetails = JSON.parse(theResult);
let metadata = [];
if (theDetails.hasOwnProperty('title')) {
this.theTitle = theDetails.title;
}
if (theDetails.hasOwnProperty('meta_names')) {
this.theDescription = theDetails.meta_names.description;
let metanames = Object.keys(JSON.parse(JSON.stringify(theDetails.meta_names)));
let metanamesObj = JSON.parse(JSON.stringify(theDetails.meta_names));
metanames.forEach((element) => {
let thekey = element.replace('twitter:', 'twitter_');
let theObj = {};
theObj.prop = element;
theObj.value = metanamesObj[element];
metadata.push(theObj);
});
}
if (theDetails.hasOwnProperty('properties')) {
let theprops = Object.keys(JSON.parse(JSON.stringify(theDetails.properties)));
let thepropsObj = JSON.parse(JSON.stringify(theDetails.properties));
theprops.forEach(element => {
let theObj = {};
theObj.prop = element;
theObj.value = thepropsObj[element];
metadata.push(theObj);
});
}
this.metadatas = metadata;
this.theMetaDetails = theResult;
});
}
getPageSpeedResults() {
this.isLoading = true;
var xhr = new XMLHttpRequest();
let theURL = this.theURL;
let theKey = this.theKey;
let theData = ''
xhr.responseType = 'text';
xhr.addEventListener("load", () => {
sendPayloadToApex({ 'theData': xhr.responseText }).then((resp) => {
theData = resp;
this.isLoading = false;
this.thePData = theData;
});
} );
xhr.open('GET', 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https%3A%2F%2Fsfdcboss.com&key=AIzaSyDIWrdBSYfmDY6Q8qi6QwN5BmUMOOO6b6I');
xhr.send(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment