Difference between two HubSpot tracking code versions

I’m working on a website that already has this HubSpot tracking code installed:

<script type="text/javascript" language="javascript">
var portal_id = 789012;
var version_log = "2.00";
var tracking_domain = "client-portal.app15.hubspot.com";
document.write(unescape("%3Cscript src='" + document.location.protocol + "//" + tracking_domain + "/salog.js.aspx' type='text/javascript'%3E%3C/script%3E"));
</script>

Now I need to implement this second script that someone said is also for HubSpot analytics:

<script type="text/javascript">
    (function(doc, tag, identifier, refresh) {
        if (doc.getElementById(identifier)) {return;}
        var element = doc.createElement(tag), first = doc.getElementsByTagName(tag)[0];
        element.id = identifier;
        element.src = '//js.hubspot.com/analytics/' + (Math.ceil(new Date()/refresh) * refresh) + '/789012.js';
        first.parentNode.insertBefore(element, first);
    })(document, "script", "hubspot-analytics", 300000);
</script>

I’m confused about whether these are two different HubSpot scripts that serve different purposes, or if one is just a newer version of the other. Can someone explain what each one does and if I need both of them on the same page?

i get your confusion! the 1st script is older and uses document.write, which can slow things down. the 2nd one is newer and loads more efficiently. u really only need the 2nd script for better performance, so ditch the old one.