Update: Thoroughly improved version of the environment available here
It can be important to understand what your users’ are favoriting.
That is the case whether you need to cut down on existing content and want to know which content is more highly prized, or you are creating a new and better dashboard, and want to know who has favorited the old dashboard so you can inform them.
However, Looker’s methods of retrieving your user’s favorited contents are severely limited. If you are a normal user, you can only see your own favorited content. Which might be fine for you.. but if you are an admin, you also only see your own favorite content? Whatever arcane reason led to this situation, there is a way to get the full list. But you DO need to be an admin for it.
First, we need to be an admin.
After that, we can create an API ID/Secret for ourselves.
And after that… we create our development environment and get to work tinkering away. (Breakdown shown below if you can’t view the page)
In the above link, I have shared my completed work environment and method for solving this issue, but lets continue.
If we use the looker api explorer to search for sdk related to favorite contents, and test different content ids we quickly find that you can only retrieve favorite contents for the current user. This is why we need to be an admin for this workaround. The fix is that we need to loop authenticating as every single user in the instance and perform the sdk call as each user.
In order to get the list of favorited content we need to perform 4 actions:
Below I will detail the methods with picture, but because pictures are big, they are hidden by default. You can click on the button below to show them.
I HIGHLY RECOMMEND COPYING THE ENVIRONMENT TO YOUR OWN gDRIVE. HOWEVER, ANYTHING YOU WRITE IN THE NOTEBOOK IS NOT ACCESSIBLE BY ANY OTHER USER.
First, there are two main phases to creating the environment, the first on is getting the SDK Initialization working, and the second is to get the code functional.
If we open up the “Run me to initialize SDK” section, we can see there are a few code snippets broken up. In shared Colab environment, I have added the parameters we need to adjust on the right-hand side to make them easy to setup.
The code is a little updated from the displayed code below, but is functionally the same.
After you have each of the form fields to your specifications (NOTE: you really only need to change the base_url, client_id and client_secret), we can get to the SDK calling.
I am not a Python savant, so there may be a better way, and please let me know if there is one!
Borrowing from our above list:
user_list = dict()
users = sdk.all_users()
for user in users:
if user['email'] and not user['is_disabled']: #removing users that don't have emails setup and aren't disabled
user_list[user['email']] = user['id']
print(user_list)
The first few code snippets don’t check if you are actually correctly authenticated in, so you might get some errors at this point.
If everything works out, you should see a printed list, and you can open the variables tab to see the full list of users.
dashboard_list = dict()
dashboards = sdk.all_dashboards()
for dashboard in dashboards:
if dashboard['content_metadata_id']: #removing dashboards without content_metadata_id
dashboard_list[dashboard['title']] = dashboard['content_metadata_id']
print(dashboard_list)
You can use the same method as above to check if the dashboards are getting retrieved.
favorited_content = dict()
def retrieve_favorite_content(): #Create a function so we can loop it for each user
for k,v in dashboard_list.items():
response = sdk.search_content_favorites(content_metadata_id=v)
if response:
email = [k for k,v in user_list.items() if v == response[0]['user_id']][0] #Change user_id into email for data readability
dashboard = [k for k,v in dashboard_list.items() if v == response[0]['content_metadata_id']][0] #Change content_metadata_id into dashboard name for data readability
if email in favorited_content: #If we already have a favorited content, we need to append the new content
favorited_content[email].append(dashboard)
else: #If we don't have any favorited content yet, we add the new content as a list
favorited_content[email] = []
favorited_content[email].append(dashboard)
for k,v in user_list.items():
sdk.auth.logout() #This is required if you stopped the sdk mid-way
sudo_auth = sdk.login_user(v)
sdk.auth.login_user(v)
retrieve_favorite_content()
print(favorited_content)
Before running this.. I must say that it takes a LONG time. I ran this for 30 minutes on my instance to retrieve the values, and it might take much longer if you have a bigger instance. Hopefully someone can mention a more performant way in the comments…
Having said that, you can test that it is working correctly, by stopping at any time, or during the process you can check the favorited_contents dictionary in the variables view.
The above processes’ setup shouldn’t take more than a few minutes. However, the SDK call is unfortunately very long because of the looping through each user and then each content. Again, probably a better way to do this, and if someone shares it I will update this.
However, it IS possible to get the information in exchange for runtime. From there, you can copy/paste the favorited_contents dictionary, or you can add a write_file method etc.