def test_normalize_and_to_dict(self): product_id = "product-1" dict_fields = { "quantity": 2, "item_price": 3.14, "title": "title4", "description": "description5", "brand": "brand6", "category": "category7", } normalized_fields = dict_fields.copy() normalized_fields["id"] = product_id dict_fields["product_id"] = product_id content = Content( product_id=dict_fields["product_id"], quantity=dict_fields["quantity"], item_price=dict_fields["item_price"], title=dict_fields["title"], description=dict_fields["description"], brand=dict_fields["brand"], category=dict_fields["category"], ) self.assertEqual(content.to_dict(), dict_fields) self.assertEqual(content.normalize(), normalized_fields)
def notify_view_content(product_id, url, remote_address, user_agent): user_data = UserData( # It is recommended to send Client IP and User Agent for Conversions API Events. client_ip_address=remote_address, client_user_agent=user_agent # fbc='fb.1.1554763741205.AbCdEfGhIjKlMnOpQrStUvWxYz1234567890', # fbp='fb.1.1558571054389.1098115397', ) content = Content( product_id=product_id, ) custom_data = CustomData( contents=[content], ) event = Event( event_name='ViewContent', event_time=int(time.time()), user_data=user_data, custom_data=custom_data, event_source_url=url, action_source=ActionSource.WEBSITE, ) event_request = EventRequest( events=[event], pixel_id=FACEBOOK_PIXEL_ID, ) event_response = event_request.execute()
def test_delivery_category_is_validated_when_set(self): delivery_category = 'undefined_delivery_category' with self.assertRaises(TypeError) as context: Content( delivery_category=delivery_category, ) expected_exception_message = 'delivery_category must be of type DeliveryCategory. Passed invalid category: ' + delivery_category; self.assertTrue(expected_exception_message in str(context.exception))
def test_not_equals(self): content1 = Content( product_id="product-1", quantity=2, item_price=3.14, title="title4", description="description5", brand="brand6", category="category7", ) content2 = Content( product_id="product-1", quantity=2, item_price=3.14, title="title4", # no description brand="brand6", category="category7", ) self.assertTrue(content1 != content2)
def notify_initiate_checkout(basket_id, user_id, url, remote_address, user_agent): basket = Basket.objects.get(id=basket_id) user_phone = None user_email = None if user_id: user = ShopUser.objects.get(id=user_id) user_phone = user.phone user_email = user.email user_data = UserData( email=user_email, phone=user_phone, # It is recommended to send Client IP and User Agent for Conversions API Events. client_ip_address=remote_address, client_user_agent=user_agent # fbc='fb.1.1554763741205.AbCdEfGhIjKlMnOpQrStUvWxYz1234567890', # fbp='fb.1.1558571054389.1098115397', ) contents = [] for item in basket.items.all(): contents.append(Content( product_id=item.product.id, title=item.product.title, brand=item.product.manufacturer.name, item_price=str(item.price.quantize(Decimal('1'), rounding=ROUND_HALF_EVEN)), quantity=item.quantity )) custom_data = CustomData( contents=contents, content_type='product', currency='rub', value=float(basket.total) ) event = Event( event_name='InitiateCheckout', event_time=int(time.time()), user_data=user_data, custom_data=custom_data, event_source_url=url, action_source=ActionSource.WEBSITE, ) event_request = EventRequest( events=[event], pixel_id=FACEBOOK_PIXEL_ID, ) event_response = event_request.execute() log.error(event_response)
def test_normalize(self): content = Content(product_id='id0', quantity='quantity1', item_price=3.99) custom_properties = {'custom1': 'property1', 'custom2': 'property2'} expected = { 'value': 0.5, 'currency': 'usd', 'content_name': 'content-content1', 'content_category': 'content-category2', 'content_ids': ['id1', 'id2'], 'content_type': 'content-type3', 'contents': [ { 'id': content.product_id, 'quantity': content.quantity, 'item_price': content.item_price, } ], 'order_id': 'order-id4', 'predicted_ltv': 5.99, 'num_items': 6, 'status': 'status7', 'search_string': 'search-string8', 'item_number': 'item-number9', 'custom1': 'property1', 'custom2': 'property2', } custom_data = CustomData( value=expected['value'], currency=expected['currency'], content_name=expected['content_name'], content_category=expected['content_category'], content_ids=expected['content_ids'], contents=[content], content_type=expected['content_type'], order_id=expected['order_id'], predicted_ltv=expected['predicted_ltv'], num_items=expected['num_items'], status=expected['status'], search_string=expected['search_string'], item_number=expected['item_number'], custom_properties=custom_properties, ) self.assertEqual(custom_data.normalize(), expected)
def notify_add_to_cart(product_id, url, remote_address, user_agent): product = Product.objects.get(id=product_id) user_data = UserData( # It is recommended to send Client IP and User Agent for Conversions API Events. client_ip_address=remote_address, client_user_agent=user_agent # fbc='fb.1.1554763741205.AbCdEfGhIjKlMnOpQrStUvWxYz1234567890', # fbp='fb.1.1558571054389.1098115397', ) content = Content( product_id=product_id, item_price=str(product.price.quantize(Decimal('1'), rounding=ROUND_HALF_EVEN)) ) custom_data = CustomData( contents=[content], content_type='product', currency='rub' ) event = Event( event_name='AddToCart', event_time=int(time.time()), user_data=user_data, custom_data=custom_data, event_source_url=url, action_source=ActionSource.WEBSITE, ) event_request = EventRequest( events=[event], pixel_id=FACEBOOK_PIXEL_ID, ) event_response = event_request.execute() log.error(event_response)
FacebookAdsApi.init(access_token=access_token) user_data = UserData( emails=['*****@*****.**'], phones=['12345678901', '14251234567'], # It is recommended to send Client IP and User Agent for Conversions API Events. client_ip_address=request.META.get('REMOTE_ADDR'), client_user_agent=request.headers['User-Agent'], fbc='fb.1.1554763741205.AbCdEfGhIjKlMnOpQrStUvWxYz1234567890', fbp='fb.1.1558571054389.1098115397', ) content = Content( product_id='product123', quantity=1, delivery_category=DeliveryCategory.HOME_DELIVERY, ) custom_data = CustomData( contents=[content], currency='usd', value=123.45, ) event = Event( event_name='Purchase', event_time=int(time.time()), user_data=user_data, custom_data=custom_data, event_source_url='http://jaspers-market.com/product/123',
# Should fill in value before running this script. access_token = None pixel_id = None if access_token is None or pixel_id is None: raise Exception( "You must set your access token and pixel id before executing") FacebookAdsApi.init(access_token=access_token) user_data = UserData(email="*****@*****.**", phone="16505551212", country_code="us", gender=Gender.FEMALE) content = Content(product_id="XYZ789", quantity=4, item_price=3.5) contents = [content] custom_data = CustomData(contents=contents, value=14.0, currency="usd") event = Event( event_name="Purchase", event_time=int(time.time()), user_data=user_data, custom_data=custom_data, ) events = [event] event_request = EventRequest(events=events, pixel_id=pixel_id) event_response = event_request.execute()