You will fall behind if you don't use JavaScript in n8n (no-code tutorial)

Learn to build a LinkedIn post analyzer in n8n by using ChatGPT to write JavaScript code for you. The workflow scrapes posts, calculates engagement metrics and velocity, ranks top performers, finds the best posting times, and exports results to Google Sheets without writing code manually.
Chapters
What's up guys, in this video we're going to be going over how to use JavaScript inside of NAN without writing a single line of code yourself. If you haven't been using JavaScript in NAN because you thought you knew how to write it, then don't worry because I use it all the time. I've said this in previous videos, I use it all the time and I have no idea how to write it. By the way guys, just before we get into it, the waitlist for my new NAN community is out and I've actually just done a full breakdown inside of there of the JavaScript node. So if you want to learn more about how to use the JavaScript node as well as so many other things inside of NAN and properly master N-A-N, then sign up for the waitlist and you'll be able to get an early bird discount and you'll get notified as soon as it drops. So we're just going to jump straight into this. As you can see here in N-A-N, I just have a very, very simple setup at the moment. Basically, I've got a system connected up to an actor on Apify and it scrapes LinkedIn posts for me. So it scrapes posts in a specific niche. For this one, it's just AI, and it gives me an output that looks like this with the post URL, the text the author's name, the stats of the post, the post at that date, some hashtag, and then some more on the content, and then the image information. And then that's all we really need to worry about. So what we're going to do is we're going to write some JavaScript out here using AI, and we're going to run this through. I'm going to show you all of the possibilities of using JavaScript and NNN because there's so many different possibilities, it's impossible to cover it in one video. But I want to give you an idea of the kind of things you can do with JavaScript because unlike— it's different to other nodes. Other nodes just kind of have like a set use or it says what it does. With JavaScript, it can kind of do anything. It's not really clear what exactly you do with it. You kind of have to figure out when and where to use it. So what we're going to do is if we come over to ChatGPT, this is what I use to just generate all of my JavaScript. First thing we're going to do is give it context of what we're doing. So we'll just say to it quickly, I am working in 'Nan right now and need some JavaScript.' Cool. And then what we're going to do is we're going to attach, um, a screenshot of the information we want just to give it full context. So there's a couple things we need here. Basically what we're going to be doing is we're going to be analyzing these posts and seeing which posts are performing better. So we need the information on the post, we need the stats of the post, so If we screenshot this and then attach it, and then we're also going to need the posted at date, and that's all we're really going to need for this. Okay, cool. So we're just going to say to it, I'm going to use the text-to-speech. I'm going to say, I have a node that scrapes LinkedIn posts and gives me a bunch of different information. I've attached a screenshot of some examples of how this information is displayed. I want some JavaScript that is able to calculate the engagement per minute based on the likes, shares, and comments. It will use the published at date to calculate the velocity of the engagement. Okay, cool. So we're just going to say that to it. We're going to send this off. And then this is kind of what you got to do. You got to go back and forth. Sometimes it's going to generate JavaScript. It might not quite work and you've kind of got to work with, um, ChatGPT or whichever LLM you're using 'til it, um, gets it right. So here are some JavaScript you can drop into a function node. Okay, cool. So we're just gonna copy this, come back over, and then put down the JavaScript node. So you can just type in JavaScript or it just calls it a code node in NAN. Uh, delete this demo information and then just paste in that JavaScript that NAN gave us. And if we execute step, okay, this is what it's given us. So, so you can see it's done total reactions, comments, shares, total engagement, and then minutes since posted, engagement per minute. It's obviously done this for the first one. Yeah, because this first one, it doesn't have, um, any engagement on it at all. But the issue is here that we forgot to mention, or I forgot to mention to ChatGPT, I didn't mention that there was gonna be multiple items. So as you can see here, it only outputted 1 items, but of course it got inputted with 50 items. So we're just gonna say to it, right now it only does the first item. There will be multiple items. So I said right now it only does the first item, there will be multiple items. And I said, okay, thanks for that, because I should have told it that already. It's going to generate us some new JavaScript. So if I copy this and I can just replace what's in here and then hit execute step, now we get 50 items and it also gives us all of the other information. Oh cool, this is the information that we had before anyway, so I assume the new one is going to be at the bottom. Yeah, cool. So it gives us total reactions, comments, shares, total engagement, minutes since posted, and then engagement per minute. Again, for this first one, there's no information. So if we switch to a table view, and then we can just look at the second one, scroll right to the end here, and then— okay, cool. So total reactions, 1, comments, 0, shares, 0, total engagement is 1, minutes since posted, and then this is the engagement per minute. So pretty low for this one. If we find the next one, I'll find one here that actually has some engagement, um, so we can better test this out. Okay, so this one here, the total reactions are 2, it's got 3 comments and 0 shares, so the total engagement is 5. These are the amount of minutes since it's posted, and then this is the engagement per minute. There's quite a lot in here that have like no engagement at all. So what we're going to do is we're going to come back over and we're going to say, can you make it so it filters out all of the ones with a total engagement score of 4 or less. So can you make it so filters out all of the ones with an engagement score of 4 or less? You can see here we've got a total engagement score, and that's the one I'm talking about. Um, so it's going to generate us this new JavaScript, and then we should see a lot less in here. So if we get rid of this, paste in our new JavaScript, hit execute step. Okay, this time we only got 3 items back. Um, so there's only 3 in here that actually have a decent amount of engagement, clearly. So this one had 5, this one had 5, and this one had 5. Cool. So what we're going to do here actually is we're going to change the amount. So right now there's 50 items coming through. We're just going to change that so that more items come through so we have more data to work with. So we're going to go to 500, and then obviously if the rule of averages is correct, then we should get, um, 30 items coming through here because we did 10 times more. Okay, so for some reason it wasn't letting me do 500, but let me do 200, and you can here. Now I've run it and we've got 41 items in here. So if we scroll along, we should see some more content in here. So yeah, these ones are getting obviously more engagement. So total engagement, total reactions, and then it also gives us the minutes since posted and the engagement per minute of all of these. And then we're also going to ask it to give us the ratios between shares, likes, and comments. So we're going to say, can you also give it Get it to give us a ratio between shares, likes, and comments. Can you also give it to give us the ratio between shares, likes, and comments? Um, and you can get it, and you can go into like specifics of exactly how those ratios should look, or you can just get, um, ChatGPT to do that for you. So for this video, I'm just getting ChatGPT to do it for me. So it's given us this new one. Let's delete this and then paste this in here. And then, okay, if we scroll to the end, then we get the ratios here. So likes ratio, comments ratio, shares ratio, uh, likes, comments, shares, uh, likes, comments, shares. Perfect. Okay, cool. Okay, so what we're going to do now is we're going to come back over and we're going to add aggregate after this. So if we go aggregate and then all items into a single list, and then we get one item like that. Perfect. Okay, now we're going to do another JavaScript code node after that, and we're going to get some overall sort of calculations and identifications from this, uh, information. So we're going to rank the top 5 engagements by velocity, and then also the top 5 evergreen ones. So just the ones that are doing well overall that have been posted for a while, and the top 5 ones I've done the most recently, so the velocity is the highest. So we're going to say I have added an aggregate node after this JavaScript node that puts all of the data into a single list. Can you please give me some more JavaScript that will take all this information and then give me the top 5 posts with the highest velocity and also the top 5 evergreen posts? So the ones that have been posted for a while but they have been getting good engagement during that time. Okay, cool. We'll send that off and then we'll get our other code node ready while it's here. So if we go to code again, place that down, and just get rid of this in here. And then it should have given us the new JavaScript. Cool. Let's copy that and then paste that into here. Hit execute step. And then, okay, so now we've got an error. So it says post.map is not a function, line 5. All right, so what we're going to do, we can literally just copy that, come over to ChatGPT and say I ran it and it gave me that. And then we'll see what it comes back with. So it's going to think longer for a better answer. Probably think for a second. So I'll come back when it's done. Okay, so it said the error means the function expected post to be an array, but the aggregated output you actually fed it wasn't an array. Maybe it was an object or the array is nested under.json.data. Okay, cool. So basically it just didn't know the exact input from the aggregate. So what we're going to do is we're going to copy what it gave us and this potentially might not work. Um, but if it doesn't work, we're just going to give it the output of like how the aggregate looks. So we'll paste this out and we'll try it. And then, okay, perfect, it found. So total posts found was 41. Top velocity, so top velocity rank number 1 was this post here, and this is the author, this is all of their information. Cool. And then top, and then top velocity number 2, number 3, number 4, and number 5. And then top evergreen It's then ranked all of those for us as well. So number 1, number 2, number 3, number 4, and number 5. Okay, perfect. So now we've got 5 top velocity ones and 5 top evergreen ones. Okay, now we're gonna get it to give us the best time of the day to post and the engagement distribution. So we're gonna say to it, now can you add to this JavaScript a function that will output the best time of the day to post based on that information as well as the engagement distribution? Okay, cool. Let's send that off and then we'll see the JavaScript that it gives back to us. Uh, so it's just kind of explaining what it's going to do here, and then it's going to output— it's going to re-output what it just outputted, and it's going to add on more at the bottom of it. Okay, cool, it's done. So if we copy this, come back over— so just pasted in the new one. Let's hit Execute Step, and then again we get the velocity. We can actually just close all of that, and the evergreen, and then the best posting hour. And the best posting hour, it says 0, which is weird, so we'll come back to that. The average engagement by hour, you see 0, 1, 2, 3, 4, and then 23. So that's a little bit weird, not too sure why it's done it that way. Uh, if I come back over— oh, okay, so what it's done, it's just done the, um, hours. So 0, I guess, is like 12 o'clock, um, at night. So 0, I guess, is just 12 o'clock, and then it's done like that. But at the bottom here it said, do you want me to do the formula? Do you want me to do it in the, uh, in a readable time label like 2 PM? I'm just saying Yes, do it in a readable time label for the distribution and for the best posting hour. Okay, cool. So yes, do it in a readable time label for the distribution part and for the best posting hour. So we'll give this a sec to generate the JavaScript and then we'll take it back to 1 AM. Okay, so it's just finished generating. Let's copy this and then delete this. Paste in the new one and then execute. And then we can close these down because we know they work. And then you can see, yeah, best posting hours 12 AM, and then 12 AM, 1 AM, 2 AM, 3 AM, 4 AM, and 11 PM. Obviously it skips straight to 11 PM because there was no data for the time in between, which is all right. So then you can now do literally anything with this information. You could just put it into a Google Sheet if you wanted. You could do all 41, uh, from here into a Google Sheet, or you could just put the information from here into a Google Sheet with the top velocity post and the top evergreen post as well, as well as all of the best posting times. And that's going to be for if you run this every day, it's going to show you the best posting times from today or from yesterday or from the whole of last week, that kind of thing. So very, very cool. I hope this kind of like opens your eyes on how you can use JavaScript. And one of the best things about JavaScript as well is you could probably do this with AI. Well, you could do this with AI. In fact, if I put down an LLM step and prompted it, it would be able to do the same thing. But there's two main issues when using AI. AI is obviously a fantastic node, and there's things that AI can do that no other node in NAN will be able to do. But there's two issues. The first issue is that it's inconsistent. So it could give you one output one time, then you can give it the exact same data with the same prompt, and it could give you a different output. The next time. So it's inconsistent. JavaScript is not inconsistent at all. This is going to work the exact same time if I run it a million times. It doesn't matter. The second one is the volume. Obviously LLMs can only handle a certain amount of volume. Right now I did 200 items and 200 posts, which an LLM probably would be able to handle. But if I did 2,000, an LLM wouldn't be able to handle that. It would get confused, it would hit context window limits, I'd get rate limited by OpenAI. But with JavaScript, it makes no difference. Doesn't matter how many items go into there, it's just running through the script and it will just output the same thing regardless of how many items go in. Also, there's a third thing which I didn't mention. Well, I guess a third and a fourth, you can combine it into one thing, is that the speed and the cost of it. The cost to generate the JavaScript is completely free, doesn't cost me anything. I guess you can consider a ChatGPT subscription, but on this ChatGPT account, I don't even have a subscription. It's a free account. So cost with an LLM, there's going to be a cost involved, of course, with the API pricing. And the second one is the speed. An LLM is always going to be slower, especially if it's doing hundreds of items. It's going to be way slower. And if you need this thing to be quick, it's probably not best using LLM. But with this, as you can see, it is literally instant. If I do this, it is as instant as any of these other nodes, as you can see. So pretty much instant, which is something an LLM just can't do.
More from Rory

Dynamic Workflows are crazy. This is how to actually use them (beginner's guide)










