@memberjunction/actions-bizapps-social
Version:
Social Media Actions for MemberJunction - Twitter, LinkedIn, Facebook, Instagram, TikTok, YouTube, HootSuite, Buffer
266 lines • 10.3 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HootSuiteGetAnalyticsAction = void 0;
const global_1 = require("@memberjunction/global");
const hootsuite_base_action_1 = require("../hootsuite-base.action");
const actions_1 = require("@memberjunction/actions");
/**
* Action to retrieve analytics data from HootSuite
*/
let HootSuiteGetAnalyticsAction = class HootSuiteGetAnalyticsAction extends hootsuite_base_action_1.HootSuiteBaseAction {
/**
* Get analytics data from HootSuite
*/
async InternalRunAction(params) {
const { Params, ContextUser } = params;
try {
// Initialize OAuth
const companyIntegrationId = this.getParamValue(Params, 'CompanyIntegrationID');
if (!await this.initializeOAuth(companyIntegrationId)) {
throw new Error('Failed to initialize OAuth connection');
}
// Extract parameters
const postId = this.getParamValue(Params, 'PostID');
const profileId = this.getParamValue(Params, 'ProfileID');
const startDate = this.getParamValue(Params, 'StartDate');
const endDate = this.getParamValue(Params, 'EndDate');
const metricsType = this.getParamValue(Params, 'MetricsType') || 'all';
const aggregateByProfile = this.getParamValue(Params, 'AggregateByProfile') || false;
// Determine what analytics to fetch
let analyticsData;
if (postId) {
// Get analytics for specific post
analyticsData = await this.getPostAnalytics(postId, startDate, endDate);
}
else if (profileId) {
// Get analytics for specific profile
analyticsData = await this.getProfileAnalytics(profileId, startDate, endDate, metricsType);
}
else {
// Get analytics for all profiles
analyticsData = await this.getAllProfilesAnalytics(startDate, endDate, metricsType, aggregateByProfile);
}
// Normalize analytics
const normalizedAnalytics = this.processAnalyticsData(analyticsData, metricsType);
// Create summary
const summary = {
period: {
start: startDate || 'Not specified',
end: endDate || 'Not specified'
},
metricsType: metricsType,
totalMetrics: this.calculateTotalMetrics(normalizedAnalytics),
topPerformingPosts: this.getTopPerformingPosts(normalizedAnalytics),
engagementRate: this.calculateEngagementRate(normalizedAnalytics)
};
// Update output parameters
const outputParams = [...Params];
const analyticsParam = outputParams.find(p => p.Name === 'Analytics');
if (analyticsParam)
analyticsParam.Value = normalizedAnalytics;
const summaryParam = outputParams.find(p => p.Name === 'Summary');
if (summaryParam)
summaryParam.Value = summary;
return {
Success: true,
ResultCode: 'SUCCESS',
Message: 'Successfully retrieved analytics data',
Params: outputParams
};
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
return {
Success: false,
ResultCode: 'ERROR',
Message: `Failed to get analytics: ${errorMessage}`,
Params
};
}
}
/**
* Get analytics for a specific post
*/
async getPostAnalytics(postId, startDate, endDate) {
const params = {};
if (startDate)
params.startTime = this.formatHootSuiteDate(startDate);
if (endDate)
params.endTime = this.formatHootSuiteDate(endDate);
const response = await this.axiosInstance.get(`/analytics/posts/${postId}`, { params });
return response.data;
}
/**
* Get analytics for a specific profile
*/
async getProfileAnalytics(profileId, startDate, endDate, metricsType) {
const params = {
socialProfileIds: profileId
};
if (startDate)
params.startTime = this.formatHootSuiteDate(startDate);
if (endDate)
params.endTime = this.formatHootSuiteDate(endDate);
if (metricsType && metricsType !== 'all')
params.metrics = this.getMetricsList(metricsType);
const response = await this.axiosInstance.get('/analytics/profiles', { params });
return response.data;
}
/**
* Get analytics for all profiles
*/
async getAllProfilesAnalytics(startDate, endDate, metricsType, aggregateByProfile) {
// First get all profiles
const profiles = await this.getSocialProfiles();
if (profiles.length === 0) {
return { data: [] };
}
const params = {
socialProfileIds: profiles.map(p => p.id).join(',')
};
if (startDate)
params.startTime = this.formatHootSuiteDate(startDate);
if (endDate)
params.endTime = this.formatHootSuiteDate(endDate);
if (metricsType && metricsType !== 'all')
params.metrics = this.getMetricsList(metricsType);
if (aggregateByProfile)
params.groupBy = 'socialProfile';
const response = await this.axiosInstance.get('/analytics/profiles', { params });
return response.data;
}
/**
* Get list of metrics based on type
*/
getMetricsList(metricsType) {
const metricsMap = {
'engagement': ['likes', 'comments', 'shares', 'engagements'],
'reach': ['impressions', 'reach'],
'clicks': ['clicks', 'linkClicks'],
'all': ['likes', 'comments', 'shares', 'clicks', 'impressions', 'engagements', 'reach']
};
return (metricsMap[metricsType] || metricsMap['all']).join(',');
}
/**
* Process and normalize analytics data
*/
processAnalyticsData(data, metricsType) {
if (!data || !data.data)
return [];
const results = [];
if (Array.isArray(data.data)) {
data.data.forEach((item) => {
results.push(this.normalizeAnalytics(item.metrics));
});
}
else if (data.metrics) {
results.push(this.normalizeAnalytics(data.metrics));
}
return results;
}
/**
* Calculate total metrics across all data
*/
calculateTotalMetrics(analytics) {
return analytics.reduce((total, current) => ({
impressions: total.impressions + current.impressions,
engagements: total.engagements + current.engagements,
clicks: total.clicks + current.clicks,
shares: total.shares + current.shares,
comments: total.comments + current.comments,
likes: total.likes + current.likes,
reach: total.reach + current.reach,
saves: (total.saves || 0) + (current.saves || 0),
videoViews: (total.videoViews || 0) + (current.videoViews || 0),
platformMetrics: {}
}), {
impressions: 0,
engagements: 0,
clicks: 0,
shares: 0,
comments: 0,
likes: 0,
reach: 0,
saves: 0,
videoViews: 0,
platformMetrics: {}
});
}
/**
* Get top performing posts based on engagement
*/
getTopPerformingPosts(analytics) {
return analytics
.sort((a, b) => b.engagements - a.engagements)
.slice(0, 5);
}
/**
* Calculate average engagement rate
*/
calculateEngagementRate(analytics) {
const total = this.calculateTotalMetrics(analytics);
if (total.impressions === 0)
return 0;
return (total.engagements / total.impressions) * 100;
}
/**
* Define the parameters this action expects
*/
get Params() {
return [
...this.commonSocialParams,
{
Name: 'PostID',
Type: 'Input',
Value: null
},
{
Name: 'StartDate',
Type: 'Input',
Value: null
},
{
Name: 'EndDate',
Type: 'Input',
Value: null
},
{
Name: 'MetricsType',
Type: 'Input',
Value: null
},
{
Name: 'AggregateByProfile',
Type: 'Input',
Value: null
},
{
Name: 'Analytics',
Type: 'Output',
Value: null
},
{
Name: 'Summary',
Type: 'Output',
Value: null
}
];
}
/**
* Get action description
*/
get Description() {
return 'Retrieves analytics data from HootSuite for posts, profiles, or overall account performance';
}
};
exports.HootSuiteGetAnalyticsAction = HootSuiteGetAnalyticsAction;
exports.HootSuiteGetAnalyticsAction = HootSuiteGetAnalyticsAction = __decorate([
(0, global_1.RegisterClass)(actions_1.BaseAction, 'HootSuiteGetAnalyticsAction')
], HootSuiteGetAnalyticsAction);
//# sourceMappingURL=get-analytics.action.js.map