示例#1
0
def submit_hubspot_cta_form(request):
    form_data = {data: value[0] for data, value in dict(request.POST).items()}
    form_id = form_data.pop('hubspot_form_id')
    page_url = form_data.pop('page_url')
    page_name = form_data.pop('page_name')

    hubspot_cookie = request.COOKIES.get(HUBSPOT_COOKIE)
    form_data['hs_context'] = json.dumps({
        "hutk":
        hubspot_cookie,
        "ipAddress":
        get_client_ip_from_request(request),
        "pageUrl":
        page_url,
        "pageName":
        page_name,
    })

    hubspot_id = settings.ANALYTICS_IDS.get('HUBSPOT_API_ID')
    if not hubspot_id:
        return JsonResponse({
            "success":
            False,
            "message":
            gettext("No hubspot API ID is present."),
        })

    url = f"https://forms.hubspot.com/uploads/form/v2/{hubspot_id}/{form_id}"
    response = requests.post(url, data=form_data)
    log_response('HS', form_data, response)
    response.raise_for_status()

    return JsonResponse({
        "success": True,
    })
示例#2
0
def identify(email, properties):
    """
    Set the given properties on a KISSmetrics user.
    :param email: The email address by which to identify the user.
    :param properties: A dictionary or properties to set on the user.
    :return:
    """
    api_key = settings.ANALYTICS_IDS.get("KISSMETRICS_KEY", None)
    if api_key and analytics_enabled_for_email(email):
        km = KISSmetrics.Client(key=api_key)
        res = km.set(email, properties)
        log_response("KM", {'email': email, 'properties': properties}, res)
        # TODO: Consider adding some better error handling for bad/failed requests.
        _raise_for_urllib3_response(res)
示例#3
0
def _hubspot_post(url, data):
    """
    Lightweight wrapper to add hubspot api key and post data if the HUBSPOT_API_KEY is defined
    :param url: url to post to
    :param data: json data payload
    :return:
    """
    api_key = settings.ANALYTICS_IDS.get('HUBSPOT_API_KEY', None)
    if api_key:
        headers = {'content-type': 'application/json'}
        params = {'hapikey': api_key}
        response = _send_post_data(url, params, data, headers)
        log_response('HS', data, response)
        response.raise_for_status()
示例#4
0
def _send_form_to_hubspot(form_id,
                          webuser,
                          hubspot_cookie,
                          meta,
                          extra_fields=None,
                          email=False):
    """
    This sends hubspot the user's first and last names and tracks everything they did
    up until the point they signed up.
    """
    if ((webuser and not hubspot_enabled_for_user(webuser))
            or (not webuser and not hubspot_enabled_for_email(email))):
        # This user has analytics disabled
        metrics_counter('commcare.hubspot.sent_form.rejected')
        return

    hubspot_id = settings.ANALYTICS_IDS.get('HUBSPOT_API_ID')
    if hubspot_id and hubspot_cookie:
        data = {
            'email':
            email if email else webuser.username,
            'hs_context':
            json.dumps({
                "hutk": hubspot_cookie,
                "ipAddress": get_client_ip_from_meta(meta)
            }),
        }
        if webuser:
            data.update({
                'firstname': webuser.first_name,
                'lastname': webuser.last_name,
            })
        if extra_fields:
            data.update(extra_fields)

        response = _send_hubspot_form_request(hubspot_id, form_id, data)
        log_response('HS', data, response)
        response.raise_for_status()
示例#5
0
def _track_workflow_task(email, event, properties=None, timestamp=0):
    def _no_nonascii_unicode(value):
        if isinstance(value, str):
            return value.encode('utf-8')
        return value

    api_key = settings.ANALYTICS_IDS.get("KISSMETRICS_KEY", None)
    if api_key:
        km = KISSmetrics.Client(key=api_key)
        res = km.record(
            email, event, {
                _no_nonascii_unicode(k): _no_nonascii_unicode(v)
                for k, v in properties.items()
            } if properties else {}, timestamp)
        log_response(
            "KM", {
                'email': email,
                'event': event,
                'properties': properties,
                'timestamp': timestamp
            }, res)
        # TODO: Consider adding some better error handling for bad/failed requests.
        _raise_for_urllib3_response(res)