Exemplo n.º 1
0
def createMonthsForOneCountryForAllCampaignsDataset():
    country_name = request.json["countryName"]
    vol_token = get_vol_access_token(vol_access_id, vol_access_key)
    vol_dates = create_vol_date_range(180, mgid_timezone)
    vol_start_date = vol_dates[0]
    vol_end_date = vol_dates[1]
    return create_months_for_one_country_for_all_campaigns_dataset(vol_token,
            vol_start_date, vol_end_date, country_name)
Exemplo n.º 2
0
def createDaysForOneOfferForAllCampaignsDataset():
    offer_name = request.json["offerName"]
    vol_token = get_vol_access_token(vol_access_id, vol_access_key)
    vol_dates = create_vol_date_range(180, mgid_timezone)
    vol_start_date = vol_dates[0]
    vol_end_date = vol_dates[1]
    return create_days_for_one_offer_for_all_campaigns_dataset(vol_token,
            vol_start_date, vol_end_date, offer_name) 
Exemplo n.º 3
0
def createMonthsForOneAdForAllCampaignsDataset():
    ad_image = request.json["adImage"]
    vol_token = get_vol_access_token(vol_access_id, vol_access_key)
    vol_dates = create_vol_date_range(180, mgid_timezone)
    vol_start_date = vol_dates[0]
    vol_end_date = vol_dates[1]
    return create_months_for_one_ad_for_all_campaigns_dataset(vol_token,
            vol_start_date, vol_end_date, ad_image)
Exemplo n.º 4
0
def createMonthsForOneCWidgetForAllCampaignsDataset():
    c_widget_id = request.json["cWidgetID"]
    vol_token = get_vol_access_token(vol_access_id, vol_access_key)
    vol_dates = create_vol_date_range(180, mgid_timezone)
    vol_start_date = vol_dates[0]
    vol_end_date = vol_dates[1]
    return create_months_for_one_c_widget_for_all_campaigns_dataset(vol_token,
            vol_start_date, vol_end_date, c_widget_id)
Exemplo n.º 5
0
def createMonthsForOnePWidgetForOneCampaignDataset():
    p_widget_id = request.json["pWidgetID"]
    vol_id = request.json["volID"]
    vol_token = get_vol_access_token(vol_access_id, vol_access_key)
    vol_dates = create_vol_date_range(180, mgid_timezone)
    vol_start_date = vol_dates[0]
    vol_end_date = vol_dates[1]
    return create_months_for_one_p_widget_for_one_campaign_dataset(vol_token,
            vol_start_date, vol_end_date, p_widget_id, vol_id)
Exemplo n.º 6
0
def createDaysForOneAdForOneCampaignDataset():
    print("here")
    ad_image = request.json["adImage"]
    vol_id = request.json["volID"]
    vol_token = get_vol_access_token(vol_access_id, vol_access_key)
    vol_dates = create_vol_date_range(180, mgid_timezone)
    vol_start_date = vol_dates[0]
    vol_end_date = vol_dates[1]
    return create_days_for_one_ad_for_one_campaign_dataset(vol_token,
            vol_start_date, vol_end_date, ad_image, vol_id)
