-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds keyword search volume data feature for tracked keywords.
- Adds a volume field in the keyword table. - Adds a button in the Adwords Integration screen to update all the tracked keywords. - When a new keyword is added, the volume data is automatically fetched. - Adds ability to sort keywords based on search volume.
- Loading branch information
Showing
16 changed files
with
341 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
database/migrations/1709217223856-add-keyword-volume-field copy.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Migration: Adds volume field to the keyword table. | ||
|
||
// CLI Migration | ||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
return queryInterface.sequelize.transaction(async (t) => { | ||
try { | ||
const keywordTableDefinition = await queryInterface.describeTable('keyword'); | ||
if (keywordTableDefinition) { | ||
if (!keywordTableDefinition.volume) { | ||
await queryInterface.addColumn('keyword', 'volume', { | ||
type: Sequelize.DataTypes.STRING, allowNull: false, defaultValue: 0, | ||
}, { transaction: t }); | ||
} | ||
} | ||
} catch (error) { | ||
console.log('error :', error); | ||
} | ||
}); | ||
}, | ||
down: (queryInterface) => { | ||
return queryInterface.sequelize.transaction(async (t) => { | ||
try { | ||
const keywordTableDefinition = await queryInterface.describeTable('keyword'); | ||
if (keywordTableDefinition) { | ||
if (keywordTableDefinition.volume) { | ||
await queryInterface.removeColumn('keyword', 'volume', { transaction: t }); | ||
} | ||
} | ||
} catch (error) { | ||
console.log('error :', error); | ||
} | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import { Op } from 'sequelize'; | ||
import db from '../../database/database'; | ||
import Keyword from '../../database/models/keyword'; | ||
import verifyUser from '../../utils/verifyUser'; | ||
import parseKeywords from '../../utils/parseKeywords'; | ||
import { getKeywordsVolume, updateKeywordsVolumeData } from '../../utils/adwords'; | ||
|
||
type KeywordsRefreshRes = { | ||
keywords?: KeywordType[] | ||
error?: string|null, | ||
} | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
await db.sync(); | ||
const authorized = verifyUser(req, res); | ||
if (authorized !== 'authorized') { | ||
return res.status(401).json({ error: authorized }); | ||
} | ||
if (req.method === 'POST') { | ||
return updatekeywordVolume(req, res); | ||
} | ||
return res.status(502).json({ error: 'Unrecognized Route.' }); | ||
} | ||
|
||
const updatekeywordVolume = async (req: NextApiRequest, res: NextApiResponse<KeywordsRefreshRes>) => { | ||
const { keywords = [], domain = '', update = true } = req.body || {}; | ||
if (keywords.length === 0 && !domain) { | ||
return res.status(400).json({ error: 'Please provide keyword Ids or a domain name.' }); | ||
} | ||
|
||
try { | ||
let keywordsToSend: KeywordType[] = []; | ||
if (keywords.length > 0) { | ||
const allKeywords:Keyword[] = await Keyword.findAll({ where: { ID: { [Op.in]: keywords } } }); | ||
keywordsToSend = parseKeywords(allKeywords.map((e) => e.get({ plain: true }))); | ||
} | ||
if (domain) { | ||
// const allDomain = domain === 'all'; | ||
// const allKeywords:Keyword[] = allDomain ? await Keyword.findAll() : await Keyword.findAll(allDomain ? {} : { where: { domain } }); | ||
// keywordsToSend = parseKeywords(allKeywords.map((e) => e.get({ plain: true }))); | ||
} | ||
|
||
if (keywordsToSend.length > 0) { | ||
const keywordsVolumeData = await getKeywordsVolume(keywordsToSend); | ||
// console.log('keywordsVolumeData :', keywordsVolumeData); | ||
if (keywordsVolumeData.error) { | ||
return res.status(400).json({ keywords: [], error: keywordsVolumeData.error }); | ||
} | ||
if (keywordsVolumeData.volumes !== false) { | ||
if (update) { | ||
const updated = await updateKeywordsVolumeData(keywordsVolumeData.volumes); | ||
if (updated) { | ||
return res.status(200).json({ keywords }); | ||
} | ||
} | ||
} else { | ||
return res.status(400).json({ error: 'Error Fetching Keywords Volume Data from Google Adwords' }); | ||
} | ||
} | ||
|
||
return res.status(400).json({ keywords: [], error: 'Error Updating Keywords Volume data' }); | ||
} catch (error) { | ||
console.log('[Error] updating keywords Volume Data: ', error); | ||
return res.status(400).json({ error: 'Error Updating Keywords Volume data' }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.