def get_sessions_by_dimensions(
    service, profile_id, startDate, endDate, segment, dimensions, metrics="ga:sessions"
):
    """
    Query the API for channel grouping for sessions WITH dimensions
    And sort query output by the first dimension specified
    
    Args:
        service: authenticated connection object
        profile_id: view in GA in the form 'ga:12345678'
        startDate: start of query as YYYY-MM-DD
        endDate: end of query as YYYY-MM-DD
        segment: Segment to apply. Leave blank if not required.
        dimensions: string of comma separated values. 
        metrics: string of comma separated values. Leave blank for default.
        
    Returns: query output
    """
    # if there is more than one dimension,
    # use a substring for the first dimension before the 1st comma
    sort = dimensions
    if sort.find(",") > 0:
        sort = sort[: sort.find(",")]

    # Execute.
    # Choose between query structure with and without segment
    if segment:
        result = (
            service.data()
            .ga()
            .get(
                ids=profile_id,
                start_date=startDate,
                end_date=endDate,
                metrics=metrics,
                segment=segment,  # with segment
                dimensions=dimensions,
                sort=sort,
            )
            .execute()
        )
    else:
        result = (
            service.data()
            .ga()
            .get(
                ids=profile_id,
                start_date=startDate,
                end_date=endDate,
                metrics=metrics,
                dimensions=dimensions,
                sort=sort,
            )
            .execute()
        )
    return result