Exemplo n.º 7
0
def create_p_and_c_widgets_for_one_campaign_dataset(mgid_token, vol_token,
        campaign, days_ago, output_name):
    try:

        # create mgid and vol dates
        vol_dates = create_vol_date_range(days_ago, mgid_timezone)
        vol_start_date = vol_dates[0]
        vol_end_date = vol_dates[1]
        mgid_dates = create_mgid_date_range(days_ago, mgid_timezone)
        mgid_start_date = mgid_dates[0]
        mgid_end_date = mgid_dates[1]

        # extract needed campaign info from mgid and vol
        name = campaign["name"]
        mgid_id = campaign["mgid_id"]
        vol_id = campaign["vol_id"]
        mpl = campaign["max_lead_cpa"] 
        mps = campaign["max_sale_cpa"] 
        list_to_get_c_bid = campaign["name"].split('_')
        c_bid = float(list_to_get_c_bid[len(list_to_get_c_bid) - 1])

        # create a metadata dictionary
        metadata = {"mgid_start_date": mgid_start_date,
                "mgid_end_date": mgid_end_date,
                "vol_start_date": vol_start_date,
                "vol_end_date": vol_end_date,
                "name": name,
                "mgid_id": mgid_id,
                "vol_id": vol_id,
                "mpl": mpl,
                "mps": mps, 
                "c_bid": c_bid 
                 }

        # get clicks and costs for each widget from mgid
        mgid_widget_data = get_mgid_widget_clicks_and_costs_by_campaign(mgid_token,
                mgid_id, mgid_start_date,
                mgid_end_date)

        # get conversion data for each widget from vol
        vol_results = get_vol_widget_conversions_by_campaign(vol_token,
                vol_id, vol_start_date,
                vol_end_date)
        
        # merge the data from mgid and voluum into one dictionary
        for widget_id in mgid_widget_data:

            if widget_id not in vol_results:
                mgid_widget_data[widget_id]['revenue'] = 0.0 
                mgid_widget_data[widget_id]['leads'] = 0 
                mgid_widget_data[widget_id]['sales'] = 0 
                mgid_widget_data[widget_id]['referrer'] = [] 
            else:
                vol_widget = vol_results[widget_id]
                for key in vol_widget:
                    mgid_widget_data[widget_id][key] = vol_widget[key]

        complete_widget_data = mgid_widget_data

        complete_data_ready_for_json = {"metadata": metadata,
                "data": complete_widget_data}

        with open(f"{os.environ.get('ULANMEDIAAPP')}/data/p_and_c_widgets_for_one_campaign/{output_name}.json", "w") as file:
            json.dump(complete_data_ready_for_json, file)

        print(f"{output_name} created")
    except Exception as e:
        print("Failed - email sent")
        send_email("*****@*****.**", "Failed - create_p_and_c_widgets_for_one_campaign_dataset()", e)
        sys.exit()
from config.config import *
from config.mgid_token import mgid_token
from functions.data_acquisition_functions.get_vol_access_token import get_vol_access_token
from functions.data_acquisition_functions.get_mgid_access_token import get_mgid_access_token
from functions.data_acquisition_functions.combine_mgid_vol_ads_data import combine_mgid_vol_ads_data
from functions.data_acquisition_functions.get_mgid_ads_data import get_mgid_ads_data
from functions.data_acquisition_functions.get_vol_ads_data import get_vol_ads_data
from functions.misc.create_vol_date_range import create_vol_date_range
import sys

vol_token = get_vol_access_token(vol_access_id, vol_access_key)

date_range = "oneeighty"
vol_dates = create_vol_date_range(180, mgid_timezone)
vol_start_date = vol_dates[0]
vol_end_date = vol_dates[1]

vol_data = get_vol_ads_data(vol_token, vol_start_date, vol_end_date,
                            mgid_timezone)

mgid_data = get_mgid_ads_data(mgid_token, mgid_client_id)

combine_mgid_vol_ads_data(mgid_token, vol_token, date_range, vol_start_date,
                          vol_end_date, mgid_data, vol_data)

