googleads / googleads-dfa-reporting-samples

Samples for the DoubleClick for Advertisers Reporting and Trafficking API
Apache License 2.0
106 stars 173 forks source link

existing pricing period Data Truncation when adding new pricing periods to the existing placement #114

Open datadogcoder opened 4 days ago

datadogcoder commented 4 days ago

Hi Team

I am facing a existing pricing period data truncation issue when inserting a new pricing period row using DCM API.

NOTE: The github code reference link does not have placement.Patch method usage. This pricing insert is working fine when using google API explorer. What is the problem with my code? def create_pricing_period(pricing): try: return { 'startDate': pricing['pricingperiodstartdate'], 'endDate': pricing['pricingperiodenddate'], 'units': pricing['units'], 'rateOrCostNanos': int(float(pricing['rateorcostnanos']) * 1e9), 'pricingComment': pricing['pricingcomment'] } except ValueError as e: print(f"Error converting rateOrCostNanos: {e}") return None

def patch_pricing_period_to_placement(campaign_id, placement_id, site_id, new_pricing_period):
    try:
        placement = dfa_service.placements().get(profileId=profile_id, id=placement_id).execute()
        print("current placement data",placement)

        pricing_schedule = placement.get('pricingSchedule', {})
        pricing_periods = pricing_schedule.get('pricingPeriods', [])
        print("Old pricing period", pricing_periods)
        # Directly append new pricing period without validation
        #pricing_periods.append(new_pricing_period)
        print("New pricing period", new_pricing_period)

     ```

Update body aligned with provided structure

        update_body = {
            'pricingSchedule': {
                'pricingPeriods': new_pricing_period
            }
        }

            placement.update(update_body)
            updated_placement = dfa_service.placements().patch(
                profileId=profile_id,
                id=placement_id,
                body=update_body
            ).execute()
            # updated_placement = dfa_service.placements().update(
            #     profileId=profile_id,
            #     body=placement
            # ).execute()
            # get an updated copy of data - test this on monday ???
            # placement.update(updated_placement)
            print(f"New pricing period added successfully: {updated_placement}")

        except Exception as e:
            print(f"An error occurred while patching pricing period: {e}")

    print(pricing_schedule_df.columns)
    for _, pricing in pricing_schedule_df.iterrows():
        print(pricing)
        campaign_id = pricing['campaignid']
        placement_id = pricing['placementid']
        site_id = pricing['siteid']
        print(f"Processing CampaignID: {campaign_id}, PlacementID: {placement_id}, SiteID: {site_id}")

        matching_campaign = find_matching_campaign_placement(campaign_id, placement_id, site_id)

        if matching_campaign is not None:
            new_pricing_period = create_pricing_period(pricing)

            if new_pricing_period:
                patch_pricing_period_to_placement(campaign_id, placement_id, site_id, new_pricing_period)
                # placement = dfa_service.placements().get(profileId=profile_id, id=placement_id).execute()

        else:
            print(f"No matching campaign found for CampaignID: {campaign_id}, PlacementID: {placement_id}, SiteID: {site_id}")
datadogcoder commented 3 days ago

any updates?