/
upload_conversion_adjustment.py
187 lines (172 loc) · 6.08 KB
/
upload_conversion_adjustment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This example imports conversion adjustments for existing conversions.
To set up a conversion action, run the add_conversion_action.py example.
"""
import argparse
import sys
from google.ads.google_ads.client import GoogleAdsClient
from google.ads.google_ads.errors import GoogleAdsException
# [START upload_conversion_adjustment]
def main(
client,
customer_id,
conversion_action_id,
gclid,
adjustment_type,
conversion_date_time,
adjustment_date_time,
restatement_value,
):
# Determine the adjustment type.
conversion_adjustment_type_enum = client.get_type(
"ConversionAdjustmentTypeEnum"
)
if adjustment_type.lower() == "retraction":
conversion_adjustment_type = conversion_adjustment_type_enum.RETRACTION
elif adjustment_type.lower() == "restatement":
conversion_adjustment_type = conversion_adjustment_type_enum.RESTATEMENT
else:
raise ValueError("Invalid adjustment type specified.")
# Associates conversion adjustments with the existing conversion action.
# The GCLID should have been uploaded before with a conversion
conversion_adjustment = client.get_type(
"ConversionAdjustment", version="v6"
)
conversion_action_service = client.get_service(
"ConversionActionService", version="v6"
)
conversion_adjustment.conversion_action = conversion_action_service.conversion_action_path(
customer_id, conversion_action_id
)
conversion_adjustment.adjustment_type = conversion_adjustment_type
conversion_adjustment.adjustment_date_time = adjustment_date_time
# Set the Gclid Date
conversion_adjustment.gclid_date_time_pair.gclid = gclid
conversion_adjustment.gclid_date_time_pair.conversion_date_time = (
conversion_date_time
)
# Sets adjusted value for adjustment type RESTATEMENT.
if (
restatement_value
and conversion_adjustment_type
== conversion_adjustment_type_enum.RESTATEMENT
):
conversion_adjustment.restatement_value.adjusted_value = float(
restatement_value
)
conversion_adjustment_upload_service = client.get_service(
"ConversionAdjustmentUploadService", version="v6"
)
try:
response = conversion_adjustment_upload_service.upload_conversion_adjustments(
customer_id, [conversion_adjustment], partial_failure=True
)
conversion_adjustment_result = response.results[0]
print(
f"Uploaded conversion that occurred at "
f'"{conversion_adjustment_result.adjustment_date_time}" '
f"from Gclid "
f'"{conversion_adjustment_result.gclid_date_time_pair.gclid}" '
f'to "{conversion_adjustment_result.conversion_action}"'
)
except GoogleAdsException as ex:
print(
f'Request with ID "{ex.request_id}" failed with status '
f'"{ex.error.code().name}" and includes the following errors:'
)
for error in ex.failure.errors:
print(f'\tError with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print(f"\t\tOn field: {field_path_element.field_name}")
sys.exit(1)
# [END upload_conversion_adjustment]
if __name__ == "__main__":
# GoogleAdsClient will read the google-ads.yaml configuration file in the
# home directory if none is specified.
google_ads_client = GoogleAdsClient.load_from_storage()
parser = argparse.ArgumentParser(
description="Uploads a conversion adjustment."
)
# The following argument(s) should be provided to run the example.
parser.add_argument(
"-c",
"--customer_id",
type=str,
required=True,
help="The Google Ads customer ID.",
)
parser.add_argument(
"-a",
"--conversion_action_id",
type=str,
required=True,
help="The conversion action ID to be " "uploaded to.",
)
parser.add_argument(
"-g",
"--gclid",
type=str,
required=True,
help="The Google Click Identifier ID.",
)
parser.add_argument(
"-d",
"--adjustment_type",
type=str,
required=True,
help="The Adjustment type, e.g. " "RETRACTION, RESTATEMENT",
)
parser.add_argument(
"-t",
"--conversion_date_time",
type=str,
required=True,
help="The the date and time of the "
"conversion. The format is "
'"yyyy-mm-dd hh:mm:ss+|-hh:mm", e.g. '
"“2019-01-01 12:32:45-08:00”",
)
parser.add_argument(
"-v",
"--adjustment_date_time",
type=str,
required=True,
help="The the date and time of the "
"adjustment. The format is "
'"yyyy-mm-dd hh:mm:ss+|-hh:mm", e.g. '
"“2019-01-01 12:32:45-08:00”",
)
# Optional: Specify an adjusted value for adjustment type RESTATEMENT.
# This value will be ignored if you specify RETRACTION as adjustment type.
parser.add_argument(
"-r",
"--restatement_value",
type=str,
required=False,
help="The adjusted value for " "adjustment type RESTATEMENT.",
)
args = parser.parse_args()
main(
google_ads_client,
args.customer_id,
args.conversion_action_id,
args.gclid,
args.adjustment_type,
args.conversion_date_time,
args.adjustment_date_time,
args.restatement_value,
)