Beispiel #1
0
def identify_org(org, attributes=None):
    """
    Creates and identifies an org on our analytics backends where appropriate
    """
    if not settings.IS_PROD:
        return

    if not attributes:
        attributes = {}

    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=json.encode_datetime(org.created_on),
            custom_attributes=attributes,
            **intercom_attributes,
        )
Beispiel #2
0
def identify_org(org, attributes=None):
    """
    Creates and identifies an org on our analytics backends where appropriate
    """
    if not settings.IS_PROD:
        return

    if not attributes:
        attributes = {}

    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=json.encode_datetime(org.created_on),
            custom_attributes=attributes,
            **intercom_attributes,
        )
Beispiel #3
0
def change_consent(email, consent):
    """
    Notifies 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 = json.encode_datetime(timezone.now())

            user = get_intercom_user(email)

            if consent:
                if not user or not user.custom_attributes.get(
                        "consent", False):
                    _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 Exception:
            logger.error("error posting to intercom", exc_info=True)
Beispiel #4
0
def change_consent(email, consent):
    """
    Notifies 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 = json.encode_datetime(timezone.now())

            user = get_intercom_user(email)

            if consent:
                if not user or not user.custom_attributes.get("consent", False):
                    _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 Exception:
            logger.error("error posting to intercom", exc_info=True)
Beispiel #5
0
def identify(user, brand, org):
    """
    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

    attributes = dict(email=user.username,
                      first_name=user.first_name,
                      segment=randint(1, 10),
                      last_name=user.last_name,
                      brand=brand)
    user_name = f"{user.first_name} {user.last_name}"
    if org:
        attributes["org"] = org.name
        attributes["paid"] = org.account_value()

    # post to segment if configured
    if _segment:  # pragma: no cover
        segment_analytics.identify(user.username, 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=user.username,
                name=user_name,
                custom_attributes=attributes)

            intercom_user.companies = [
                dict(
                    company_id=org.id,
                    name=org.name,
                    created_at=json.encode_datetime(org.created_on),
                    custom_attributes=dict(brand=org.brand, org_id=org.id),
                )
            ]

            _intercom.users.save(intercom_user)
        except Exception:
            logger.error("error posting to intercom", exc_info=True)
Beispiel #6
0
def identify(user, brand, org):
    """
    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

    attributes = dict(
        email=user.username, first_name=user.first_name, segment=randint(1, 10), last_name=user.last_name, brand=brand
    )
    user_name = f"{user.first_name} {user.last_name}"
    if org:
        attributes["org"] = org.name
        attributes["paid"] = org.account_value()

    # post to segment if configured
    if _segment:  # pragma: no cover
        segment_analytics.identify(user.username, 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=user.username, name=user_name, custom_attributes=attributes)

            intercom_user.companies = [
                dict(
                    company_id=org.id,
                    name=org.name,
                    created_at=json.encode_datetime(org.created_on),
                    custom_attributes=dict(brand=org.brand, org_id=org.id),
                )
            ]

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