justinooo icon

IG Advertising Campgaign Value Calculation Algorithm

justinooo | PRO | 05/20/23 07:28:44 PM UTC | 0 ⭐ | 1684 👁️ | Never ⏰ | [python, instagram, algorithm]
Python |

2.32 KB

|

None

|

0 👍

/

0 👎

    def generate_ig_value(self):
        """
        Generate a scalar value as a score for the influencer, based on a variety of weighted factors.
        Factors taken into account are engagement rate, average likes/comments, followers, audience ages, and audience credibility.
        """
 
        assert self.platform == "IG"
 
        values = []
        def add_value(x, y = 1):
            x = round(max(x, 1), 2)
            values.append((x, y))
 
        audiscore = 0
        audicount = 0
        audicred = 0
        for group in ["likers", "followers"]:
            audience = self.data["audience_" + group]
            adata = audience["data"] if audience["success"] else None
            if adata:
                
                audicred = adata["audience_credibility"]
                audiscore += (audicred * 100) * 15
                audicount += 1
 
                for agedata in adata["audience_ages"]:
                    w1 = agedata["weight"] * 100
                    w2 = {
                        "13-17": 2, "18-24": 4, "25-34": 7,
                        "35-44": 10, "45-64": 14, "65-": 11
                    }.get(agedata["code"], 2)
                    audiscore += w1 * w2
        
        up = self.data["user_profile"]
        add_value(up["engagement_rate"] * 100, 50)
        add_value(up["avg_likes"], 20)
        add_value(up["avg_comments"], 10)
 
        multi = 1
        mmap = {
            5: 10000000,
            4: 5000000,
            3: 2500000,
            2: 1000000
        }
 
        if audicount > 0:
            followers = up["followers"] * audicred
            for k, v in mmap.items():
                if followers >= v:
                    multi = k
                    break
            add_value(followers, 5)
            add_value(audiscore / audicount, 10)
        else:
            raise Exception("Can't generate score. TensorReport missing 'audience_likers' & 'audience_followers' but at least one is required.")
 
        score = 0
        for x, y in values:
            score += x * y
 
        weights = [v[1] for v in values]
        avg = score / sum(weights) 
        ret = round(avg / 100.0) * 100.0
        ret *= multi * 0.05
 
        if up["avg_views"] > 0:
            ret += (up["avg_views"] / 1000) * 5
 
        return int(ret)

Comments