Advertisement
justinooo

IG Advertising Campgaign Value Calculation Algorithm

May 20th, 2023
1,368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1.     def generate_ig_value(self):
  2.         """
  3.        Generate a scalar value as a score for the influencer, based on a variety of weighted factors.
  4.        Factors taken into account are engagement rate, average likes/comments, followers, audience ages, and audience credibility.
  5.        """
  6.  
  7.         assert self.platform == "IG"
  8.  
  9.         values = []
  10.         def add_value(x, y = 1):
  11.             x = round(max(x, 1), 2)
  12.             values.append((x, y))
  13.  
  14.         audiscore = 0
  15.         audicount = 0
  16.         audicred = 0
  17.         for group in ["likers", "followers"]:
  18.             audience = self.data["audience_" + group]
  19.             adata = audience["data"] if audience["success"] else None
  20.             if adata:
  21.                
  22.                 audicred = adata["audience_credibility"]
  23.                 audiscore += (audicred * 100) * 15
  24.                 audicount += 1
  25.  
  26.                 for agedata in adata["audience_ages"]:
  27.                     w1 = agedata["weight"] * 100
  28.                     w2 = {
  29.                         "13-17": 2, "18-24": 4, "25-34": 7,
  30.                         "35-44": 10, "45-64": 14, "65-": 11
  31.                     }.get(agedata["code"], 2)
  32.                     audiscore += w1 * w2
  33.        
  34.         up = self.data["user_profile"]
  35.         add_value(up["engagement_rate"] * 100, 50)
  36.         add_value(up["avg_likes"], 20)
  37.         add_value(up["avg_comments"], 10)
  38.  
  39.         multi = 1
  40.         mmap = {
  41.             5: 10000000,
  42.             4: 5000000,
  43.             3: 2500000,
  44.             2: 1000000
  45.         }
  46.  
  47.         if audicount > 0:
  48.             followers = up["followers"] * audicred
  49.             for k, v in mmap.items():
  50.                 if followers >= v:
  51.                     multi = k
  52.                     break
  53.             add_value(followers, 5)
  54.             add_value(audiscore / audicount, 10)
  55.         else:
  56.             raise Exception("Can't generate score. TensorReport missing 'audience_likers' & 'audience_followers' but at least one is required.")
  57.  
  58.         score = 0
  59.         for x, y in values:
  60.             score += x * y
  61.  
  62.         weights = [v[1] for v in values]
  63.         avg = score / sum(weights)
  64.         ret = round(avg / 100.0) * 100.0
  65.         ret *= multi * 0.05
  66.  
  67.         if up["avg_views"] > 0:
  68.             ret += (up["avg_views"] / 1000) * 5
  69.  
  70.         return int(ret)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement