Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
Cm offline api additional fields (#41)
Browse files Browse the repository at this point in the history
* Enhancements to allow uploading of value, quantity, and CustomVariables fields.

* Adding additional CM fields to README.
  • Loading branch information
blevitan516 authored Aug 16, 2021
1 parent 07cb834 commit 202474a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Sample integration code for onboarding offline/CRM data from BigQuery as custom
- Measurement Protocol [[details]](https://developers.google.com/analytics/devguides/collection/protocol/v1#:~:text=Measurement%20Protocol%20Overview%20bookmark_border&text=The%20Google%20Analytics%20Measurement%20Protocol,directly%20to%20Google%20Analytics%20servers.)

- **Campaign Manager**
- Offline Conversions API **(user id, device id, match id, gclid, dclid)** [[details]](https://developers.google.com/doubleclick-advertisers/guides/conversions_upload)
- Offline Conversions API **(user id, device id, match id, gclid, dclid, value, quantity, and customVariables)** [[details]](https://developers.google.com/doubleclick-advertisers/guides/conversions_upload)

- **Google Analytics 4**
- Measurement protocol (Web + App) [[details]](https://developers.google.com/analytics/devguides/collection/protocol/ga4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# 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.

import logging
import math
import time
Expand Down Expand Up @@ -107,14 +106,29 @@ def _do_upload_data(
elif 'matchId' in conversion and conversion['matchId']:
to_upload['matchId'] = conversion['matchId']

if 'value' in conversion:
to_upload['value'] = conversion['value']
if 'quantity' in conversion:
to_upload['quantity'] = conversion['quantity']
if 'customVariables' in conversion:
custom_variables = []
for r in conversion['customVariables']:
cv = {
'type': r['type'],
'value': r['value'],
'kind': 'dfareporting#customFloodlightVariable',
}
custom_variables.append(cv)
to_upload['customVariables'] = custom_variables

conversions.append(to_upload)

request_body = {
'conversions': conversions,
}

logger.info(f'Conversions: \n{conversions}')

request = service.conversions().batchinsert(
profileId=campaign_manager_account_id, body=request_body)
response = request.execute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_conversion_upload(mocker, uploader):


def test_conversion_upload_match_id(mocker, uploader):
mocker.patch.object(uploader, '_get_dcm_service')
mocker.patch.object(uploader, '_get_dcm_service', autospec=True)

floodlight_activity_id = 'floodlight_activity_id'
floodlight_configuration_id = 'floodlight_configuration_id'
Expand Down Expand Up @@ -124,6 +124,63 @@ def test_conversion_upload_match_id(mocker, uploader):
profileId='dcm_profile_id', body=expected_body)


def test_conversion_upload_match_id_additional_fields(mocker, uploader):
mocker.patch.object(uploader, '_get_dcm_service', autospec=True)

floodlight_activity_id = 'floodlight_activity_id'
floodlight_configuration_id = 'floodlight_configuration_id'

source = Source('orig1', SourceType.BIG_QUERY, ('dt1', 'buyers'))
destination = Destination(
'dest1',
DestinationType.CM_OFFLINE_CONVERSION,
(floodlight_activity_id, floodlight_configuration_id))
execution = Execution(_account_config, source, destination)
current_time = time.time()

mocker.patch.object(time, 'time')
time.time.return_value = current_time

conversions_input = [{
'matchId': 'abc',
'value': 1,
'quantity': 2,
'customVariables': [{
'type': 'U1',
'value': "5.6",
}, {
'type': 'U2',
'value': "abcd",
}]
}]

expected_body = {
'conversions': [{
'matchId': 'abc',
'floodlightActivityId': floodlight_activity_id,
'floodlightConfigurationId': floodlight_configuration_id,
'ordinal': math.floor(current_time * 10e5),
'timestampMicros': math.floor(current_time * 10e5),
'value': 1,
'quantity': 2,
'customVariables': [{
'type': 'U1',
'value': "5.6",
'kind': 'dfareporting#customFloodlightVariable',
}, {
'type': 'U2',
'value': "abcd",
'kind': 'dfareporting#customFloodlightVariable',
}]
}],
}

uploader._do_process(Batch(execution, conversions_input), current_time)

uploader._get_dcm_service().conversions().batchinsert.assert_any_call(
profileId='dcm_profile_id', body=expected_body)


def test_error_on_api_call(mocker, uploader, caplog):
caplog.set_level(logging.INFO, 'megalista.CampaignManagerConversionsUploader')
mocker.patch.object(uploader, '_get_dcm_service')
Expand Down

0 comments on commit 202474a

Please sign in to comment.