Beispiel #1
0
def change_consent(email, consent):  # pragma: no cover
    """
    Notities analytics backends of a user's consent status.
    """
    # no op if we aren't prod
    if not settings.IS_PROD:
        return

    if _intercom:
        try:
            change_date = datetime_to_json_date(timezone.now())

            user = get_intercom_user(email)

            if consent:
                if not user or not user.custom_attributes.get("consent", False):
                    print(email, "consents")
                    _intercom.users.create(
                        email=email, custom_attributes=dict(consent=consent, consent_changed=change_date)
                    )
            else:
                if user:
                    _intercom.users.create(
                        email=email, custom_attributes=dict(consent=consent, consent_changed=change_date)
                    )

                    # this archives a user on intercom so they are no longer processed
                    _intercom.users.delete(user)

        except:
            logger.error("error posting to intercom", exc_info=True)
Beispiel #2
0
def identify_org(org, attributes={}):  # pragma: no cover
    """
    Creates and identifies an org on our analytics backends where appropriate
    """
    if not settings.IS_PROD:
        return

    if _intercom:

        intercom_attributes = {}
        for key in ("monthly_spend", "industry", "website"):
            value = attributes.pop(key, None)
            if value:
                intercom_attributes[key] = value

        attributes["brand"] = org.brand
        attributes["org_id"] = org.id

        _intercom.companies.create(
            company_id=org.id,
            name=org.name,
            created_at=datetime_to_json_date(org.created_on),
            custom_attributes=attributes,
            **intercom_attributes
        )
Beispiel #3
0
def identify(email, name, attributes, orgs=[]):  # pragma: no cover
    """
    Creates and identifies a new user to our analytics backends. It is ok to call this with an
    existing user, their name and attributes will just be updated.
    """
    # no op if we aren't prod
    if not settings.IS_PROD:
        return

    # post to segment if configured
    if _segment:
        segment_analytics.identify(email, attributes)

    # post to intercom if configured
    if _intercom:
        try:
            # rip out duplicate fields for intercom
            for key in ("first_name", "last_name", "email"):
                attributes.pop(key, None)

            intercom_user = _intercom.users.create(email=email, name=name, custom_attributes=attributes)

            intercom_user.companies = [
                dict(
                    company_id=org.id,
                    name=org.name,
                    created_at=datetime_to_json_date(org.created_on),
                    custom_attributes=dict(brand=org.brand, org_id=org.id),
                )
                for org in orgs
            ]

            _intercom.users.save(intercom_user)
        except:
            logger.error("error posting to intercom", exc_info=True)
Beispiel #4
0
def format_datetime(value):
    """
    Datetime fields are formatted with microsecond accuracy for v2
    """
    return datetime_to_json_date(value, micros=True) if value else None
Beispiel #5
0
def format_datetime(value):
    """
    Datetime fields are limited to millisecond accuracy for v1
    """
    return datetime_to_json_date(value, micros=False) if value else None