示例#1
0
def adapt_html(html_text, extra_metadata, click_tracking=True, open_tracking=True, configuration=None, **kwargs):
    """Changes an HTML string by replacing links (<a href...>) with tracking
    links and by adding a 1x1 transparent pixel just before the closing body
    tag.

    :param html_text: The HTML to change (unicode or bytestring).
    :param extra_metadata: A dict that can be json-encoded and that will
        be encoded in the tracking link.
    :param click_tracking: If links (<a href...>) must be changed.
    :param open_tracking: If a transparent pixel must be added before the
        closing body tag.
    :param configuration: An optional Configuration instance.
    :param kwargs: Optional configuration parameters. If provided with a
        Configuration instance, the kwargs parameters will override the
        Configuration parameters.
    """
    configuration = get_configuration(configuration, kwargs)

    tree = html.fromstring(html_text)

    if click_tracking:
        _replace_links(tree, extra_metadata, configuration)

    if open_tracking:
        _add_tracking_pixel(tree, extra_metadata, configuration)

    new_html_text = html.tostring(tree)

    return new_html_text.decode("utf-8")
示例#2
0
def get_tracking_result(request, path, is_open, configuration=None, **kwargs):
    """Builds a tracking result from a Django request.

    :param request: A Django request
    :param path: The path of the URL that is encoded (usually possible to
        distinguish this path using a URL parameter).
    :param is_open: If this is an open tracking link request.
    :param configuration: An optional Configuration instance.
    :param kwargs: Optional configuration parameters. If provided with a
        Configuration instance, the kwargs parameters will override the
        Configuration parameters.
    """
    configuration = get_configuration(configuration, kwargs)
    request_data = get_request_data(request)
    return configuration.get_tracking_result(path, request_data, is_open)
示例#3
0
def send_webhook(tracking_result, configuration=None, **kwargs):
    """Sends a POST request to the webhook URL specified in tracking_result.

    The POST request will have a body of type application/json that contains a
    json representation of the tracking result:

    ::

        {
            "is_open_tracking": False,
            "is_click_tracking": True,
            "metadata": {...},
            "request_data": None,
            "tracked_url": "http://...",
            "timestamp": 1389177318
        }

    :param tracking_result: The TrackingResult instance to post to a webhook.
    :param configuration: An optional Configuration instance.
    :param kwargs: Optional configuration parameters. If provided with a
        Configuration instance, the kwargs parameters will override the
        Configuration parameters.
    """
    configuration = get_configuration(configuration, kwargs)

    payload = {
        "is_open_tracking": tracking_result.is_open_tracking,
        "is_click_tracking": tracking_result.is_click_tracking,
        "metadata": tracking_result.metadata,
        "request_data": tracking_result.request_data,
        "timestamp": tracking_result.timestamp
    }

    if tracking_result.tracked_url:
        payload["tracked_url"] = tracking_result.tracked_url

    response = requests.post(
        tracking_result.webhook_url, json=payload,
        timeout=configuration.webhook_timeout_seconds)

    return response
示例#4
0
def send_webhook(tracking_result, configuration=None, **kwargs):
    """Sends a POST request to the webhook URL specified in tracking_result.

    The POST request will have a body of type application/json that contains a
    json representation of the tracking result:

    ::

        {
            "is_open_tracking": False,
            "is_click_tracking": True,
            "metadata": {...},
            "request_data": None,
            "tracked_url": "http://...",
            "timestamp": 1389177318
        }

    :param tracking_result: The TrackingResult instance to post to a webhook.
    :param configuration: An optional Configuration instance.
    :param kwargs: Optional configuration parameters. If provided with a
        Configuration instance, the kwargs parameters will override the
        Configuration parameters.
    """
    configuration = get_configuration(configuration, kwargs)

    payload = {
        "is_open_tracking": tracking_result.is_open_tracking,
        "is_click_tracking": tracking_result.is_click_tracking,
        "metadata": tracking_result.metadata,
        "request_data": tracking_result.request_data,
        "timestamp": tracking_result.timestamp
    }

    if tracking_result.tracked_url:
        payload["tracked_url"] = tracking_result.tracked_url

    response = requests.post(tracking_result.webhook_url,
                             json=payload,
                             timeout=configuration.webhook_timeout_seconds)

    return response
示例#5
0
def get_configuration_from_settings(settings_name="PYTRACKING_CONFIGURATION"):
    """Builds a Configuration instance from the parameters in
    settings.PYTRACKING_CONFIGURATION.
    """
    kwargs = getattr(settings, settings_name)
    return get_configuration(None, kwargs)