ImpectAPI / impectR

An R package to facilitate interaction with the Impect customer API
Other
2 stars 0 forks source link

impectR

A package provided by: Impect GmbH

Version: v2.2.0

Updated: Ocotber 17th 2024


Supported API Version: V5
For older versions, please see list below:


Introduction

The goal of the impectR package is to provide an easy way for Impect Customers to access data from the customer API. This API includes basic information about competitions, competition iterations, and matches as well as event data and aggregated scorings per player and position on match and season level.

Installation

You can install the latest version of impectR from GitHub with:

# install.packages("devtools")
devtools::install_github("ImpectAPI/impectR@v2.2.0")

Usage

Getting started

Before accessing any data via our API, you will need to request a bearer token for authorization. You can get this authorization token using the following code snippet:

library(impectR)

# define login credentials
username <- "yourUsername"
password <- "yourPassword"

# get access token
token <- getAccessToken(username = username, password = password)

This access token is a requirement to use any of the functions that requests data from the API. We recommend to first get a list of competition iterations that are enabled for your account.

Retrieve Basic Information

# get list of iterations
iterations <- getIterations(token = token)

# print iterations to console
iterations

If any iteration you were expected to see is not listed, please contact your sales representative. Now let’s assume you are interested in data for 2022/23 season of the 1. Bundesliga (iteration = 518). The following snippet gets you a list of matches for this iteration:

# get matches for iteration
matches <- getMatches(iteration = 518, token = token)

# print matches to console
matches

The column available denotes whether a given match has been tagged by Impect and the data is available to you.

Retrieve Match Level Data

Let’s assume you are interested in the FC Bayern München vs Borussia Dortmund game from April 1st 2023 (matchId = 84344). As the function allow for multiple games to be requested at once, we need to wrap the matchId into a list. Hence, to request the event data for this game, run the following code snippet:

# define matches to get event data for
matchIds <- c(84344)

# get event data for match
events <- getEvents(matches = matchIds, token = token)

# print first few rows from events dataframe to console
head(events)

You can access the aggregated KPIs, scores and ratios per player and position or per squad for this match in a similar way. Also, we provide you with IMPECT scores and ratios that you might know from our Scouting and Analysis portals. On player level, these are calculated across positions which is why you have to supply the function with a list of positions your want to retrieve data for:

# define matches to get matchsums for
matchIds <- c(84344)

# get kpi matchsums for match per player and position
playerMatchsums <- getPlayerMatchsums(matches = matchIds, token = token)

# get kpi matchsums for match per squad
squadMatchsums <- getSquadMatchsums(matches = matchIds, token = token)

# define positions to get scores aggregated by
positions <- c("LEFT_WINGBACK_DEFENDER", "RIGHT_WINGBACK_DEFENDER")

# get player scores and ratios for match and positions per player
playerMatchScores <- 
  getPlayerMatchScores(matches = matchIds, positions = positions, token = token)

# get squad scores and ratios for match per squad
squadMatchScores <- getSquadMatchScores(matches = matchIds, token = token)

In case you wish to retrieve data for multiple matches, we suggest using the following method to do so in order to minimize the amount of requests sent to the API. Let’s also get the event data for the RB Leipzig vs FSV Mainz 05 game (matchId = 84350) from the same day:

# define list of matches
matchIds <- c(84344, 84350)

# get event data for matches
events <- getEvents(matches = matchIds, token = token)

# get matchsums for matches per player and position
playerMatchsums <- getPlayerMatchsums(matches = matchIds, token = token)

# get matchsums for matches per squad
squadMatchsums <- getSquadMatchsums(matches = matchIds, token = token)

# define positions to get scores aggregated by
positions <- c("LEFT_WINGBACK_DEFENDER", "RIGHT_WINGBACK_DEFENDER")

# get player scores and ratios for match and positions per player
playerMatchScores <- 
  getPlayerMatchScores(matches = matchIds, positions = positions, token = token)

# get squad scores and ratios for match per squad
squadMatchScores <- getSquadMatchScores(matches = matchIds, token = token)

Retrieve Iteration Level Data

Starting from API version V5, we also offer an endpoint to get KPI average values per iteration on player as well as squad level. These averages are calculated by dividing the kpi sum of all individual matches by the sum of matchShares the player accumulated at a given position. On a team level we divide the score by the amount of matches played by the team. Also, we provide you with IMPECT scores and ratios that you might know from our Scouting and Analysis portals. On player level, these are calculated across positions which is why you have to supply the function with a list of positions your want to retrieve data for. Let’s assume you were interested in wingbacks in the 2022/2023 Bundesliga season, then you could use this code snippet:

# define iteration ID
iteration <- 518

# define positions to get scores aggregated by
positions <- c("LEFT_WINGBACK_DEFENDER", "RIGHT_WINGBACK_DEFENDER")

# get player kpi averages for iteration
playerIterationAverages <-
  getPlayerIterationAverages(iteration = iteration, token = token)

# get squad kpi averages for iteration
squadIterationAverages <-
  getSquadIterationAverages(iteration = iteration, token = token)

# get player scores and ratios for iteration and positions
playerIterationScores <- 
  getPlayerIterationScores(iteration = iteration, positions = positions, token = token)

# get squad scores and ratios for iteration
squadIterationScores <- 
  getSquadIterationScores(iteration = iteration, token = token)

Please keep in mind that Impect enforces a rate limit of 10 requests per second per user. A token bucket logic has been implemented to restrict the amount of API calls made on the client side already. The rate limit is read from the first limit policy sent back by the API, so if this limit increases over time, this package will act accordingly.

Final Notes

Further documentation on the data and explanations of variables can be found in our glossary.