What is Google Trends?
Practical Uses of Google Trends Data in Salesforce
Google Trends is a valuable tool from Google that provides insights and comparisons of keyword search volumes directly from the source. However, Google Trends does not offer an official API. Therefore, we need to scrape the data to utilize it.
In many CRM setups, a product database is maintained to manage sales in Salesforce’s Sales Cloud. Google Trends data can be used to check if a product is trending, identify hot items, and strategize around related keywords. For example:
- Digital Products: If you sell niche software like VPNs, Google Trends can analyze data and provide related keywords and topics without needing additional input beyond the initial product entries.
- Physical Products: For products like cell phones, you can compare interest and volume between iPhone and Android. This data can then be used to create focused campaigns around specific keywords and geographical locations.
Using Google Trends data in Salesforce can help you strategize more effectively, targeting customers with precision based on demand in specific areas.
Apex Class for Google Trends Data Integration
Here is an optimized version of the Apex class for gathering Google Trends data:
public with sharing class seoBoss_API_GoogleTrends extends seoBoss_API_Base {
public static Boolean useAsync = false;
private String theURL = '';
public static String projectId;
public seoBoss_API_GoogleTrends(String theKeyword) {
super(getEndpointDetails(
getClassNameFromException(),
new List<String>(),
new List<String>{theKeyword}
));
this.theURL = theURL;
}
public override Map<String,Object> assemblePayload() {
return null;
}
public class Payload {
String[] args;
public Payload(String[] theStrings) {
this.args = theStrings;
}
}
public enum EndpointResource {
suggestions,
related_queries,
trending_keywords
}
public String getTrendsData(String theKeyword) {
return this.doWork(useAsync);
}
public static String getTrendsSuggestionData(String theKeyword) {
String theBody = buildPayload(EndpointResource.suggestions, theKeyword);
seoBoss_API_GoogleTrends co = new seoBoss_API_GoogleTrends(theBody);
return co.getTrendsData(theBody);
}
public static String getTrendsRelatedQueriesData(String theKeyword) {
String theBody = buildPayload(EndpointResource.related_queries, theKeyword);
seoBoss_API_GoogleTrends co = new seoBoss_API_GoogleTrends(theBody);
return co.getTrendsData(theBody);
}
public static String getTrendsTrendingKeywordsData(String theKeyword) {
String theBody = buildPayload(EndpointResource.trending_keywords, theKeyword);
seoBoss_API_GoogleTrends co = new seoBoss_API_GoogleTrends(theBody);
return co.getTrendsData(theBody);
}
public static List<Keyword__c> breakdownTrendsIntoKeywordSObjects(Set<String> trends) {
List<Keyword__c> insertKws = new List<Keyword__c>();
if (trends.size() > 0) {
Map<String,String> keywordResults = processResults(new List<String>(trends));
for (String s : keywordResults.keySet()) {
String thekw = s;
String theKey = s;
String theValue = keywordResults.get(s);
theValue = theValue.length() > 5 ? theValue.substringAfter('-') : theValue;
theValue = theValue.replaceAll('-', '').trim();
String theTopValue = '';
String theRisingValue = '';
if (theValue != '' && theValue != null) {
if (Integer.valueOf(theValue) <= 100) {
theTopValue = theValue;
} else {
theRisingValue = theValue;
}
}
insertKws.add(new Keyword__c(
Project__c = projectId,
Source__c = 'Google Trends',
Seed_Keyword__c = thekw,
Keyword__c = theKey,
Google_Trends_Rising_Score__c = theRisingValue,
Google_Trends_Top_Score__c = theTopValue
));
}
}
return insertKws;
}
public static String buildPayload(EndpointResource resource, String theKeyword) {
trendsObject theTrendsObject = new trendsObject(resource.name(), new List<String>{theKeyword});
return JSON.serialize(theTrendsObject);
}
public class trendsObject {
String method { get; set; }
String[] params { get; set; }
public trendsObject(String method, String[] params) {
this.method = method;
this.params = params;
}
}
public static Map<String, Keyword__c> getTrends(Set<String> keywords, String projectId) {
Set<String> theTrendsKeywords = getTrendsKeywords(keywords);
List<Keyword__c> theKeywords = getKWRecordsFromTrendsResponse(theTrendsKeywords);
Map<String, Keyword__c> keywordMap = new Map<String, Keyword__c>();
for (Keyword__c kw : theKeywords) {
keywordMap.put(kw.Keyword__c, kw);
}
return keywordMap;
}
public static Set<String> getTrendsKeywords(Set<String> keywords) {
Set<String> theResults = new Set<String>();
for (String k : keywords) {
try {
theResults.addAll(GoogleAPI_GoogleTrends.getRelatedKeywords(new List<String>{k}));
} catch (Exception ex) {
// Handle exceptions if necessary
}
}
return theResults;
}
public static List<Keyword__c> getKWRecordsFromTrendsResponse(Set<String> keywords) {
try {
return GoogleAPI_GoogleTrends.breakdownTrendsIntoKeywordSObjects(keywords);
} catch (Exception ex) {
// Handle exceptions if necessary
return new List<Keyword__c>();
}
}
public static Map<String,String> processResults(List<String> incomingResults) {
Map<String,String> theResultsMap = new Map<String,String>();
for (String s : incomingResults) {
String keyword = s.substringBefore('-').trim();
String theValue = s.substringAfter('-').trim();
theValue = theValue.length() > 5 ? s.substringAfterLast('-') : theValue;
theResultsMap.put(keyword, theValue);
}
return theResultsMap;
}
}
This optimized Apex class integrates Google Trends data into Salesforce by scraping the necessary data and processing it to create meaningful keyword objects that can be used for various CRM functionalities. The provided methods allow for fetching suggestions, related queries, and trending keywords, which can then be used to enhance your Salesforce data with valuable insights from Google Trends.
Leave a Reply