print(f"{date_range} ads dataset created")
def create_campaigns_for_all_campaigns_dataset(vol_token, mgid_token, days_ago,
                                               output_name):
    try:
        vol_dates = create_vol_date_range(days_ago, mgid_timezone)
        vol_start_date = vol_dates[0]
        vol_end_date = vol_dates[1]
        mgid_dates = create_mgid_date_range(days_ago, mgid_timezone)
        mgid_start_date = mgid_dates[0]
        mgid_end_date = mgid_dates[1]

        # get leads, sales, and revenue by campaign from voluum
        vol_campaign_data = get_all_campaign_conversions_by_traffic_source(
            vol_token, mgidVolTrafficSourceId, vol_start_date, vol_end_date)
        # get clicks, imps, and cost by campaign from mgid
        mgid_campaign_data = get_mgid_campaign_costs(
            mgid_token, mgid_client_id, mgid_start_date,
            mgid_end_date)["campaigns-stat"]

        # get campaign_sets
        campaign_sets = get_campaign_sets()

        # create an dictionary to hold data and metadata
        # the metadata is for request dates
        # the data is an array of dicts where each dict is a campaign
        # the stats in each campaign come from both mgid and voluum
        # the campaign data is only collected for campains in campaign_sets.txt

        campaigns_data = {
            "metadata": {
                "mgid_start_date": mgid_start_date,
                "mgid_end_date": mgid_end_date,
                "vol_start_date": vol_start_date,
                "vol_end_date": vol_end_date,
            },
            "data": []
        }

        pattern = re.compile(r'.*cpc_(.*)')
        for row in campaign_sets:
            # extract the data out of a single campaign
            mgid_campaign_id = row["mgid_id"]
            vol_campaign_id = row["vol_id"]
            campaign_name = row["name"]
            pattern = re.compile(r'\d+.\d+')
            res = pattern.findall(campaign_name)
            bid = float(list(res)[0])
            max_lead_cpa = row["max_lead_cpa"]
            max_sale_cpa = row["max_sale_cpa"]
            res = pattern.findall(campaign_name)
            max_cpc = list(res)[0]

            # create an empty dict to hold the final data for a single campaign
            campaign_data = {}

            # fill in the single campaign data
            # some data comes from campaigns_sets.txt
            # some data comes from mgid
            # some data comes from vol
            campaign_data["mgid_id"] = mgid_campaign_id
            campaign_data["vol_id"] = vol_campaign_id
            campaign_data["name"] = campaign_name
            campaign_data["bid"] = bid
            campaign_data["max_lead_cpa"] = max_lead_cpa
            campaign_data["max_sale_cpa"] = max_sale_cpa
            campaign_data["max_cpc"] = max_cpc
            campaign_data["clicks"] = mgid_campaign_data[mgid_campaign_id][
                "clicks"]
            campaign_data["imps"] = mgid_campaign_data[mgid_campaign_id][
                "imps"]
            campaign_data["cost"] = mgid_campaign_data[mgid_campaign_id][
                "spent"]
            if vol_campaign_id in vol_campaign_data:
                campaign_data["leads"] = vol_campaign_data[vol_campaign_id][
                    "leads"]
                campaign_data["sales"] = vol_campaign_data[vol_campaign_id][
                    "sales"]
                campaign_data["revenue"] = vol_campaign_data[vol_campaign_id][
                    "revenue"]
            else:
                campaign_data["leads"] = 0
                campaign_data["sales"] = 0
                campaign_data["revenue"] = 0
            campaigns_data["data"].append(campaign_data)

        for campaign in campaigns_data["data"]:
            campaign["classification"] = classify_campaign_for_all_campaigns(
                campaign)

        with open(
                f"{os.environ.get('ULANMEDIAAPP')}/data/campaigns_for_all_campaigns/{output_name}.json",
                "w") as file:
            json.dump(campaigns_data, file)

        print(f"{output_name} created")
    except Exception as e:
        print("Failed - email sent")
        send_email("*****@*****.**",
                   "Failed - create_campaigns_for_all_campaigns_dataset()", e)
        sys.exit()
def add_yesterday_in_days_for_one_campaign_dataset(vol_token, mgid_token):
    try:

        # create campaigns lookup dictionary
        campaigns_sets = get_campaign_sets()
        campaigns_lookup = {}
        for campaign in campaigns_sets:
            campaigns_lookup[campaign["vol_id"]] = campaign["mgid_id"]

        with open(f'{os.environ.get("ULANMEDIAAPP")}/data/days_for_one_campaign/days_for_one_campaign_dataset.json', 'r') as file:
             days_for_one_campaign_dataset = json.load(file)
             

        # create vol dates
        # to get yesterday's daily stats, vol_start_date is yesterday and 
        # vol_end_date is today
        vol_date_range = create_vol_date_range(1, mgid_timezone)
        vol_start_date = vol_date_range[0]
        vol_end_date = vol_date_range[1]

        # create mgid dates 
        # to get yesterday's daily stats, mgid_start_date is yesterday and 
        # mgid_end_date is yesterday 
        mgid_date_range = create_mgid_date_range(1, mgid_timezone)
        mgid_start_date = mgid_date_range[0]
        mgid_end_date = mgid_date_range[1]

        # set up the dictionary to store daily stats by campaign
        # keys are vol_id, values are yesterday's daily stats for that campaign
        daily_stats= {}
        for campaign in campaigns_sets:
            daily_stats[campaign["vol_id"]] = []

        # get vol and mgid data
        vol_response = get_vol_daily_stats_data(vol_token, vol_start_date,
                vol_end_date, mgid_timezone)
        mgid_response = get_mgid_daily_stats_data(mgid_token, mgid_start_date,
                mgid_end_date)
        
        # fill in the daily_stats dictionary
        # each key is a vol campaign id
        # each value is a dictionary of data, some from mgid, some from vol
        # data from mgid
             # clicks
             # cost
        # data from vol
            # conversions
            # revenue
            # name
            # day

        # Some of this code is complicated and going to be very difficult to
        # understand. The reason for all the loops and conditionals is that if
        # a campaign does not have any data for a particular day, voluum
        # returns nothing rather than returning a response that says clicks=0,
        # etc. That is why I had to create lists of campaigns with data and
        # campaigns without data. 
        campaigns_with_day_data = []
        campaigns_without_day_data = []
        day = ""
        for campaign in vol_response:
            vol_id = campaign["campaignId"]
            day = campaign["day"]
            campaigns_with_day_data.append(vol_id)
            # this conditional will exclude vol responses from campaigns that
            # are no longer in campaign_sets.txt
            if vol_id in daily_stats:
                mgid_id = str(campaigns_lookup[vol_id])
                daily_stats[vol_id].append({"vol_id": vol_id,
                        "conversions": campaign["conversions"],
                        "revenue": campaign["revenue"],
                        "name": re.sub(r"^.* - ", "",campaign["campaignName"], count=1),
                        "day": campaign["day"],
                        "clicks": mgid_response[mgid_id]["clicks"],
                        "cost": mgid_response[mgid_id]["spent"],
                            })
        for campaign in daily_stats.keys():
            if campaign not in campaigns_with_day_data:
                campaigns_without_day_data.append(campaign)
        for campaign in campaigns_without_day_data:
            daily_stats[campaign].append({"vol_id": campaign,
                   "conversions": 0,
                   "revenue": 0,
                   "name": "",
                   "day": day,
                   "clicks": 0,
                   "cost": 0,
                       })

        for campaign in days_for_one_campaign_dataset:
            # add daily stats for yesterday
            days_for_one_campaign_dataset[campaign].insert(0,
                    daily_stats[campaign][0] )
            # remove daily stats for 50 days ago
            days_for_one_campaign_dataset[campaign].pop()

        # create a json file
        with open(f"../../data/days_for_one_campaign/days_for_one_campaign_dataset.json", "w") as file:
              json.dump(days_for_one_campaign_dataset, file)
        print(f"yesterday's daily stats added to days_for_one_campaign_dataset.json" )
    except:
        print("Failed - email sent")
        send_email("*****@*****.**", "Failed - add_yesterday_in_days_for_one_campaign_dataset()", "Failed - add_yesterday_in_days_for_one_campaign_dataset()")
        sys.exit()
from config.config import *
from functions.data_acquisition_functions.create_conversions_for_each_campaign_dataset import create_conversions_for_each_campaign_dataset
from functions.data_acquisition_functions.get_vol_access_token import get_vol_access_token
from functions.misc.create_vol_date_range import create_vol_date_range

vol_token = get_vol_access_token(vol_access_id, vol_access_key)

#############################################
# create a data set for yesterday 
date_range = "yesterday"
vol_dates = create_vol_date_range(1, mgid_timezone)
vol_start_date = vol_dates[0]
vol_end_date = vol_dates[1]

create_conversions_for_each_campaign_dataset(vol_token, vol_start_date, vol_end_date, date_range)

print(f"{date_range} conversions for each campaign dataset created")

#############################################
# create a data set for the last seven days
date_range = "seven"
vol_dates = create_vol_date_range(7, mgid_timezone)
vol_start_date = vol_dates[0]
vol_end_date = vol_dates[1]

create_conversions_for_each_campaign_dataset(vol_token, vol_start_date, vol_end_date, date_range)

print(f"{date_range} conversions for each campaign dataset created")

#############################################
# create a data set for the last thirty days